Flutter Integration Screenshot not working for Android

Issue

I am trying to capture screenshots using integration_test but the

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

Hangs for Android with a message

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

The app is the Flutter default app with

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);
    });
  });
}

script

#!/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');
  }
}

For iOS, I also had an issue but I managed to solve it

I was getting an error

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

I changed IntegrationTestPlugin.m

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

Solution

Adding an additional tester.pumpAndSettle(); after convertFlutterSurfaceToImage fixed the problem

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

Answered By – Ryan Heitner

Answer Checked By – Marie Seifert (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *