bootstrapのマウス操作
4981 ワード
TouchLongClick package io.appium.android.bootstrap.handler;
import android.os.SystemClock;
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 long click elements in the Android UI.
*
*/
public class TouchLongClick extends TouchEvent {
/*
* UiAutomator has a broken longClick, so we'll try to implement it using the
* touchDown / touchUp events.
*/
private boolean correctLongClick(final int x, final int y, final int duration) {
try {
/*
* bridge.getClass() returns ShellUiAutomatorBridge on API 18/19 so use
* the super class.
*/
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);
if ((Boolean) touchDown.invoke(utils.getController(), x, y)) {
SystemClock.sleep(duration);
if ((Boolean) touchUp.invoke(utils.getController(), x, y)) {
return true;
}
}
return false;
} catch (final Exception e) {
Logger.debug("Problem invoking correct long click: " + e);
return false;
}
}
@Override
protected boolean executeTouchEvent() throws UiObjectNotFoundException {
final Object paramDuration = params.get("duration");
int duration = 2000; // two seconds
if (paramDuration != null) {
duration = Integer.parseInt(paramDuration.toString());
}
printEventDebugLine("TouchLongClick", duration);
if (correctLongClick(clickX, clickY, duration)) {
return true;
}
// if correctLongClick failed and we have an element
// then uiautomator's longClick is used as a fallback.
if (isElement) {
Logger.debug("Falling back to broken longClick");
return el.longClick();
}
return false;
}
}
TouchLongClickクラスはTouchEventに継承され、TouchEventはCommandHandlerに継承される.TouchEventを呼び出すexecuteのメソッドでは、executeTouchEventメソッドが呼び出されているので、上のexecuteTouchEventを見てみると良いでしょう.ロングクリックイベントを実行し、uiautomatorにUiobjectがあります.longClick()メソッドですが、caseを書いた人は知っています.このメソッドは私たちのニーズに達しないことがあります.しかし、私たちは自分でTouchDownとTouchUpの2つのメソッドを反射して呼び出しました.appiumで解決してあげました.それは自分であなたを助けました.コントロールオブジェクトに伝わったら、それは間違いありません.Uibjectを呼び出します.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の変更により、api 18以降のバージョンでは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();
final Method touchDown = utils.getControllerMethod("touchDown", int.class,
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の分析は言うまでもなく、反射の原理です.
TouchDown
同類
TouchMove
同類
package io.appium.android.bootstrap.handler;
import android.os.SystemClock;
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 long click elements in the Android UI.
*
*/
public class TouchLongClick extends TouchEvent {
/*
* UiAutomator has a broken longClick, so we'll try to implement it using the
* touchDown / touchUp events.
*/
private boolean correctLongClick(final int x, final int y, final int duration) {
try {
/*
* bridge.getClass() returns ShellUiAutomatorBridge on API 18/19 so use
* the super class.
*/
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);
if ((Boolean) touchDown.invoke(utils.getController(), x, y)) {
SystemClock.sleep(duration);
if ((Boolean) touchUp.invoke(utils.getController(), x, y)) {
return true;
}
}
return false;
} catch (final Exception e) {
Logger.debug("Problem invoking correct long click: " + e);
return false;
}
}
@Override
protected boolean executeTouchEvent() throws UiObjectNotFoundException {
final Object paramDuration = params.get("duration");
int duration = 2000; // two seconds
if (paramDuration != null) {
duration = Integer.parseInt(paramDuration.toString());
}
printEventDebugLine("TouchLongClick", duration);
if (correctLongClick(clickX, clickY, duration)) {
return true;
}
// if correctLongClick failed and we have an element
// then uiautomator's longClick is used as a fallback.
if (isElement) {
Logger.debug("Falling back to broken longClick");
return el.longClick();
}
return false;
}
}
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);
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);
}
}
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();
final Method touchDown = utils.getControllerMethod("touchDown", int.class,
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の分析は言うまでもなく、反射の原理です.
TouchDown
同類
TouchMove
同類
同類