@Overridepublic AndroidCommandResult execute(final AndroidCommand command)
throws JSONException {
// DragArguments is created on each execute which prevents leaking state// across executions.final DragArguments dragArgs = new DragArguments(command);
if (command.isElementCommand()) {
return dragElement(dragArgs);
} else {
return drag(dragArgs);
}
}
private AndroidCommandResult dragElement(final DragArguments dragArgs) {
Point absEndPos = new Point();
if (dragArgs.destEl == null) {
try {
//这里判断如果无destEl时,那么就需要计算结束坐标相对于设备的坐标位置。这里的计算方法与swipe的转换是一样的
absEndPos = PositionHelper.getDeviceAbsPos(dragArgs.end);
} catch (final InvalidCoordinatesException e) {
return getErrorResult(e.getMessage());
} catch (final UiObjectNotFoundException e) {
return getErrorResult(e.getMessage());
}
Logger.debug("Dragging the element with id " + dragArgs.el.getId()
+ " to " + absEndPos.toString() + " with steps: "
+ dragArgs.steps.toString());
try {
//调用UiAutomator中的drag方法,这里需要注意api要大于18,如果小于则拖拽不成功finalboolean rv = dragArgs.el.dragTo(absEndPos.x.intValue(),
absEndPos.y.intValue(), dragArgs.steps);
if (!rv) {
return getErrorResult("Drag did not complete successfully");
} else {
return getSuccessResult(rv);
}
} catch (final UiObjectNotFoundException e) {
return getErrorResult("Drag did not complete successfully"
+ e.getMessage());
}
} else {
//如果destEl不为空时,则调用Uiautomator另一个方法 boolean dragTo(UiObject destObj, int steps)
Logger.debug("Dragging the element with id " + dragArgs.el.getId()
+ " to destination element with id " + dragArgs.destEl.getId()
+ " with steps: " + dragArgs.steps);
try {
finalboolean rv = dragArgs.el.dragTo(dragArgs.destEl.getUiObject(),
dragArgs.steps);
if (!rv) {
return getErrorResult("Drag did not complete successfully");
} else {
return getSuccessResult(rv);
}
} catch (final UiObjectNotFoundException e) {
return getErrorResult("Drag did not complete successfully"
+ e.getMessage());
}
}
}
以上是针对于元素的操作
如果是针对非元素的Drag
private AndroidCommandResult drag(final DragArguments dragArgs) {
Point absStartPos = new Point();
Point absEndPos = new Point();
final UiDevice device = UiDevice.getInstance();
try {
//这里分别将起始以及结束的坐标转换成相应的设备的坐标
absStartPos = PositionHelper.getDeviceAbsPos(dragArgs.start);
absEndPos = PositionHelper.getDeviceAbsPos(dragArgs.end);
} catch (final InvalidCoordinatesException e) {
return getErrorResult(e.getMessage());
} catch (final UiObjectNotFoundException e) {
return getErrorResult(e.getMessage());
}
Logger.debug("Dragging from " + absStartPos.toString() + " to "
+ absEndPos.toString() + " with steps: " + dragArgs.steps.toString());
//再来也是一样的调用Uiautomator的drag方法finalboolean rv = device.drag(absStartPos.x.intValue(),
absStartPos.y.intValue(), absEndPos.x.intValue(),
absEndPos.y.intValue(), dragArgs.steps);
if (!rv) {
return getErrorResult("Drag did not complete successfully");
}
return getSuccessResult(rv);
}
但是问题来了,appium的api中貌似只提供了一个方法
# convenience method added to Appium (NOT Selenium 3)defdrag_and_drop(self, origin_el, destination_el):"""Drag the origin element to the destination element
:Args:
- originEl - the element to drag
- destinationEl - the element to drag to
"""
action = TouchAction(self)
action.long_press(origin_el).move_to(destination_el).release().perform()
return self
defdrag_and_drop(self, origin_el, destination_el):"""Drag the origin element to the destination element
:Args:
- originEl - the element to drag
- destinationEl - the element to drag to
"""
action = TouchAction(self)
action.long_press(origin_el).move_to(destination_el).release().perform()
return self