How to press editor action on Espresso

To send a general key press in Espresso, use something like this:

onView(isRoot()).perform(pressKey(KeyEvent.KEYCODE_MENU));

This, for example, will send the hardware Menu button event to any View, to open the Overflow Menu in the ActionBar/ToolBar.


Note: To quickly add the imports for these methods, put the blinking cursor on the unresolved method, then do Android Studio ➔ HelpFind Action ➔ search for "show context action" or "show intention action" ➔ click on the result option ➔ A popup window will appear ➔ click on "Import static method ...". You can also assign a keyboard shortcut to "Show Context Actions". More info here. Another way is to enable "Add unambiguous imports on the fly" in the Settings.


Since this is a the top google result for someone searching how to send keys using Espresso, I'd like to provide an example: onView(withId(R.id.your_id)).perform(ViewActions.pressKey(KeyEvent.YOUR_KEY));


"pressKey" expects a KEYCODE, not a FLAG. So pressKey(KeyEvent.FLAG_EDITOR_ACTION) doesn't really make sense and will definitely not work.

But there is a ViewAction for pressing the editor (IME) action, see the static method: ViewActions#pressImeActionButton()

You can view the Espresso 1.x implementation details here:

https://developer.android.com/reference/android/support/test/espresso/action/ViewActions.html#pressImeActionButton()