Skip to content

Commit

Permalink
Merge pull request #127 from YeungHoiChiu/resolves#126
Browse files Browse the repository at this point in the history
feat: Resolves #126 - 增加拖拽、以及三个touch操作(仅安卓)
  • Loading branch information
ZhouYixun authored May 29, 2024
2 parents ee27ef9 + 3603b2f commit 28bda60
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/main/java/org/cloud/sonic/driver/android/AndroidDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 28bda60

Please sign in to comment.