Skip to content

Commit

Permalink
Bootstrap clear fallback strategy by sending bunch of deletes
Browse files Browse the repository at this point in the history
  • Loading branch information
moizjv committed Aug 7, 2014
1 parent 2d64899 commit 70802ec
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.android.uiautomator.core.UiDevice;
import io.appium.android.bootstrap.Logger;

import android.view.InputEvent;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

Expand All @@ -19,11 +19,12 @@ private static Field enableField(final Class<?> clazz, final String field)
}

private Object controller = null;
private Object bridge = null;

public ReflectionUtils() throws IllegalArgumentException,
IllegalAccessException, SecurityException, NoSuchFieldException {
final UiDevice device = UiDevice.getInstance();
final Object bridge = enableField(device.getClass(), "mUiAutomationBridge")
bridge = enableField(device.getClass(), "mUiAutomationBridge")
.get(device);
if (API_18) {
controller = enableField(bridge.getClass().getSuperclass(),
Expand All @@ -43,11 +44,23 @@ public Object getController() throws IllegalArgumentException,
return controller;
}

public Object getBridge() {
return bridge;
}

public Method getControllerMethod(final String name, final Class<?>... parameterTypes)
throws NoSuchMethodException, SecurityException {
return getMethod(controller.getClass(), name, parameterTypes);
}

public Method getMethodInjectInputEvent() throws NoSuchMethodException, SecurityException {
Class bridgeClass = bridge.getClass();
if (API_18) {
bridgeClass = bridgeClass.getSuperclass();
}
return getMethod(bridgeClass, "injectInputEvent", InputEvent.class, boolean.class);
}

public Method getMethod(final Class clazz, String name, final Class<?>... parameterTypes)
throws NoSuchMethodException, SecurityException {
Logger.debug("Finding methods on class: " + clazz);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import android.graphics.Rect;
import com.android.uiautomator.common.ReflectionUtils;
import com.android.uiautomator.core.UiObject;
import android.view.InputDevice;
import android.view.KeyCharacterMap;
import com.android.uiautomator.core.UiSelector;


Expand Down Expand Up @@ -54,19 +56,45 @@ public AndroidCommandResult execute(final AndroidCommand command)
// delete it
final Method sendKey = utils.getControllerMethod("sendKey", int.class,int.class);
sendKey.invoke(utils.getController(), KeyEvent.KEYCODE_DEL, 0);
if (el.getText().isEmpty()) {
return getSuccessResult(true);
}
// If above strategy does not work then sending bunch of delete keys after clicking on element.
Logger.debug("Clearing text not successful using selectAllDelete now trying to send delete keys.");
String tempTextHolder = "";
final Object bridgeObject = utils.getBridge();
final Method injectInputEvent = utils.getMethodInjectInputEvent();
// Preventing infinite while loop.
while (!el.getText().isEmpty() && !tempTextHolder.equalsIgnoreCase(el.getText())) {
tempTextHolder = el.getText();
// Trying send delete keys after clicking in text box.
el.click();
// Sending 25 delete keys asynchronously
final long eventTime = SystemClock.uptimeMillis();
KeyEvent deleteEvent = new KeyEvent(eventTime, eventTime, KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_DEL, 0, 0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0,
InputDevice.SOURCE_KEYBOARD);
for (int count = 0; count < 25; count++) {
injectInputEvent.invoke(bridgeObject, deleteEvent, false);
}
}
// If still text exist falling back on UIautomator clearText.
if (!el.getText().isEmpty()) {
Logger.debug("Clearing text not successful falling back to UiAutomator method clear");
el.clearText();
}
return getSuccessResult(el.getText().isEmpty());
// If clear text is still unsuccessful throwing error back
if (!el.getText().isEmpty()) {
return getErrorResult("Clear text not successful.");
}
return getSuccessResult(true);
} catch (final UiObjectNotFoundException e) {
return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT,
e.getMessage());
} catch (final Exception e) { // handle NullPointerException
return getErrorResult("Unknown error clearing text");
}
}
return getErrorResult("Unknown error");
}
return getErrorResult("Unknown error");
}
}

0 comments on commit 70802ec

Please sign in to comment.