Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suppress keyboard after setting text with Android uiautomator

Using uiautomator for Android I am able to set text in a text field but not able to then close the keyboard. With some phones when in lanscape mode the keyboard takes up the whole screen and 'Done' must be tapped to get out of that view. If I can suppress the keyboard then I can run uiautomator in both landscape and portrait without issue.

new UiObject(new UiSelector().text("Enter Text")).click();
new UiObject(new UiSelector().className("android.widget.EditText").instance(0)).setText("sample text");

// This is where I need to suppress the keyboard to view the app instead of just the keyboard itself.

new UiObject(new UiSelector().text("Submit")).click();

Thanks in advance.

like image 567
Daniel Avatar asked Jun 20 '13 20:06

Daniel


3 Answers

This is quite an old question but with UiAutomator 2.0 it is possible to correctly and completely answer the question and thus here it is.

The optimal would be:

if(isKeyboardOpened()){
    UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()).pressBack();
}

But so far the problem was how to implement isKeyboardOpened().

As UiAutomator 2.0 is based on instrumentation, and thus we have access to UiAutomation, we can verify if there are any input windows present on the screen:

boolean isKeyboardOpened(){
    for(AccessibilityWindowInfo window: InstrumentationRegistry.getInstrumentation().getUiAutomation().getWindows()){
        if(window.getType()==AccessibilityWindowInfo.TYPE_INPUT_METHOD){
            return true;
        }
    }
    return false;
}
like image 142
Inês Avatar answered Sep 20 '22 13:09

Inês


Seems very wrong, but it gets the job done.

public static final int KEYBOARD_WAIT_TIME = 111;

Espresso.closeSoftKeyboard();
sleep(AutomatedTestConfig.KEYBOARD_WAIT_TIME);
like image 31
cant_login Avatar answered Sep 19 '22 13:09

cant_login


Normally clicking the Back-key will dismiss the keyboard.

getUiDevice().pressBack();
like image 20
Anders Avatar answered Sep 23 '22 13:09

Anders