Flutter Integration Testing- How to go back to previous screen when no back button available on screen
Asked Answered
A

6

9

How to go back to previous screen when no back button available on screen? Additional Information: I am trying to automate integration testing for flutter app. There is no back button on screen which I can use to go back screen. Additional information: I am writing integration tests, for same I need to go back to previous screen. And due to design there is no back button on screen, I need to do by scripts only.

Aqueous answered 26/11, 2019 at 6:17 Comment(2)
You won't answer, for developing prospective or testing?Quinn
Testing perspectiveAqueous
L
8

This is what worked for me for OpenContainers from the animations package:

  final NavigatorState navigator = tester.state(find.byType(Navigator));
  navigator.pop();
  await tester.pump();

It's used in the flutter package's tests:

https://github.com/flutter/packages/blob/63040337424f345452a5e07632774e6585329ac0/packages/animations/test/open_container_test.dart#L234

Leverick answered 16/8, 2021 at 10:22 Comment(0)
F
2

If the purpose of the test is to stress and verify the WillPopScope callbacks as well, the following approach can be used:

final dynamic widgetsAppState = _._tester.state(find.byType(WidgetsApp));
await widgetsAppState.didPopRoute();

This is used in the Flutter official repository for testing: https://github.com/flutter/flutter/blob/master/packages/flutter/test/material/will_pop_test.dart

Note that this approach also works to simulate a back button pressed on Android.

Fortuneteller answered 27/5, 2020 at 8:56 Comment(0)
S
1

Well, it's maybe a little bit late, however there are two possible ways to achieve this:

final backButton = find.byTooltip('Back');

await driver.tap(backButton);

or:

await driver.tap(find.pageBack());
Stanley answered 18/2, 2020 at 22:53 Comment(0)
I
1

My emulador defaulf locale is "en_US" so I use the word "Back" as tooltip and it is working.

final backButton = find.byTooltip("Back");
await tester.tap(backButton);
await tester.pumpAndSettle();
Incunabula answered 4/1, 2023 at 16:2 Comment(0)
C
0

you can use like this as ios provided

theme: ThemeData(
pageTransitionsTheme: PageTransitionsTheme(
  builders: {
    TargetPlatform.android: CupertinoPageTransitionsBuilder(),
    TargetPlatform.iOS: CupertinoPageTransitionsBuilder(),
  }
 )
),
Chew answered 26/11, 2019 at 9:24 Comment(0)
I
0

In my integration tests, I used this way to tap the back button on the Android emulator:

   ...
   if(Platform.isAndroid) {
     await Process.run('input', <String>['keyevent', 'KEYCODE_BACK'] );
   }
   ...

I hope it helps!

Intemerate answered 6/5 at 17:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.