How to write android espresso test script to click outside of overflow menu view

Issue

I would like to dismiss the Overflow menu(More options on action bar) without selecting menu item, since just wanted to cancel overflow menu view.

So How to write android espresso test script to perform click outside of overflow menu view?

Thanks!

Solution

Unfortunately it may not be possible to close overflow menu by touching anywhere outside with Espresso because the root view is very likely to be a menu popup layout. However, you can use UiDevice from UiAutomator:

UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()).click(0, 100)

You probably need to figure out x and y coordinates to click on the screen. Otherwise if you just want to close the overflow menu, I’d suggest a back press as the simplest solution:

Espresso.pressBack()

Answered By – Aaron

Leave a Comment