Skip to content

Commit

Permalink
Merge pull request #370 from caofengbin/feature/fix_scroll_to
Browse files Browse the repository at this point in the history
fix:修复Android上滚动到元素可见操作,滑动过快的问题
  • Loading branch information
ZhouYixun authored Jul 3, 2023
2 parents a8407b7 + 92443a5 commit ba3fe57
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -850,9 +850,9 @@ public void scrollToEle(HandleContext handleContext, String des, String selector

try {
if ("up".equals(direction)) {
AndroidTouchHandler.swipe(iDevice, xOffset, screenHeight / 3, xOffset, screenHeight * 2 / 3);
AndroidTouchHandler.swipe(iDevice, xOffset, screenHeight / 3, xOffset, screenHeight * 2 / 3, 1000);
} else if ("down".equals(direction)) {
AndroidTouchHandler.swipe(iDevice, xOffset, screenHeight * 2 / 3, xOffset, screenHeight / 3);
AndroidTouchHandler.swipe(iDevice, xOffset, screenHeight * 2 / 3, xOffset, screenHeight / 3, 1000);
} else {
handleContext.setE(new Exception("未知的滚动到方向类型设置"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.cloud.sonic.agent.common.maps.AndroidDeviceManagerMap;
import org.cloud.sonic.agent.common.maps.HandlerMap;
import org.cloud.sonic.agent.tools.PortTool;
import org.cloud.sonic.driver.android.AndroidDriver;
import org.cloud.sonic.driver.common.tool.SonicRespException;

import java.io.IOException;
Expand All @@ -45,7 +44,8 @@ public class AndroidTouchHandler {
private static final Map<String, Thread> touchMap = new ConcurrentHashMap<>();
private static final Map<String, TouchMode> touchModeMap = new ConcurrentHashMap<>();
private static final Map<String, int[]> sizeMap = new ConcurrentHashMap<>();
private static final int SWIPE_DURATION = 500;
// 默认的滑动操作完成时间,单位为毫秒
private static final int DEFAULT_SWIPE_DURATION = 500;

public enum TouchMode {
SONIC_APK,
Expand Down Expand Up @@ -108,6 +108,10 @@ public static void longPress(IDevice iDevice, int x, int y, int time) throws Son
}

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

public static void swipe(IDevice iDevice, int x1, int y1, int x2, int y2, int swipeDuration) throws SonicRespException {
switch (getTouchMode(iDevice)) {
case SONIC_APK -> {
int[] re1 = transferWithRotation(iDevice, x1, y1);
Expand All @@ -118,14 +122,12 @@ public static void swipe(IDevice iDevice, int x1, int y1, int x2, int y2) throws
} catch (InterruptedException e) {
e.printStackTrace();
}
// 默认500毫秒完成滑动操作
int duration = SWIPE_DURATION;
long startTime = System.currentTimeMillis();
while (true) {
// 当前时间
long currentTime = System.currentTimeMillis();
// 计算时间进度
float timeProgress = (currentTime - startTime) / (float) duration;
float timeProgress = (currentTime - startTime) / (float) swipeDuration;
if (timeProgress >= 1.0f) {
// 已经过渡到结束值,停止过渡
writeToOutputStream(iDevice, String.format("move %d %d\n", re2[0], re2[1]));
Expand Down Expand Up @@ -154,11 +156,11 @@ public static void swipe(IDevice iDevice, int x1, int y1, int x2, int y2) throws
writeToOutputStream(iDevice, "up\n");
}
case ADB -> AndroidDeviceBridgeTool.executeCommand(iDevice, String.format("input swipe %d %d %d %d %d",
x1, y1, x2, y2, SWIPE_DURATION));
x1, y1, x2, y2, swipeDuration));
case APPIUM_UIAUTOMATOR2_SERVER -> {
AndroidStepHandler curStepHandler = HandlerMap.getAndroidMap().get(iDevice.getSerialNumber());
if (curStepHandler != null && curStepHandler.getAndroidDriver() != null) {
curStepHandler.getAndroidDriver().swipe(x1, y1, x2, y2, SWIPE_DURATION);
curStepHandler.getAndroidDriver().swipe(x1, y1, x2, y2, swipeDuration);
}
}
default -> throw new IllegalStateException("Unexpected value: " + getTouchMode(iDevice));
Expand Down

0 comments on commit ba3fe57

Please sign in to comment.