가끔 View
에는 WebView
안에 렌더링 된 웹 페이지 내 텍스트 입력란에 프로그래밍 방식으로 입력해야하는 경우가 있습니다 (예 :). 즉, 이러한 경우
// Fetch the EditText within the iOrder Webpage.
final UiObject lUiObject = UiDevice.getInstance(getInstrumentation()).findObject(new UiSelector().className(EditText.class).textContains("Enter Loyalty Code"));
, 우리는 동적으로 EditText
를 검색 할 UiSelector
클래스를 사용할 필요가; 그러나 반환 된 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();
}
감사합니다. 몇 가지 방법을 시도해도 v16에서는 아무 것도 얻지 못했습니다. 대답은 v17로 변경되었습니다. – Michael