diff --git a/pom.xml b/pom.xml index 24d6a54b..24222dca 100644 --- a/pom.xml +++ b/pom.xml @@ -78,7 +78,7 @@ io.github.soniccloudorg sonic-driver-core - 1.1.29 + 1.1.30 net.coobird diff --git a/src/main/java/org/cloud/sonic/agent/tests/handlers/AndroidStepHandler.java b/src/main/java/org/cloud/sonic/agent/tests/handlers/AndroidStepHandler.java index a76e9ab2..cc5a7fdd 100755 --- a/src/main/java/org/cloud/sonic/agent/tests/handlers/AndroidStepHandler.java +++ b/src/main/java/org/cloud/sonic/agent/tests/handlers/AndroidStepHandler.java @@ -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"); @@ -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" -> diff --git a/src/main/java/org/cloud/sonic/agent/tests/handlers/AndroidTouchHandler.java b/src/main/java/org/cloud/sonic/agent/tests/handlers/AndroidTouchHandler.java index c67297c2..2ea0002d 100755 --- a/src/main/java/org/cloud/sonic/agent/tests/handlers/AndroidTouchHandler.java +++ b/src/main/java/org/cloud/sonic/agent/tests/handlers/AndroidTouchHandler.java @@ -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 transitionX = (start, end) -> + (int) (start + (end - start) * timeProgress); + BiFunction 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) {