Home » Android » ui automation – How to fill in password EditText with Android uiautomator?

ui automation – How to fill in password EditText with Android uiautomator?

Posted by: admin June 16, 2020 Leave a comment

Questions:

Is it possible for the uiautomator to select a password EditText? I have no problem finding other EditText views by their android:hint property, but the uiautomatorviewer shows all password fields as NAF. I tried setting the password field content description and that didn’t work either.

If it’s not possible, how do you set a timeout for a tester to manually enter a password?

How to&Answers:

I had the same problem with API v16.
Today I tried my script with v17 (Android 4.2) and it worked like a charm.
It seems that the first version of uiautomator has some major bugs.

Here is my code:

// 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();

Answer:

Sometimes, your Views won’t have a ResourceId, such as the case where you need to programmatically type into a text field within a webpage rendered inside a WebView. i.e.

// Fetch the EditText within the iOrder Webpage.
final UiObject lUiObject = UiDevice.getInstance(getInstrumentation()).findObject(new UiSelector().className(EditText.class).textContains("Enter Loyalty Code"));

In such cases, we need to use the UiSelector class to dynamically search for the EditText; however, you’ll find that the returned Matcher<View> isn’t compatible with the onView(with(...)) methods.

When using the UiSelector, you can take advantage of a UiDevice reference to programmatically fake key presses using the approach below:

/* 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();
}

Answer:

I just find them by id:

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

I see that the UI Automator Viewer now also shows the resource-id property, which would be useful if you don’t have access to the code.