5

使用 uiautomator for Android 我可以在文本字段中设置文本,但无法关闭键盘。在某些手机处于横向模式时,键盘会占据整个屏幕,必须点击“完成”才能退出该视图。如果我可以抑制键盘,那么我可以在横向和纵向中运行 uiautomator 而不会出现问题。

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

提前致谢。

4

6 回答 6

6

这是一个相当古老的问题,但使用 UiAutomator 2.0 可以正确和完整地回答这个问题,因此就在这里。

最佳方案是:

if(isKeyboardOpened()){
    UiDevice.pressBack();
}

但到目前为止的问题是如何实现isKeyboardOpened()。

由于 UiAutomator 2.0 基于仪表,因此我们可以访问 UiAutomation,我们可以验证屏幕上是否存在任何输入窗口:

boolean isKeyboardOpened(){
    for(AccessibilityWindowInfo window: InstrumentationRegistry.getInstrumentation().getUiAutomation().getWindows()){
        if(window.getType()==AccessibilityWindowInfo.TYPE_INPUT_METHOD){
            return true;
        }
    }
    return false;
}
于 2016-06-08T16:30:28.290 回答
3

似乎非常错误,但它完成了工作。

public static final int KEYBOARD_WAIT_TIME = 111;

Espresso.closeSoftKeyboard();
sleep(AutomatedTestConfig.KEYBOARD_WAIT_TIME);
于 2015-09-02T20:08:30.153 回答
2

通常单击后退键将关闭键盘。

getUiDevice().pressBack();
于 2013-07-16T06:14:03.597 回答
1

我使用了您的代码,只是在插入文本的末尾添加了 \n 。那个模拟“输入”,但键盘仍然出现,所以你需要 pressBack() 来关闭 keyb。

new UiObject(new UiSelector()
   .className("android.widget.EditText")
   .instance(0))
   .setText("sample text\n");
getUiDevice().pressBack();

有更优雅的解决方案:

new UiObject(new UiSelector()
   .className("android.widget.EditText")
   .instance(0))
   .setText("sample text");
getUiDevice().pressEnter();
于 2015-01-21T10:31:20.977 回答
0

经过大量工作后,我最终找到了执行此操作的方法。getUIDevice().pressBack()问题是如果没有显示软键盘,调用可能会中断测试。

InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isAcceptingText()) {
    getUIDevice().pressBack();
}

只有在显示键盘时才会按下。

于 2016-01-21T12:53:57.303 回答
-1

尝试使用选项 DummyIME运行该uiautomator工具。驻留在Android git 存储库中。-e disable_ime trueDummyIME

  1. 克隆源代码DummyIME

    git clone https://android.googlesource.com/platform/frameworks/testing
    
  2. 构建和安装DummyIME(您可以更改android-18):

    cd testing/uiautomator/utils/DummyIME
    android update project -p . -t android-18
    ant clean debug install
    
  3. -e disable_ime true使用带有选项的 uiautomator 框架运行您的测试。

    adb shell uiautomator runtest <JARS> -e disable_ime true -c <CLASSES> 
    

请注意,您必须恢复测试设备中默认 IME 的设置,因为它会DummyIME在运行测试后自动更改为。

于 2013-10-28T14:14:39.720 回答