pressBack() is ignored in a Espresso Test

4.1k views Asked by At

The Espresso's method pressBack() is totally ignored. I'm running the test on the emulator.

What I'm testing? Just calling an Activity, launching a second one, performing two backs, and checking that I'm out of the app.

3

There are 3 answers

2
Roc Boronat On BEST ANSWER

Use

import static android.support.test.espresso.Espresso.pressBack;

or

import static androidx.test.espresso.Espresso.pressBack;

instead of

import static android.support.test.espresso.action.ViewActions.pressBack;

2
Roc Boronat On

Another solution there:

Stop using regular Espresso and start using Barista. There, clicking the back button is clickBack() instead of pressBack(), so it will never conflict with anything.

... and well, Barista gives a lot of more features, but I just got some karma points for this question, and asked myself why am I not having this issue anymore? Oh, because of Barista! I should introduce it here.

Feel free to check it out: https://github.com/AdevintaSpain/Barista

0
AmirMohamamd On

Use this for back press:

UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()).pressBack()

Use this implementation 'androidx.test.uiautomator:uiautomator:2.2.0' to implement UiDevice.