8

E 'possibile per l'uiautomator per selezionare un EditText password? Non ho problemi a trovare altre viste EditText dalla loro proprietà android: hint, ma l'uiautomatorviewer mostra tutti i campi password come NAF. Ho provato a impostare la descrizione del contenuto del campo della password e anche questo non ha funzionato.Come inserire la password EditText con Android uiautomator?

Se non è possibile, come si fa a impostare un timeout per un tester per inserire manualmente una password?

risposta

7

Ho avuto lo stesso problema con API v16. Oggi ho provato il mio script con v17 (Android 4.2) e ha funzionato come un fascino. Sembra che la prima versione di uiautomator ha alcuni bug importanti.

Ecco il mio codice:

// 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

Grazie. Prova alcuni metodi ma non hai ottenuto nulla nella v16. Quindi la risposta è cambiata in v17. – Michael

0

ho solo loro trovano da ID:

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

vedo che l'interfaccia utente Automator Viewer ora mostra anche la proprietà delle risorse-id, che sarebbe utile se non hai accesso al codice.

0

A volte, i tuoi View s non avranno uno ResourceId, come nel caso in cui è necessario digitare a livello di codice in un campo di testo all'interno di una pagina Web resa all'interno di un WebView. vale a dire

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

In questi casi, abbiamo bisogno di usare la classe UiSelector per cercare in modo dinamico per la EditText; tuttavia, troverete che la tornata Matcher<View> non è compatibile con le onView(with(...)) metodi.

Quando si utilizza il UiSelector, è possibile usufruire di un riferimento UiDevice a livello di codice falso pressione dei tasti che utilizzano il metodo di seguito:

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