How to fill in EditText password using android uiautomator?

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

If this is not possible, how to set a timeout for the tester to manually enter the password?

+8
android ui-automation android-uiautomator uiautomator android-testing
source share
3 answers

I had the same issue with v16 API. Today I tried my script with v17 (Android 4.2) and it worked like a charm. It seems that the first version of uiautomator contains some basic 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(); 
+7
source share

I just find them by id:

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

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

0
source share

Sometimes your View will not have a ResourceId , for example, when you need to programmatically enter a text box inside a web page made 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 EditText ; however, you will find that the returned Matcher<View> not compatible with the onView(with(...)) methods.

When using UiSelector you can use the UiDevice link for software fake keystrokes 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 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 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(); } 
0
source share

All Articles