如何通过uiautomator设置密码字段的文本

时间:2017-02-03 14:55:28

标签: android android-testing android-uiautomator

我无法通过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"

1 个答案:

答案 0 :(得分:2)

如果你运行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())

将是一个接一个