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.
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;
}
Seems very wrong, but it gets the job done.
public static final int KEYBOARD_WAIT_TIME = 111;
Espresso.closeSoftKeyboard();
sleep(AutomatedTestConfig.KEYBOARD_WAIT_TIME);
Normally clicking the Back-key will dismiss the keyboard.
getUiDevice().pressBack();
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