How to click editor on Espresso

How do you press the Editor Action key on an Android on-screen key using Espresso? I tried:

onView(withId(R.id.test_title)).perform(typeText("Sample Title"), pressKey(KeyEvent.FLAG_EDITOR_ACTION));

But it doesn't work. Any idea?

+6


source to share


3 answers


"pressKey" expects a KEY, not a FLAG. So pressing the key (KeyEvent.FLAG_EDITOR_ACTION) doesn't really make sense and definitely won't work.

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



You can see the implementation details for Espresso 1.x here:

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

+16


source


Since this is the best Google result for anyone looking for how to send keys using Espresso, I would like to give an example: onView(withId(R.id.your_id)).perform(ViewActions.pressKey(KeyEvent.YOUR_KEY));



+3


source


To send a shared key press in espresso, use something like this:

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

      

This, for example, will dispatch a hardware menu button event to any view to open the overflow menu in the action / toolbar.


Note: To quickly add imports for these methods, place the blinking cursor on the unresolved method and then run Android Studio βž” Help βž” Find Action βž” Find "show intention action"

βž” click the result parameter βž” A popup will appear βž” click on "Import static method ..."

. You can also assign a keyboard shortcut for Show Intentional Actions. More details here . Another way is to enable it "Add unambiguous imports on the fly"

in the settings.

+3


source







All Articles