Flutter 集成屏幕截图不适用于 Android

Flutter Integration Screenshot not working for Android

我正在尝试使用 integration_test 捕获屏幕截图,但是

 await binding.takeScreenshot('${platform}-1'); 

挂起 Android 并发一条消息

VMServiceFlutterDriver: request_data message is taking a long time to complete...

该应用是 Flutter 默认应用

import 'package:flutter_driver/driver_extension.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'dart:io';
import 'package:flutter_stam/main.dart' as app;
void main() {
  final IntegrationTestWidgetsFlutterBinding binding = IntegrationTestWidgetsFlutterBinding();

  group('Screenshot', () {
    testWidgets('Screenshot-1', (WidgetTester tester) async {
      String platform;
      if (Platform.isAndroid) {
        platform = 'android';
      } else if (Platform.isIOS) {
        platform = 'ios';
      } else {
        platform = Platform.operatingSystem;
      }

      app.main();
      await tester.pumpAndSettle();
      // Verify the counter starts at 0.
      expect(find.text('0'), findsOneWidget);

      // Finds the floating action button to tap on.
      final Finder fab = find.byTooltip('Increment');

      // Emulate a tap on the floating action button.
      await tester.tap(fab);

      // Trigger a frame.
      await tester.pumpAndSettle();
      await binding.convertFlutterSurfaceToImage();
      await binding.takeScreenshot('${platform}-screenshot-1');

      // Verify the counter increments by 1.
      expect(find.text('1'), findsOneWidget);
    });
  });
}

脚本

#!/bin/zsh

flutter drive \
  --driver=integration_test/driver.dart \
  --target=integration_test/app_test.dart \
  -d "iPhone 13 Pro Max"

flutter drive \
  --driver=integration_test/driver.dart \
  --target=integration_test/app_test.dart \
  -d "emulator-5554"

Driver

import 'dart:io';
import 'package:integration_test/integration_test_driver_extended.dart';

Future<void> main() async {
  try {
    await integrationDriver(
      onScreenshot: (String screenshotName, List<int> screenshotBytes) async {
        final File image = await File('screenshots/$screenshotName.png')
            .create(recursive: true);
        image.writeAsBytesSync(screenshotBytes);
        return true;
      },
    );
  } catch (e) {
    print('An error occurred: $e');
  }
}

对于iOS,我也有一个问题,但我设法解决了

我遇到了一个错误

The following MissingPluginException was thrown running a test:
MissingPluginException(No implementation found for method
captureScreenshot on channel plugins.flutter.io/integration_test)

我改了IntegrationTestPlugin.m

+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
    [[IntegrationTestPlugin instance] setupChannels:registrar.messenger];
}

添加额外的 tester.pumpAndSettle(); convertFlutterSurfaceToImage 解决问题后

  await tester.pumpAndSettle();
  await binding.convertFlutterSurfaceToImage();
  await tester.pumpAndSettle();