【问题标题】:Suppress keyboard after setting text with Android uiautomator使用 Android uiautomator 设置文本后抑制键盘
【发布时间】:2022-03-06 18:56:18
【问题描述】:

使用适用于 Android 的 uiautomator 我可以在文本字段中设置文本,但无法关闭键盘。在某些手机处于横向模式时,键盘会占据整个屏幕,必须点击“完成”才能退出该视图。如果我可以抑制键盘,那么我可以在横向和纵向上运行 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();

提前致谢。

【问题讨论】:

  • 你的问题是什么?

标签: android android-uiautomator


【解决方案1】:

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

最佳方案是:

if(isKeyboardOpened()){
    UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()).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;
}

【讨论】:

  • 我正在尝试完成这项工作,但它无法解析符号 Constants。通过 Alt+Enter 快速修复 import SyncStateContract.Constants 不包含 uiAuto 字段。对此有何想法?
  • 好的,我找到了。我需要通过检测获得 UiAutomation 对象:UiAutomation uiautomation = getInstrumentation().getUiAutomation();
  • 常量是我的错(我从一个项目中复制粘贴而忘记更改那部分,抱歉)。我会编辑回复。
  • 这并不总是有效。当此函数返回 false 时,键盘存在。
  • 在什么情况下?
【解决方案2】:

看起来非常错误,但它完成了工作。

public static final int KEYBOARD_WAIT_TIME = 111;

Espresso.closeSoftKeyboard();
sleep(AutomatedTestConfig.KEYBOARD_WAIT_TIME);

【讨论】:

    【解决方案3】:

    通常单击返回键会关闭键盘。

    getUiDevice().pressBack();
    

    【讨论】:

    • 这适用于应用程序处于纵向模式且键盘不需要“完成”来点击的情况。所以我只为风景做了一个特例。
    • 有什么智能解决方案吗? IE。一种涉及检查键盘是否显示,是否阻碍点击按钮等。
    【解决方案4】:

    我使用了您的代码,只是在插入文本的末尾添加了 \n。那个模拟 'enter',但是键盘仍然出现,所以你需要 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();
    

    【讨论】:

    • 嗨,这是一个相当古老的问题,目前还不清楚您是直接回答问题还是在现有答案中添加更多内容。如果您认为这是一种普遍感兴趣的技术,可以在这里提出一个新问题并自己回答。
    【解决方案5】:

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

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

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

    【讨论】:

    • 我试过了,结果总是假的,无论是打开还是关闭键盘。你知道这是为什么吗?
    【解决方案6】:

    尝试DummyIME 并使用-e disable_ime true 选项运行uiautomator 工具。 DummyIME 驻留在Android git repository

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-26
      • 2015-07-29
      • 2011-04-24
      相关资源
      最近更新 更多