【问题标题】:How to fill in password EditText with Android uiautomator?如何使用 Android uiautomator 填写密码 EditText?
【发布时间】:2013-02-19 01:31:59
【问题描述】:

uiautomator 是否可以选择密码 EditText?我通过他们的 android:hint 属性找到其他 EditText 视图没有问题,但是 uiautomatorviewer 将所有密码字段显示为 NAF。我尝试设置密码字段内容描述,但也没有用。

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

【问题讨论】:

    标签: android ui-automation android-testing android-uiautomator


    【解决方案1】:

    我在使用 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();
    

    【讨论】:

    • 谢谢。尝试几种方法,但在 v16 中一无所获。所以答案是改为 v17。
    【解决方案2】:

    有时,您的Views 没有ResourceId,例如您需要以编程方式在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();
    }
    

    【讨论】:

      【解决方案3】:

      我只是通过 id 找到它们:

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

      我看到 UI Automator Viewer 现在还显示资源 ID 属性,如果您无权访问代码,这将很有用。

      【讨论】:

      • 据我所知,这对 WebView 不起作用。
      • 好吧,问题不在于 WebView,这是一个更复杂的问题
      猜你喜欢
      • 2013-06-30
      • 1970-01-01
      • 1970-01-01
      • 2021-06-17
      • 1970-01-01
      • 2013-06-21
      • 1970-01-01
      • 2012-03-07
      • 2011-07-20
      相关资源
      最近更新 更多