Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 增加adb和uia的拖拽控件(DragAndDrop)操作 #447

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
<dependency>
<groupId>io.github.soniccloudorg</groupId>
<artifactId>sonic-driver-core</artifactId>
<version>1.1.29</version>
<version>1.1.30</version>
</dependency>
<dependency>
<groupId>net.coobird</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import org.springframework.util.CollectionUtils;

import javax.imageio.stream.FileImageOutputStream;
import java.awt.desktop.AboutHandler;
import java.io.File;
import java.util.*;
import java.util.concurrent.Future;
Expand Down Expand Up @@ -427,7 +428,7 @@ public void appAutoGrantPermissions(HandleContext handleContext, String packageN
String getDetailCommandResult = AndroidDeviceBridgeTool.executeCommand(iDevice, dumpsysCommandStr);
List<AndroidPermissionItem> allPermissionItems =
AndroidPermissionExtractor.extractPermissions(getDetailCommandResult,
Arrays.asList("install", "runtime"), true);
Arrays.asList("install", "runtime"), true);
allPermissionItems.stream().filter(permissionItem -> !permissionItem.isGranted())
.forEach(permissionItem -> {
String curPermission = permissionItem.getPermission();
Expand Down Expand Up @@ -721,8 +722,8 @@ public void swipePoint(HandleContext handleContext, String des1, String xy1, Str
double x2 = Double.parseDouble(xy2.substring(0, xy2.indexOf(",")));
double y2 = Double.parseDouble(xy2.substring(xy2.indexOf(",") + 1));
int[] point2 = computedPoint(x2, y2);
handleContext.setStepDes("滑动拖拽" + des1 + "" + des2);
handleContext.setDetail("拖动坐标(" + point1[0] + "," + point1[1] + ")(" + point2[0] + "," + point2[1] + ")");
handleContext.setStepDes("" + des1 + "滑动到" + des2);
handleContext.setDetail("从坐标(" + point1[0] + "," + point1[1] + ")滑动到(" + point2[0] + "," + point2[1] + ")");
try {
AndroidTouchHandler.swipe(iDevice, point1[0], point1[1], point2[0], point2[1]);
} catch (Exception e) {
Expand All @@ -738,14 +739,72 @@ public void swipe(HandleContext handleContext, String des, String selector, Stri
int y1 = webElement.getRect().getY();
int x2 = webElement2.getRect().getX();
int y2 = webElement2.getRect().getY();
handleContext.setStepDes("滑动拖拽" + des + "" + des2);
handleContext.setDetail("拖动坐标(" + x1 + "," + y1 + ")(" + x2 + "," + y2 + ")");
handleContext.setStepDes("" + des + "滑动到" + des2);
handleContext.setDetail("从坐标(" + x1 + "," + y1 + ")滑动到坐标(" + x2 + "," + y2 + ")");
AndroidTouchHandler.swipe(iDevice, x1, y1, x2, y2);
} catch (Exception e) {
handleContext.setE(e);
}
}

public void dragByPoint(HandleContext handleContext, String des1, String xy1, String des2, String xy2) {
double x1 = Double.parseDouble(xy1.substring(0, xy1.indexOf(",")));
double y1 = Double.parseDouble(xy1.substring(xy1.indexOf(",") + 1));
int[] point1 = computedPoint(x1, y1);
double x2 = Double.parseDouble(xy2.substring(0, xy2.indexOf(",")));
double y2 = Double.parseDouble(xy2.substring(xy2.indexOf(",") + 1));
int[] point2 = computedPoint(x2, y2);
handleContext.setStepDes("拖拽" + des1 + "到" + des2);
handleContext.setDetail("拖拽坐标(" + point1[0] + "," + point1[1] + ")到(" + point2[0] + "," + point2[1] + ")");
try {
AndroidTouchHandler.drag(iDevice, point1[0], point1[1], point2[0], point2[1]);
} catch (Exception e) {
handleContext.setE(e);
}
}

public void dragByEle(HandleContext handleContext, String des, String selector, String pathValue, String des2, String selector2, String pathValue2) {
try {
AndroidElement webElement = findEle(selector, pathValue);
AndroidElement webElement2 = findEle(selector2, pathValue2);
int x1 = webElement.getRect().getX();
int y1 = webElement.getRect().getY();
int x2 = webElement2.getRect().getX();
int y2 = webElement2.getRect().getY();
handleContext.setStepDes("拖拽" + des + "到" + des2);
handleContext.setDetail("拖拽坐标(" + x1 + "," + y1 + ")到(" + x2 + "," + y2 + ")");
AndroidTouchHandler.drag(iDevice, x1, y1, x2, y2);
} catch (SonicRespException e) {
handleContext.setE(e);
}
}

public void motionEventByPoint(HandleContext handleContext, String des, String xy, String motionEventType) throws SonicRespException {
double x = Double.parseDouble(xy.substring(0, xy.indexOf(",")));
double y = Double.parseDouble(xy.substring(xy.indexOf(",") + 1));
int[] point = computedPoint(x, y);
handleContext.setStepDes("通过坐标" + des + "触发" + motionEventType + "-Motion事件");
handleContext.setDetail("对坐标" + point[0] + "," + point[1] + String.format("执行Motion事件(%s)", motionEventType));
try {
AndroidTouchHandler.motionEvent(iDevice, motionEventType, point[0], point[1]);
} catch (SonicRespException e) {
handleContext.setE(e);
}
}

public void motionEventByEle(HandleContext handleContext, String des, String selector, String pathValue, String motionEventType) throws SonicRespException {
try {
AndroidElement webElement = findEle(selector, pathValue);
int x = webElement.getRect().getX();
int y = webElement.getRect().getY();
handleContext.setStepDes("通过" + des + "触发" + motionEventType + "-Motion事件");
handleContext.setDetail("对坐标" + x + "," + y + String.format("执行Motion事件(%s)", motionEventType));
AndroidTouchHandler.motionEvent(iDevice, motionEventType, x, y);
} catch (SonicRespException e) {
handleContext.setE(e);
}
}

public void swipeByDefinedDirection(HandleContext handleContext, String slideDirection, int distance) throws Exception {
String size = AndroidDeviceBridgeTool.getScreenSize(iDevice);
String[] winSize = size.split("x");
Expand All @@ -771,7 +830,7 @@ public void swipeByDefinedDirection(HandleContext handleContext, String slideDir
} catch (Exception e) {
handleContext.setE(e);
}
handleContext.setDetail("拖动坐标(" + centerX + "," + centerY + ")(" + centerX + "," + targetY + ")");
handleContext.setDetail("从坐标(" + centerX + "," + centerY + ")滑动到坐标(" + centerX + "," + targetY + ")");
}
case "down" -> {
handleContext.setStepDes("从设备中心位置向下滑动" + distance + "像素");
Expand All @@ -785,7 +844,7 @@ public void swipeByDefinedDirection(HandleContext handleContext, String slideDir
} catch (Exception e) {
handleContext.setE(e);
}
handleContext.setDetail("拖动坐标(" + centerX + "," + centerY + ")(" + centerX + "," + targetY + ")");
handleContext.setDetail("从坐标(" + centerX + "," + centerY + ")滑动到(" + centerX + "," + targetY + ")");
}
case "left" -> {
handleContext.setStepDes("从设备中心位置向左滑动" + distance + "像素");
Expand All @@ -799,7 +858,7 @@ public void swipeByDefinedDirection(HandleContext handleContext, String slideDir
} catch (Exception e) {
handleContext.setE(e);
}
handleContext.setDetail("拖动坐标(" + centerX + "," + centerY + ")(" + targetX + "," + centerY + ")");
handleContext.setDetail("从坐标(" + centerX + "," + centerY + ")滑动到(" + targetX + "," + centerY + ")");
}
case "right" -> {
handleContext.setStepDes("从设备中心位置向右滑动" + distance + "像素");
Expand All @@ -813,7 +872,7 @@ public void swipeByDefinedDirection(HandleContext handleContext, String slideDir
} catch (Exception e) {
handleContext.setE(e);
}
handleContext.setDetail("拖动坐标(" + centerX + "," + centerY + ")(" + targetX + "," + centerY + ")");
handleContext.setDetail("从坐标(" + centerX + "," + centerY + ")滑动到(" + targetX + "," + centerY + ")");
}
default ->
throw new Exception("Sliding in this direction is not supported. Only up/down/left/right are supported!");
Expand Down Expand Up @@ -1767,7 +1826,7 @@ public void getPocoElementAttr(HandleContext handleContext, String des, String s
}

public void obtainPocoElementAttr(HandleContext handleContext, String des, String selector, String pathValue,
String attr, String variable) {
String attr, String variable) {
handleContext.setStepDes("获取控件 " + des + " 属性到变量");
handleContext.setDetail("目标属性:" + attr + ",目标变量:" + variable);
try {
Expand Down Expand Up @@ -2029,10 +2088,12 @@ public AndroidElement findEle(String selector, String pathValue, Integer retryTi
switch (selector) {
case "androidIterator" -> we = androidDriver.findElement(pathValue);
case "id" -> we = androidDriver.findElement(AndroidSelector.Id, pathValue, retryTime);
case "accessibilityId" -> we = androidDriver.findElement(AndroidSelector.ACCESSIBILITY_ID, pathValue, retryTime);
case "accessibilityId" ->
we = androidDriver.findElement(AndroidSelector.ACCESSIBILITY_ID, pathValue, retryTime);
case "xpath" -> we = androidDriver.findElement(AndroidSelector.XPATH, pathValue, retryTime);
case "className" -> we = androidDriver.findElement(AndroidSelector.CLASS_NAME, pathValue, retryTime);
case "androidUIAutomator" -> we = androidDriver.findElement(AndroidSelector.UIAUTOMATOR, pathValue, retryTime);
case "androidUIAutomator" ->
we = androidDriver.findElement(AndroidSelector.UIAUTOMATOR, pathValue, retryTime);
default ->
log.sendStepLog(StepType.ERROR, "查找控件元素失败", "这个控件元素类型: " + selector + " 不存在!!!");
}
Expand Down Expand Up @@ -2424,10 +2485,9 @@ public void runStep(JSONObject stepJSON, HandleContext handleContext) throws Thr
case "getElementAttr" ->
getElementAttr(handleContext, eleList.getJSONObject(0).getString("eleName"), eleList.getJSONObject(0).getString("eleType")
, eleList.getJSONObject(0).getString("eleValue"), step.getString("text"), step.getString("content"));
case "obtainElementAttr" ->
obtainElementAttr(handleContext, eleList.getJSONObject(0).getString("eleName"),
eleList.getJSONObject(0).getString("eleType"), eleList.getJSONObject(0).getString("eleValue"),
step.getString("text"), step.getString("content"));
case "obtainElementAttr" -> obtainElementAttr(handleContext, eleList.getJSONObject(0).getString("eleName"),
eleList.getJSONObject(0).getString("eleType"), eleList.getJSONObject(0).getString("eleValue"),
step.getString("text"), step.getString("content"));
case "logElementAttr" ->
logElementAttr(handleContext, eleList.getJSONObject(0).getString("eleName"), eleList.getJSONObject(0).getString("eleType")
, eleList.getJSONObject(0).getString("eleValue"), step.getString("text"));
Expand All @@ -2440,12 +2500,11 @@ public void runStep(JSONObject stepJSON, HandleContext handleContext) throws Thr
case "isExistEle" ->
isExistEle(handleContext, eleList.getJSONObject(0).getString("eleName"), eleList.getJSONObject(0).getString("eleType")
, eleList.getJSONObject(0).getString("eleValue"), step.getBoolean("content"));
case "scrollToEle" ->
scrollToEle(handleContext, eleList.getJSONObject(0).getString("eleName"),
eleList.getJSONObject(0).getString("eleType"),
eleList.getJSONObject(0).getString("eleValue"),
step.getInteger("content"),
step.getString("text"));
case "scrollToEle" -> scrollToEle(handleContext, eleList.getJSONObject(0).getString("eleName"),
eleList.getJSONObject(0).getString("eleType"),
eleList.getJSONObject(0).getString("eleValue"),
step.getInteger("content"),
step.getString("text"));
case "isExistEleNum" -> isExistEleNum(handleContext, eleList.getJSONObject(0).getString("eleName"),
eleList.getJSONObject(0).getString("eleType"),
eleList.getJSONObject(0).getString("eleValue"),
Expand All @@ -2463,6 +2522,18 @@ public void runStep(JSONObject stepJSON, HandleContext handleContext) throws Thr
case "swipe2" ->
swipe(handleContext, eleList.getJSONObject(0).getString("eleName"), eleList.getJSONObject(0).getString("eleType"), eleList.getJSONObject(0).getString("eleValue")
, eleList.getJSONObject(1).getString("eleName"), eleList.getJSONObject(1).getString("eleType"), eleList.getJSONObject(1).getString("eleValue"));
case "drag" ->
dragByPoint(handleContext, eleList.getJSONObject(0).getString("eleName"), eleList.getJSONObject(0).getString("eleValue")
, eleList.getJSONObject(1).getString("eleName"), eleList.getJSONObject(1).getString("eleValue"));
case "drag2" ->
dragByEle(handleContext, eleList.getJSONObject(0).getString("eleName"), eleList.getJSONObject(0).getString("eleType"), eleList.getJSONObject(0).getString("eleValue")
, eleList.getJSONObject(1).getString("eleName"), eleList.getJSONObject(1).getString("eleType"), eleList.getJSONObject(1).getString("eleValue"));
case "motionEvent" -> motionEventByEle(handleContext, eleList.getJSONObject(0).getString("eleName"),
eleList.getJSONObject(0).getString("eleType"), eleList.getJSONObject(0).getString("eleValue"), eleList.getJSONObject(0).getString("text"));
case "motionEventByPoint" ->
motionEventByPoint(handleContext, eleList.getJSONObject(0).getString("eleName"),
eleList.getJSONObject(0).getString("eleValue"),
eleList.getJSONObject(0).getString("text"));
case "tap" ->
tap(handleContext, eleList.getJSONObject(0).getString("eleName"), eleList.getJSONObject(0).getString("eleValue"));
case "longPressPoint" ->
Expand Down
Loading