How do you press the Editor Action key on Android softkey using Espresso? I tried:
onView(withId(R.id.test_title)).perform(typeText("Sample Title"), pressKey(KeyEvent.FLAG_EDITOR_ACTION));
But it's not working.. Any idea?
"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()
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));
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 ➔ Help ➔ Find 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.
The accepted answer was not clear for me and others don't work. The actual solution working is as below
Espresso.onView(ViewMatchers.withId(R.id.search_box))
.perform(ViewActions.pressImeActionButton())
where search_box is the id of my edittext.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With