如何使用Android uiautomator填写密码EditText?

8

UI Automator是否能够选择密码EditText?我可以根据它们的android:hint属性找到其他EditText视图,但是uiautomatorviewer将所有密码字段显示为NAF。我尝试设置密码字段内容描述符,但也没有起作用。

如果不可能,那么如何设置超时时间让测试人员手动输入密码?

3个回答

9

我在使用API v16时也遇到了同样的问题。今天我尝试使用v17(Android 4.2)运行我的脚本,它马上就运行成功了。看来第一个版本的uiautomator存在一些重大的bug。

这是我的代码:

// click the admin button
new UiObject(new UiSelector().text("admin")).click();
// set pwd text
new UiObject(new UiSelector().description("pwdEditText")).setText("admin");
// click login button
new UiObject(new UiSelector().description("loginButton")).click();

1
尝试了几种方法,但在v16中没有得到任何结果。因此答案是改为v17。 - Michael

0
有时候,您的视图可能没有资源 ID,例如您需要在 WebView 中呈现的网页中编程输入文本字段的情况。即:
// Fetch the EditText within the iOrder Webpage.
final UiObject lUiObject = UiDevice.getInstance(getInstrumentation()).findObject(new UiSelector().className(EditText.class).textContains("Enter Loyalty Code"));

在这种情况下,我们需要使用UiSelector类来动态搜索EditText;但是,您会发现返回的Matcher<View>onView(with(...))方法不兼容。
当使用UiSelector时,您可以利用UiDevice引用,通过以下方法编程方式模拟按键:
/* Declare the KeyCodeMap. */
private static final KeyCharacterMap MAP_KEYCODE = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);

/** Simulates typing within a UiObject. The typed text is appended to the Object. */
private final void type(final UiObject pUiObject, final String pString, final boolean pIsSimulateTyping, final boolean pIsClearField) throws Exception {
    // Fetch the Instrumentation.
    final Instrumentation lInstrumentation = getInstrumentation();
    // Fetch the UiDevice.
    final UiDevice        lUiDevice        = UiDevice.getInstance(lInstrumentation);
    // Are we clearing the Field beforehand?
    if(pIsClearField) {
        // Reset the Field Text.
        pUiObject.setText("");
    }
    // Are we going to simulate mechanical typing?
    if(pIsSimulateTyping) {
        // Click the Field. (Implicitly open Android's Soft Keyboard.)
        pUiObject.click();
        // Fetch the KeyEvents.
        final KeyEvent[] lKeyEvents = SignUpTest.MAP_KEYCODE.getEvents(pString.toCharArray());
        // Delay.
        lInstrumentation.waitForIdleSync();
        // Iterate the KeyEvents.
        for(final KeyEvent lKeyEvent : lKeyEvents) {
            // Is the KeyEvent a Release. (The KeyEvents contain both down and up events, whereas `pressKeyCode` encapsulates both down and up. This conditional statement essentially decimates the array.)
            if(lKeyEvent.getAction() == KeyEvent.ACTION_UP) {
                // Press the KeyEvent's corresponding KeyCode (Take account for special characters).
                lUiDevice.pressKeyCode(lKeyEvent.getKeyCode(), lKeyEvent.isShiftPressed() ? KeyEvent.META_SHIFT_ON : 0);
                // Delay.
                lInstrumentation.waitForIdleSync();
            }
        }
        // Close the keyboard.
        lUiDevice.pressBack();
    }
    else {
        // Write the String.
        pUiObject.setText(pUiObject.getText() + pString);
    }
    // Delay.
    lInstrumentation.waitForIdleSync();
}

-1

我只是通过id找到它们:

onView(withId(R.id.input_password)).perform(typeText("password"));

我看到UI Automator Viewer现在也显示资源ID属性,如果您没有访问代码的权限,这将非常有用。


就我所知,对于 WebView 来说那样行不通。 - Brill Pappin
问题不是关于WebView的,那涉及到一个比较复杂的问题。 - Dan Neacșu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接