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:AndroidDriver中增加tap,longPress,swipe的方法,对齐IOSDriver #85

Merged
merged 5 commits into from
Jun 17, 2023
Merged
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
50 changes: 50 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 @@ -405,4 +405,54 @@ public byte[] screenshot() throws SonicRespException {
public void setAppiumSettings(JSONObject settings) throws SonicRespException {
uiaClient.setAppiumSettings(settings);
}

/**
* tap position on screen.
*
* @param x
* @param y
* @throws SonicRespException
*/
public void tap(int x, int y) throws SonicRespException {
uiaClient.tap(x, y);
}

/**
* long press position on screen.
*
* @param x
* @param y
* @param ms
* @throws SonicRespException
*/
public void longPress(double x, double y, double ms) throws SonicRespException {
uiaClient.longPress(x, y, ms);
}

/**
* swipe position on screen.
*
* @param fromX
* @param fromY
* @param toX
* @param toY
* @throws SonicRespException
*/
public void swipe(int fromX, int fromY, int toX, int toY) throws SonicRespException {
this.swipe(fromX, fromY, toX, toY, null);
}

/**
* swipe position on screen with target time
*
* @param fromX
* @param fromY
* @param toX
* @param toY
* @param duration
* @throws SonicRespException
*/
public void swipe(int fromX, int fromY, int toX, int toY, Integer duration) throws SonicRespException {
uiaClient.swipe(fromX, fromY, toX, toY, duration);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,10 @@ public interface UiaClient {

//appium setting handler.
void setAppiumSettings(JSONObject settings) throws SonicRespException;

void tap(int x, int y) throws SonicRespException;

void longPress(double x, double y, double ms) throws SonicRespException;

void swipe(int fromX, int fromY, int toX, int toY, Integer duration) throws SonicRespException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -353,4 +353,65 @@ public void setAppiumSettings(JSONObject settings) throws SonicRespException {
throw new SonicRespException(b.getErr().getMessage());
}
}

@Override
public void tap(int x, int y) throws SonicRespException {
checkSessionId();
JSONObject data = new JSONObject();
data.put("x", x);
data.put("y", y);
BaseResp b = respHandler.getResp(HttpUtil.createPost(remoteUrl + "/session/" + sessionId + "/appium/tap")
.body(data.toJSONString()));
if (b.getErr() == null) {
logger.info("perform tap action %s.", data.toString());
} else {
logger.error("perform tap action failed.");
throw new SonicRespException(b.getErr().getMessage());
}
}

@Override
public void longPress(double x, double y, double ms) throws SonicRespException {
checkSessionId();
JSONObject data = new JSONObject();
JSONObject touchEventParams = new JSONObject();
touchEventParams.put("x", x);
touchEventParams.put("y", y);
touchEventParams.put("duration", ms);
data.put("params", touchEventParams);
BaseResp b = respHandler.getResp(HttpUtil.createPost(remoteUrl + "/session/" + sessionId + "/touch/longclick")
.body(data.toJSONString()));
if (b.getErr() == null) {
logger.info("perform longPress action %s.", data.toString());
} else {
logger.error("perform longPress action failed.");
throw new SonicRespException(b.getErr().getMessage());
}
}

@Override
public void swipe(int fromX, int fromY, int toX, int toY, Integer duration) throws SonicRespException {
checkSessionId();
JSONObject data = new JSONObject();
data.put("startX", fromX);
data.put("startY", fromY);
data.put("endX", toX);
data.put("endY", toY);
// steps 参数为uiautomator层定义的单位
// example:So for a 100 steps, the swipe will take about 1/2 second to complete.
if (duration == null) {
data.put("steps", 100);
} else {
data.put("steps", duration / 5);
}
BaseResp b = respHandler.getResp(HttpUtil.createPost(remoteUrl + "/session/" + sessionId + "/touch/perform")
.body(data.toJSONString()));
if (b.getErr() == null) {
logger.info("perform swipe action %s.", data.toString());
} else {
logger.error("perform swipe action failed.");
throw new SonicRespException(b.getErr().getMessage());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,33 @@ public void testScreenshot() throws IOException, SonicRespException {
public void testSetAppiumSettings() throws SonicRespException {
androidDriver.setAppiumSettings(new JSONObject());
}

@Test
public void testSwipeAction() throws SonicRespException {
// 默认滑动操作的完成时间,500毫秒
androidDriver.swipe(540, 1710, 540, 200);
// 指定滑动操作在1000毫秒内完成
androidDriver.swipe(540, 1710, 540, 200, 1000);
}

@Test
public void testTapAction() throws SonicRespException {
// 替换为任意待测试的元素
AndroidElement androidElement = androidDriver.findElement(AndroidSelector.Id,
"com.xueqiu.android:id/my_groups_new_title_bar_ding");
ElementRect elementRect = androidElement.getRect();
androidDriver.tap(elementRect.getX() + elementRect.getWidth() / 2, elementRect.getY() + elementRect.getHeight() / 2);
}

@Test
public void testLongPressAction() throws SonicRespException {
// 替换为任意待测试的元素
AndroidElement androidElement = androidDriver.findElement(AndroidSelector.Id,
"com.xueqiu.android:id/my_groups_list_item_stock_name_label");
ElementRect elementRect = androidElement.getRect();
androidDriver.longPress((double) elementRect.getX() + (double) elementRect.getWidth() / 2,
(double) elementRect.getY() + (double) elementRect.getHeight() / 2,
100);
}

}