using the system back button from flutter driver
Asked Answered
C

3

14

How can I use the system back button in an integration test?

So I'm using flutter and am writing integration tests, in most circumstances I can use the AppBar navigation, finding it by tool tip looks like this :

driver.tap(find.byTooltip('Back'));

But one of my tests opens a web page, after this opens I need to carry on with my tests which means I need to press the system back button, is this possible?

many thanks

Coati answered 1/3, 2019 at 15:33 Comment(0)
S
10

If you have adb installed on your machine, you can run a command to perform the backpress using a keyevent:

import 'dart:io';
await Process.run(
  'adb', 
  <String>['shell', 'input', 'keyevent', 'KEYCODE_BACK'], 
  runInShell: true,
);
Swaziland answered 9/3, 2020 at 9:9 Comment(3)
what about iPhones? how can we press the home button etc.. programmaticallyGlobate
iPhone's home button is not a back button, it directs the user to the device home. If you want to test the equivalent of a backpress on iOS you may want to try a drag from right to left.Swaziland
is there any way to press the Home screen on iPhone programmatically?Globate
F
4

Maybe this can help you

 await device.shellExec('input', <String>['keyevent', 'KEYCODE_BACK']);

Found in one of the official flutter driver tests link

Forestry answered 27/1, 2020 at 13:27 Comment(1)
Seems that the device.shellExec depends on the flutter_devicelab which is only available within the frameworkSwaziland
W
-3

Check out this link. In short, you'll need to use WillPopScope class that handles device back button with callbacks.

Wilding answered 2/3, 2019 at 18:6 Comment(1)
not applicable from flutter driver, im aware of willPopScope and its usage but thank youCoati

© 2022 - 2024 — McMap. All rights reserved.