TouchLongClick類繼承于TouchEvent,而TouchEvent繼承于CommandHandler.調用TouchEvent的execute的方法中,調用了executeTouchEvent方法,是以我們來看上面的executeTouchEvent就好了,執行長點選事件,在uiautomator裡有UiObject.longClick()方法,但是寫過case的人知道,有時候這個方法達不到我們的需求,但是我們是自己了反射調用TouchDown和TouchUp兩個個方法,而在appium裡幫你解決了,它自己就幫你做到了這一點,如果你傳入到是控件對象,那無可厚非,還是調用UiObject.longClick方法,如果你想根據坐标,時間在點選的話,那麼就調用currectLongClick這個appium給你封裝好的方法。
final ReflectionUtils utils = new ReflectionUtils();
final Method touchDown = utils.getControllerMethod("touchDown", int.class,
int.class);
final Method touchUp = utils.getControllerMethod("touchUp", int.class, int.class);
通過反射得到uiautomator裡的沒有公開的類,進而我們想要的方法touchDown和touchUp.
public ReflectionUtils() throws IllegalArgumentException,
IllegalAccessException, SecurityException, NoSuchFieldException {
final UiDevice device = UiDevice.getInstance();
final Object bridge = enableField(device.getClass(), "mUiAutomationBridge")
.get(device);
if (API_18) {
controller = enableField(bridge.getClass().getSuperclass(),
"mInteractionController").get(bridge);
} else {
controller = enableField(bridge.getClass(), "mInteractionController")
.get(bridge);
}
}
因為uiautomator api的改動,在api18以上的版本中,mInteractionController是存在于UiAutomationBridge的父類中的變量,而在18以下的版本中它是存在于本類中的。是以反射時會有一點點小小點差異,但總的來說都是要獲得InteractionController這個類,因為這個類裡面存在有我們要但touch類但方法。最後我們就能輕松調用滑鼠的TouchUp和TouchDown他們啦。然後再加上時間,長按就實作啦。
TouchUp
package io.appium.android.bootstrap.handler;
import com.android.uiautomator.common.ReflectionUtils;
import com.android.uiautomator.core.UiObjectNotFoundException;
import io.appium.android.bootstrap.Logger;
import java.lang.reflect.Method;
/**
* This handler is used to perform a touchDown event on an element in the
* Android UI.
*
*/
public class TouchDown extends TouchEvent {
@Override
protected boolean executeTouchEvent() throws UiObjectNotFoundException {
printEventDebugLine("TouchDown");
try {
final ReflectionUtils utils = new ReflectionUtils();
int.class);
return (Boolean) touchDown.invoke(utils.getController(), clickX, clickY);
} catch (final Exception e) {
Logger.debug("Problem invoking touchDown: " + e);
return false;
}
有了上面的分析,對TouchUp和TouchDown還有TouchMove的分析就不用再多說了,都是反射的原理