13

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

Miguel Beltran
  • 2,132
  • 1
  • 23
  • 36
martinseal1987
  • 1,862
  • 8
  • 44
  • 77

3 Answers3

9

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,
);
Miguel Beltran
  • 2,132
  • 1
  • 23
  • 36
4

Maybe this can help you

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

Found in one of the official flutter driver tests link

-3

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

Darshan
  • 10,550
  • 5
  • 49
  • 61