diff --git a/src/main/java/org/cloud/sonic/driver/android/AndroidDriver.java b/src/main/java/org/cloud/sonic/driver/android/AndroidDriver.java index 31a9e6b..2da8a17 100644 --- a/src/main/java/org/cloud/sonic/driver/android/AndroidDriver.java +++ b/src/main/java/org/cloud/sonic/driver/android/AndroidDriver.java @@ -455,4 +455,38 @@ public void swipe(int fromX, int fromY, int toX, int toY) throws SonicRespExcept public void swipe(int fromX, int fromY, int toX, int toY, Integer duration) throws SonicRespException { uiaClient.swipe(fromX, fromY, toX, toY, duration); } + + /** + * Performs a long press followed by an immediate drag to a location and releases. + * fromX(Y) are required if elementId is not provided, so do toX(Y) if destElId is not provided. + * + * @param fromX Starting X coordinate + * @param fromY Starting Y coordinate + * @param toX Ending X coordinate + * @param toY Ending Y coordinate + * @param duration Duration of the action in milliseconds + * @param elementId ID of the original element (optional), for specific interaction scenarios + * @param destElId ID of the target element (optional), for specific interaction scenarios + * @throws SonicRespException Throws when the operation fails + */ + public void drag(int fromX, int fromY, int toX, int toY, Integer duration, String elementId, String destElId) throws SonicRespException { + uiaClient.drag(fromX, fromY, toX, toY, duration, elementId, destElId); + } + + /** + * Performs a touch action. + * This method delegates to the UIA client's touchAction method to simulate + * a touch event at a specified position on the screen with a given action type. + * + * @param methodType The type of touch action, enumerate in (down, up, move). + * Specific supported types depend on the UIA client's implementation. + * @param x The X coordinate of the touch point. + * @param y The Y coordinate of the touch point. + * @throws SonicRespException If an error occurs while performing the touch action, + * this exception is thrown. + */ + public void touchAction(String methodType, int x, int y) throws SonicRespException { + uiaClient.touchAction(methodType, x, y); + } + } diff --git a/src/main/java/org/cloud/sonic/driver/android/service/UiaClient.java b/src/main/java/org/cloud/sonic/driver/android/service/UiaClient.java index 10395f3..79841ca 100644 --- a/src/main/java/org/cloud/sonic/driver/android/service/UiaClient.java +++ b/src/main/java/org/cloud/sonic/driver/android/service/UiaClient.java @@ -88,4 +88,10 @@ public interface UiaClient { void longPress(double x, double y, double ms) throws SonicRespException; void swipe(int fromX, int fromY, int toX, int toY, Integer duration) throws SonicRespException; + + void drag(int fromX, int fromY, int toX, int toY, Integer duration, String elementId, String destElId) throws SonicRespException; + + // touch handler. + void touchAction(String methodType, int x, int y) throws SonicRespException; + } diff --git a/src/main/java/org/cloud/sonic/driver/android/service/impl/UiaClientImpl.java b/src/main/java/org/cloud/sonic/driver/android/service/impl/UiaClientImpl.java index 41d24e8..9cc976a 100644 --- a/src/main/java/org/cloud/sonic/driver/android/service/impl/UiaClientImpl.java +++ b/src/main/java/org/cloud/sonic/driver/android/service/impl/UiaClientImpl.java @@ -415,4 +415,57 @@ public void swipe(int fromX, int fromY, int toX, int toY, Integer duration) thro } } + @Override + public void drag(int fromX, int fromY, int toX, int toY, Integer duration, String elementId, String destElId) throws SonicRespException { + checkSessionId(); + JSONObject data = new JSONObject(); + data.put("startX", fromX); + data.put("startY", fromY); + data.put("endX", toX); + data.put("endY", toY); + data.put("steps", duration != null && duration > 0 ? duration / 5 : 100); + data.put("elementId", elementId); + data.put("destElId", destElId); + BaseResp move = respHandler.getResp(HttpUtil.createPost(remoteUrl + "/session/" + sessionId + "/touch/drag").body(data.toJSONString())); + if (move.getErr() == null) { + logger.info("perform drag action %s.", data.toString()); + } else { + logger.error("perform drag action failed."); + throw new SonicRespException(move.getErr().getMessage()); + } + } + + @Override + public void touchAction(String methodType, int x, int y) throws SonicRespException { + checkSessionId(); + JSONObject data = new JSONObject(); + JSONObject touchEventParams = new JSONObject(); + touchEventParams.put("x", x); + touchEventParams.put("y", y); + data.put("params", touchEventParams); + String path = "/touch/down"; + switch (methodType) { + case "down": + break; + case "up": + path = "/touch/up"; + break; + case "move": + path = "/touch/move"; + break; + default: + throw new RuntimeException("methodType error."); + } + + BaseResp b = respHandler.getResp(HttpUtil.createPost(remoteUrl + "/session/" + sessionId + path) + .body(data.toJSONString())); + if (b.getErr() == null) { + logger.info(String.format("perform touch %s action %s.", methodType, data)); + } else { + logger.error("perform touch %s action failed.", methodType); + throw new SonicRespException(b.getErr().getMessage()); + } + } + + } diff --git a/src/test/java/org/cloud/sonic/driver/android/AndroidDriverTest.java b/src/test/java/org/cloud/sonic/driver/android/AndroidDriverTest.java index 94d3beb..ea5521a 100644 --- a/src/test/java/org/cloud/sonic/driver/android/AndroidDriverTest.java +++ b/src/test/java/org/cloud/sonic/driver/android/AndroidDriverTest.java @@ -188,4 +188,27 @@ public void testLongPressAction() throws SonicRespException { 100); } + @Test + public void testDragAction() throws SonicRespException { + // 替换为任意待测试的元素 + androidDriver.drag(182, 629, 565, 2259, 100, null, null); + } + + @Test + public void testTouchActionDown() throws SonicRespException { + // 替换为任意待测试的元素 + androidDriver.touchAction("down", 182, 629); + } + + @Test + public void testTouchActionMove() throws SonicRespException { + // 替换为任意待测试的元素 + androidDriver.touchAction("move", 565, 2259); + } + + @Test + public void testTouchActionUp() throws SonicRespException { + // 替换为任意待测试的元素 + androidDriver.touchAction("up", 565, 2259); + } }