fix:上位机

1.修改xpath的方式的逻辑
master
李杰应 2024-08-15 20:10:46 +08:00
parent 8b0939baf8
commit 6ef190c7d7
1 changed files with 45 additions and 22 deletions

View File

@ -1311,7 +1311,7 @@ public class ElementUtil {
Integer perPageTimeout = waitTimeout;
Integer swipeNum = swipeCount;
if ((swipeCount <= 0 && swipeCount > 5)/*限定滑屏查找不超过5次*/ || "0".equals(swipe) /*不滑屏*/) {
if ((swipeCount <= 0 || swipeCount > 5)/*限定滑屏查找不超过5次*/ || "0".equals(swipe) /*不滑屏*/) {
swipeNum = 0;
}
if (swipeNum > 0) {
@ -1322,25 +1322,7 @@ public class ElementUtil {
} catch (NoSuchSessionException e) {
throw e;
}
WebElement webElement = null;
while (swipeNum >= 0) {
List<WebElement> elements = null;
try {
elements = driver.findElements(By.xpath(xpath));
} catch (WebDriverException e) {
logger.error("driver查找控件异常", e);
throw new ExecuteException("设备自动化驱动连接异常,查找控件失败");
}
logger.info("找到元素{}个", elements.size());
if (!CollectionUtils.isEmpty(elements)) { //xpath找到了
webElement = elements.get(0);
} else { //xpath没找到
if (swipeNum > 0) {
ScreenUtil.standardSwipe(swipe, 2000, driver);
}
}
swipeNum--;
}
WebElement webElement = getWebElement(driver, swipe, xpath, swipeNum);
/*SessionId sessionId = driver.getSessionId();
logger.debug("当前driver的sessionId为{}", sessionId);
@ -1358,8 +1340,24 @@ public class ElementUtil {
logger.error("xpath找元素出错", e);
xpathThread.kill();
}*/
if (null != webElement) {
Rectangle rect = webElement.getRect();
Rectangle rect = null;
try {
rect = webElement.getRect();
} catch (StaleElementReferenceException e) {
logger.warn("页面发生变化,重新查找元素", e);
try {
Thread.sleep(2000L);
} catch (InterruptedException ex) {
logger.error("在查找元素等待时被打断", ex);
}
logger.info("再次查找当前页面的元素");
WebElement we = getWebElement(driver, swipe, xpath, swipeNum);
if (null != we) {
rect = we.getRect();
}
}
if (null != rect) {
int x = rect.getX();
int y = rect.getY();
int width = rect.getWidth();
@ -1369,6 +1367,31 @@ public class ElementUtil {
return point;
}
private static WebElement getWebElement(AppiumDriver<WebElement> driver, String swipe, String xpath, Integer swipeNum) {
WebElement webElement = null;
while (swipeNum >= 0) {
List<WebElement> elements = null;
try {
elements = driver.findElements(By.xpath(xpath));
} catch (WebDriverException e) {
logger.error("driver查找控件异常", e);
throw e;
}
logger.info("找到元素{}个", elements.size());
if (!CollectionUtils.isEmpty(elements)) { //xpath找到了
webElement = elements.get(0);
} else { //xpath没找到
// 输出当前页面的ui树
logger.debug("当前页面的信息:{}", driver.getPageSource());
if (swipeNum > 0) {
ScreenUtil.standardSwipe(swipe, 2000, driver);
}
}
swipeNum--;
}
return webElement;
}
public static WebElement findWebElementByXpath(AppiumDriver<WebElement> driver, IStepTarget target, Integer waitTimeout, boolean waitStatus, String swipe, Integer swipeCount) {
JSONObject object = JSON.parseObject(target.getValue(), JSONObject.class);