我无法通过uiautomator为密码字段设置文本:

UiObject eaPassword = uiDevice.findObject(new UiSelector().textContains("Password"));
assertTrue(eaPassword.waitForExists(35_000));
eaPassword.click(); // optional
eaPassword.setText("1234"); // return true


它能够找到对象本身。

如果执行eaPassword.getText();,它将返回"Password"

最佳答案

如果您运行eaPassword.click(),则屏幕将发生变化(键盘弹出),
因此您无法再次找到eaPassword(Uiobject)

setText()此函数有两个行为
1.长按EditView
2.输入文字

UiObject eaPassword = uiDevice.findObject(new UiSelector().textContains("Password"));
assertTrue(eaPassword.waitForExists(35_000));
eaPassword.setText("1234");


或尝试其他方法

UiObject eaPassword = uiDevice.findObject(new UiSelector().textContains("Password"));
assertTrue(eaPassword.waitForExists(35_000));
eaPassword.click();
setStrings("test");

 public void setStrings(String text) {
        for (int i = 0; i < text.length(); i++) {
            char c = text.charAt(i);
            if (c >= 48 && c <= 57) // 0~9
                uiDevice.pressKeyCode(c - 41);
            else if (c >= 65 && c <= 90) // A~Z
                uiDevice.pressKeyCode(c - 36, 1);
            else if (c >= 97 && c < 122) // a~z
                uiDevice.pressKeyCode(c - 68);
            else if (c == 42) // *
                uiDevice.pressKeyCode(KEYCODE_STAR);
            else if (c == 35) // #
                uiDevice.pressKeyCode(KEYCODE_POUND);
            else if (c == 46) // .
                uiDevice.pressKeyCode(KEYCODE_PERIOD);
            else if (c == 47) // /
                uiDevice.pressKeyCode(KEYCODE_SLASH);
            else if (c == 58)  // :
                uiDevice.pressKeyCode(KEYCODE_SEMICOLON);
        }
}


输入单词将是一个接一个的(不使用setText())

关于android - 如何通过uiautomator设置密码字段的文本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42027040/

10-09 03:01