Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Press Overview Button in Android Espresso

I want to create a test for my Android app. Everytime it loses focus, it should do something.

Therefore, I want to test this out by pressing the overview button 2 times (the button next to the home button). But how can I do this with Espresso? I tried out some Keycodes to press, but none of them worked (Unfortunately, there is no KEY_OVERVIEW in the table).

So how can I test this?

Thanks,

Niklas

like image 758
Niklas Wünsche Avatar asked Mar 10 '23 14:03

Niklas Wünsche


1 Answers

This is not possible with espresso, espresso is for UI testing within your app but can not interact with UI elements outside.

If you want to press the Overview (or Back- or any other system) button you can use ui-automator:

UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
mDevice.pressRecentApps();
mDevice.pressRecentApps();

this code above presses the recent Apps button twice.

ui-automator is exactly for interacting with UI outside your app (home or recent app button, permission dialogs, ....) and is perfectly combineable with espresso to enhance your tests. To use it include this in your gradle:

androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
like image 79
stamanuel Avatar answered Mar 16 '23 03:03

stamanuel