Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to press editor action on Espresso

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?

like image 387
Jezer Crespo Avatar asked Jan 07 '15 05:01

Jezer Crespo


4 Answers

"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()

like image 122
yogurtearl Avatar answered Nov 16 '22 01:11

yogurtearl


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));

like image 6
HRVHackers Avatar answered Nov 16 '22 01:11

HRVHackers


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.

like image 3
Mr-IDE Avatar answered Nov 16 '22 01:11

Mr-IDE


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.

like image 1
Ishaan Avatar answered Nov 15 '22 23:11

Ishaan