Skip to content

Commit

Permalink
Merge pull request #449 from YeungHoiChiu/feat-android-uitest-dragand…
Browse files Browse the repository at this point in the history
…drop

feat: `Android`自动化测试步骤中新增拖拽操作以及拖拽动作拆分的三个独立事件操作
  • Loading branch information
ZhouYixun authored Aug 8, 2024
2 parents c742d50 + cfc9967 commit 3ec07d9
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 1 deletion.
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 @@ -751,6 +751,66 @@ public void swipe(HandleContext handleContext, String des, String selector, Stri
}
}

public void dragByPoint(HandleContext handleContext, String des1, String xy1, String des2, String xy2) {
xy1 = TextHandler.replaceTrans(xy1, globalParams);
xy2 = TextHandler.replaceTrans(xy2, globalParams);
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 Down Expand Up @@ -2488,6 +2548,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"), step.getString("text"));
case "motionEventByPoint" ->
motionEventByPoint(handleContext, eleList.getJSONObject(0).getString("eleName"),
eleList.getJSONObject(0).getString("eleValue"),
step.getString("text"));
case "tap" ->
tap(handleContext, eleList.getJSONObject(0).getString("eleName"), eleList.getJSONObject(0).getString("eleValue"));
case "longPressPoint" ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,85 @@ public static void swipe(IDevice iDevice, int x1, int y1, int x2, int y2, int sw
}
}

public static void drag(IDevice iDevice, int x1, int y1, int x2, int y2) throws SonicRespException {
drag(iDevice, x1, y1, x2, y2, DEFAULT_SWIPE_DURATION);
}

public static void drag(IDevice iDevice, int x1, int y1, int x2, int y2, int swipeDuration) throws SonicRespException {
switch (getTouchMode(iDevice)) {
case ADB ->
AndroidDeviceBridgeTool.executeCommand(iDevice, String.format("input draganddrop %d %d %d %d %d", x1, y1, x2, y2, swipeDuration));
case APPIUM_UIAUTOMATOR2_SERVER -> {
AndroidStepHandler curStepHandler = HandlerMap.getAndroidMap().get(iDevice.getSerialNumber());
if (curStepHandler != null && curStepHandler.getAndroidDriver() != null) {
curStepHandler.getAndroidDriver().drag(x1, y1, x2, y2, swipeDuration, null, null);
}
}
case SONIC_APK -> {
// 原本这段代码是在`swipe(IDevice iDevice, int x1, int y1, int x2, int y2, int swipeDuration)`中的
// 但是swipe的效果应该是在屏幕滑动,而不是在第一个坐标按下时存在延迟,所以放在drag方法里面更加符合
int[] re1 = transferWithRotation(iDevice, x1, y1);
int[] re2 = transferWithRotation(iDevice, x2, y2);
writeToOutputStream(iDevice, String.format("down %d %d\n", re1[0], re1[1]));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
long startTime = System.currentTimeMillis();
while (true) {
// 当前时间
long currentTime = System.currentTimeMillis();
// 计算时间进度
float timeProgress = (currentTime - startTime) / (float) swipeDuration;
if (timeProgress >= 1.0f) {
// 已经过渡到结束值,停止过渡
writeToOutputStream(iDevice, String.format("move %d %d\n", re2[0], re2[1]));
break;
}
BiFunction<Integer, Integer, Integer> transitionX = (start, end) ->
(int) (start + (end - start) * timeProgress);
BiFunction<Integer, Integer, Integer> transitionY = (start, end) ->
(int) (start + (end - start) * timeProgress);

int currentX = transitionX.apply(re1[0], re2[0]);
int currentY = transitionY.apply(re1[1], re2[1]);
// 使用当前坐标进行操作
writeToOutputStream(iDevice, String.format("move %d %d\n", currentX, currentY));
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
writeToOutputStream(iDevice, "up\n");
}

}
}

public static void motionEvent(IDevice iDevice, String motionEventType, int x1, int y1) throws SonicRespException {
switch (getTouchMode(iDevice)) {
case ADB ->
AndroidDeviceBridgeTool.executeCommand(iDevice, String.format("input motionevent %s %d %d", motionEventType, x1, y1));
case SONIC_APK -> {
int[] re1 = transferWithRotation(iDevice, x1, y1);
writeToOutputStream(iDevice, String.format("%s %d %d\n", motionEventType.toLowerCase(), re1[0], re1[1]));
}
case APPIUM_UIAUTOMATOR2_SERVER -> {
AndroidStepHandler curStepHandler = HandlerMap.getAndroidMap().get(iDevice.getSerialNumber());
if (curStepHandler != null && curStepHandler.getAndroidDriver() != null) {
curStepHandler.getAndroidDriver().touchAction(motionEventType.toLowerCase(), x1, y1);
}
}
}
}

private static int[] transferWithRotation(IDevice iDevice, int x, int y) {
Integer directionStatus = AndroidDeviceManagerMap.getRotationMap().get(iDevice.getSerialNumber());
if (directionStatus == null) {
Expand Down

0 comments on commit 3ec07d9

Please sign in to comment.