Search code examples
flutterdartflutter-testflutter-integration-test

Flutter Integration Test: Fails on second screen


I wrote a basic Integration Test and had problems getting it to work. Basically I have a main screen that has a ÌconButton. If clicked it opens a secondary screen.

My test looked like this:

  group('end-to-end test', () {
    testWidgets('test settings', (WidgetTester tester) async {
      appmain.main();
      await tester.pumpAndSettle();

      final Finder settingButton = find.byTooltip('Settings');

      // Button is fine
      expect(settingButton, findsOneWidget);
      
      // Button is tapped -- a new screen should open
      await tester.tap(settingButton);

      // "Host" should be on the second screen, HOWEVER this test FAILS
      expect(find.text("Host"), findsWidgets);
    });
  });

So the question was: If the Button is successfully tapped, why does the test still fail?


Solution

  • Turns out if you tap the ÌconButton in the integration test there is still an animation -- just like if you tapped the button in production. Because the animation is still going on, the test expect(find.text("Host"), findsWidgets) fails because it can't find anything. The solution is to wait for the animation to finish by calling await tester.pumpAndSettle() before calling the failed line.


    Side note: I also tried using press instead of tap but this only caused new issues because press is something different in the conext of mobile apps. You most likely want to use tap.