From c05ca15444d8e277a52d12f859bdb3db18c19137 Mon Sep 17 00:00:00 2001 From: "yineng.huang" Date: Tue, 30 Jul 2024 15:40:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=BB=91=E5=8A=A8=E8=A7=A3=E9=94=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../element/mobile/keywords/CommonTools.java | 51 +++++++++++ .../mobile/thread/PressAndSwipeThread.java | 86 +++++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 cctp-test-element/cctp-test-element-library/cctp-test-element-mobile/src/main/java/net/northking/cctp/element/mobile/thread/PressAndSwipeThread.java diff --git a/cctp-test-element/cctp-test-element-library/cctp-test-element-mobile/src/main/java/net/northking/cctp/element/mobile/keywords/CommonTools.java b/cctp-test-element/cctp-test-element-library/cctp-test-element-mobile/src/main/java/net/northking/cctp/element/mobile/keywords/CommonTools.java index ed48d2a..f2f5f45 100644 --- a/cctp-test-element/cctp-test-element-library/cctp-test-element-mobile/src/main/java/net/northking/cctp/element/mobile/keywords/CommonTools.java +++ b/cctp-test-element/cctp-test-element-library/cctp-test-element-mobile/src/main/java/net/northking/cctp/element/mobile/keywords/CommonTools.java @@ -1634,6 +1634,57 @@ public class CommonTools { return result; } + @Keyword(alias = "滑动解锁", category = "0", attributes = "4") + @Return(name = "result", comment = "手机滑动", type = DataType.BOOLEAN) + public Boolean pressAndSwipe( + IExecuteContext context, + @Argument(name = "__deviceDriver", scope = ParamScope.CONTEXT, comment = "设备连接", type = DataType.OBJECT) DeviceDriver deviceDriver, + @Argument(name = "targets", scope = ParamScope.STEP, comment = "控件位置", type = DataType.LIST, defaultDisplay = false) List targets, + @Argument(name = "passingPoints", scope = ParamScope.ARGS, comment = "经过的点", type = DataType.LIST, defaultDisplay = true) List passingPoints, + @Argument(name = "waitTime", scope = ParamScope.ARGS, comment = "等待时间(单位s)", type = DataType.INTEGER, defaultValue = "30") Integer waitTime, + @Argument(name = "preExecuteWait", scope = ParamScope.ARGS, comment = "执行前等待(单位:s)", type = DataType.INTEGER, required = false, defaultValue = "1.0", defaultDisplay = false) Float preExecuteWait, + @Argument(name = "sufExecuteWait", scope = ParamScope.ARGS, comment = "执行后等待(单位:s)", type = DataType.INTEGER, required = false, defaultValue = "1.0", defaultDisplay = false) Float sufExecuteWait + ) { + PressAndSwipeThread monkeyTestThread = new PressAndSwipeThread(deviceDriver,targets,passingPoints,preExecuteWait,sufExecuteWait); + Future future = executorService.submit(monkeyTestThread); + Boolean result = null; + deviceDriver.setFuture(future); + int count = targets.size(); + try { + result = future.get(waitTime*count, TimeUnit.SECONDS); + } catch (InterruptedException e) { + logger.warn("该线程被中断了。。。。。。"); + throw new ExecuteException("执行中断"); + } catch (ExecutionException e) { + logger.warn("在执行的时候报错了。。。。"); + Throwable cause = e.getCause(); + if (cause instanceof ExecuteException) { + ExecuteException ee = (ExecuteException) cause; + throw ee; + } else if (cause instanceof ParamMistakeException) { + ParamMistakeException pe = (ParamMistakeException) cause; + throw pe; + } else if (cause instanceof NoSuchSessionException) { + NoSuchSessionException ne = (NoSuchSessionException) cause; + throw ne; + } else if (cause instanceof EnvironmentException) { + EnvironmentException ee = (EnvironmentException) cause; + throw ee; + }else{ + logger.error("出现了其他的问题。。。。"); + throw new ExecuteException(cause.getMessage()); + } + } catch (TimeoutException e) { + logger.warn("处理超时了。。。。。。"); + throw new ExecuteException("输入超时"); + } catch (CancellationException e) { + logger.warn("该操作被取消了"); + throw new ExecuteException("用户取消,执行失败"); + } + return result; + } + + private void setParamValue(IExecuteContext context, String variable, String value) { try { if (variable.contains("#{") && variable.contains("}")) { diff --git a/cctp-test-element/cctp-test-element-library/cctp-test-element-mobile/src/main/java/net/northking/cctp/element/mobile/thread/PressAndSwipeThread.java b/cctp-test-element/cctp-test-element-library/cctp-test-element-mobile/src/main/java/net/northking/cctp/element/mobile/thread/PressAndSwipeThread.java new file mode 100644 index 0000000..9dcb572 --- /dev/null +++ b/cctp-test-element/cctp-test-element-library/cctp-test-element-mobile/src/main/java/net/northking/cctp/element/mobile/thread/PressAndSwipeThread.java @@ -0,0 +1,86 @@ +package net.northking.cctp.element.mobile.thread; + +import com.alibaba.fastjson.JSON; +import io.appium.java_client.AppiumDriver; +import net.northking.cctp.element.core.DeviceDriver; +import net.northking.cctp.element.core.IExecuteContext; +import net.northking.cctp.element.core.IStepTarget; +import net.northking.cctp.element.core.exception.ExecuteException; +import net.northking.cctp.element.mobile.constants.UsingType; +import net.northking.cctp.element.mobile.entity.PointMessage; +import net.northking.cctp.element.mobile.utils.CommonUtils; +import net.northking.cctp.element.mobile.utils.phoneUtils.ElementUtil; +import net.northking.cctp.element.mobile.utils.phoneUtils.HandleUtils; +import net.northking.cctp.element.mobile.utils.phoneUtils.ScreenUtil; +import org.openqa.selenium.Point; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.interactions.PointerInput; +import org.openqa.selenium.interactions.Sequence; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.time.Duration; +import java.util.*; +import java.util.concurrent.Callable; + +public class PressAndSwipeThread implements Callable { + + private Logger logger = LoggerFactory.getLogger(PressAndSwipeThread.class); + + private DeviceDriver deviceDriver; + private List passingPoints; + private Float preExecuteWait; + private Float sufExecuteWait; + private List targets; + + public PressAndSwipeThread(DeviceDriver deviceDriver, List targets,List passingPoints, Float preExecuteWait, Float sufExecuteWait) { + this.deviceDriver = deviceDriver; + this.targets = targets; + this.passingPoints = passingPoints; + this.preExecuteWait = preExecuteWait; + this.sufExecuteWait = sufExecuteWait; + + } + + @Override + public Boolean call() throws Exception { + CommonUtils.handlePreExecuteWait(preExecuteWait); + AppiumDriver driver = (AppiumDriver) deviceDriver; + WebElement webElement = null; + for (IStepTarget target : targets) { + if (UsingType.SELECTOR.equalsIgnoreCase(target.getUsing())) { + webElement = ElementUtil.findWebElementByXpath(driver, target, 30, true, "0", 0); + } + } + if (null == webElement) { + throw new ExecuteException("未找到解锁的滑动区域"); + } + Point location = webElement.getLocation(); + int x = location.getX(); + int y = location.getY(); + int width = webElement.getRect().width; + int height = webElement.getRect().height; + List> pointList = new ArrayList<>(); + logger.info("手势区域,位置=>x:{},y:{},宽:{},高:{}",x,y,width,height); + int perWidth = width /3; + int perHeight = height /3; + int pointY = y - perHeight/2; + int n = 1; + for (int i = 0; i < 3; i++) { + pointY = pointY + perHeight; //每一行的y坐标 + int pointX = x - perWidth/2; + for (int j = 0; j < 3; j++) { + pointX = pointX + perWidth; + logger.info("第{}个点的坐标x:{},y:{}",n,pointX,pointY); + Map point = new HashMap<>(); + point.put("x", pointX); + point.put("y", pointY); + pointList.add(n-1,point); + n++; + } + } + CommonUtils.handleSufExecuteWait(sufExecuteWait); + ScreenUtil.pressAndSwipe(driver, pointList, passingPoints); + return true; + } +}