Manchmal haben Ihre View
s keine ResourceId
, z. B. wenn Sie in ein Textfeld innerhalb einer WebView
gerenderten Webseite programmgesteuert eingeben müssen. das heißt
// Fetch the EditText within the iOrder Webpage.
final UiObject lUiObject = UiDevice.getInstance(getInstrumentation()).findObject(new UiSelector().className(EditText.class).textContains("Enter Loyalty Code"));
In solchen Fällen müssen wir die UiSelector
Klasse verwenden dynamisch für die EditText
zu suchen; Sie werden jedoch feststellen, dass die zurückgegebene Matcher<View>
nicht mit den Methoden onView(with(...))
kompatibel ist.
Wenn die UiSelector
verwenden, können Sie die Vorteile einer UiDevice
Bezug auf programmatisch gefälschte Tastendrücken mit dem Ansatz unter:
/* 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();
}
Dank. Versuchen Sie ein paar Methoden, aber nichts in V16. Die Antwort lautet also v17. – Michael