Matthew Pape
Matthew Pape

Reputation: 1691

Detect or Suppress Keyboard in UiAutomator Tests

In my UI tests I am setting the text of two UiObjects near the top of the screen, and clicking on third UiObject which is located near the bottom of the screen. On some devices (eg my Nexus 6 running 5.0) this works perfectly. The text is set, the UiObject at the bottom of the screen is still visible, and it is successfully clicked on.

On other devices (eg my Nexus 4 running 4.4) calling setText on the UiObjects brings up the soft keyboard, which obscures the third UiObject near the bottom of the screen and prevents it from being clicked on.

I considered using UiDevice.pressBack() to dismiss the keyboard, but the problem is that the keyboard shows on some devices and not others. Pressing back on the devices that do not show the keyboard causes other behavior which leads to the test failing.

I also considered using UiDevice.pressEnter() as a solution. I figured this would dismiss the keyboard if it was visible, and do nothing if it wasn't (best of both worlds). The problem is, that the keyboard shown by UiAutomator when it is running my tests does not have an "enter" button, it has a "next" button. This is contrary to the way I have the actual UI elements setup in code though. When I test this manually the keyboard shows the "enter" button as expected. However, since UiAutomator is seeing the "next" button, that is the functionality it is executing. The focus simply passes to the next UiObject and the keyboard is still visible.

So what I am trying to do is dismiss the keyboard IF it is visible, without risking pressing the back button if it is not. Is there a way to determine if it is showing or not? Or better yet, never show it in the first place?

Upvotes: 4

Views: 3479

Answers (3)

ActionWind
ActionWind

Reputation: 1

I think maybe you can try this : Write a if() on that step , if cannot find the UiObject in that view ,then UiDevice.pressBack().

Upvotes: 0

Eddy
Eddy

Reputation: 3723

Different device have different packages, and keyboards function may different too.
So, the details may not be all the same.

Steps listed below are succeed in Android 4.0+ on my device:

  1. Disable packages relative to keyboard input method.

    The commands are listed here:

    adb root
    adb shell pm disable com.google.android.apps.inputmethod.hindi
    adb shell pm disable com.google.android.inputmethod.korean
    adb shell pm disable com.google.android.inputmethod.pinyin
    adb shell pm disable com.google.android.inputmethod.latin
    adb shell pm disable jp.co.omronsoft.iwnnime.ml
    pause
    

    Note: Some package name like "com.google.android.inputmethod.pinyin" may not exist in device. Use this command to check:

    adb shell pm list packages inputmethod
    
  2. Disable the "Google voice typing"

    "Google voice typing" can still popup the keyboard.

    If you want to disable keyboard:

    Go to Settings → Apps → Running (or All) → "Google Keyboard" → Settings → Disable "Google voice typing".

Note: If you want enable the keyboard, run command like this:

adb shell pm enable com.google.android.inputmethod.latin

Upvotes: 2

Matthew Pape
Matthew Pape

Reputation: 1691

I guess I found a way to make the tests pass, which is to simply scroll the screen down further. In my case the UI is scroll-able, so this works.

UiScrollable layout = new UiScrollable(new UiSelector().resourceId(...));
layout.scrollForward(5);

I am posting this in case it helps anyone in a similar situation. It does not actually answer my question though, so I am not accepting this.

Upvotes: 0

Related Questions