提交 7ed062e8 authored 作者: 黄承天's avatar 黄承天

修正操作过程中元素过期问题

上级 d8919640
......@@ -8,6 +8,7 @@ import com.zjty.autotest.common.action.LabelType;
import com.zjty.autotest.pojo.report.ElementDetail;
import com.zjty.autotest.pojo.report.Measure;
import com.zjty.autotest.pojo.report.Report;
import com.zjty.autotest.pojo.test.ActResult;
import com.zjty.autotest.pojo.test.Input;
import com.zjty.autotest.pojo.test.Project;
import com.zjty.autotest.util.FileUtil;
......@@ -139,7 +140,7 @@ public class SeleniumExecutor {
currentHistoryAttributes.clear();
//开始遍历操作元素队列中的元素 返回各个元素测试信息
List<ElementDetail> elementDetails = Lists.newArrayList();
traversal(elements, elementDetails);
traversal(elementDetails);
//如果全部元素通过则是通过
Boolean success = elementDetails.stream().allMatch(ElementDetail::getSuccess);
//如果出错 则提供截图
......@@ -172,43 +173,37 @@ public class SeleniumExecutor {
return measure;
}
private void traversal(Map<Attributes, WebElement> elements, List<ElementDetail> elementDetails) {
private void traversal(List<ElementDetail> elementDetails) {
for (Attributes attributes : elements.keySet()) {
WebElement element = elements.get(attributes);
if (!currentHistoryAttributes.contains(attributes) && isInputAble(element)) {
ElementDetail elementDetail = act(attributes, element, INPUT);
ActResult act = act(attributes, element, INPUT);
ElementDetail elementDetail = act.getElementDetail();
elementDetails.add(elementDetail);
if (act.getStaleness()) {
traversal(elementDetails);
}
}
}
for (Attributes attributes : elements.keySet()) {
WebElement element = elements.get(attributes);
if (!currentHistoryAttributes.contains(attributes) && isClickAble(element)) {
ElementDetail elementDetail = act(attributes, element, CLICK);
boolean urlChanged = checkURl();
boolean sourceChanged = checkSource();
if (urlChanged) {
boolean newPage = !historyUrls.contains(driver.getCurrentUrl())
&& !Sets.newHashSet(urlQueue).contains(driver.getCurrentUrl())
&& sourceChanged;
if (newPage) {
log.info("检测到新url:{} 加入队列 ", driver.getCurrentUrl());
urlQueue.add(driver.getCurrentUrl());
urlQueue = queueDuplicateRemoval(urlQueue);
}
reload();
}
if (nonNull(elementDetail)) {
elementDetails.add(elementDetail);
ActResult act = act(attributes, element, CLICK);
ElementDetail elementDetail = act.getElementDetail();
elementDetails.add(elementDetail);
if (act.getStaleness()) {
traversal(elementDetails);
}
}
}
}
private ElementDetail act(Attributes attributes, WebElement element, String actType) {
private ActResult act(Attributes attributes, WebElement element, String actType) {
String type = String.format("<%s>%s", element.getTagName(), element.getText());
String message = null;
long responseTime = -1L;
boolean success = false;
boolean staleness = false;
try {
if (isEnabled(element)) {
log.info("元素 ----- tag:[{}] ------ text:[{}] ---- attrs:[{}] ------ notStale:{}", element.getTagName(), element.getText(), attributes, !ExpectedConditions.stalenessOf(element).apply(driver));
......@@ -225,6 +220,24 @@ public class SeleniumExecutor {
element.click();
sleep();
success = true;
boolean urlChanged = checkURl();
boolean sourceChanged = checkSource();
if (urlChanged) {
boolean newPage = !historyUrls.contains(driver.getCurrentUrl())
&& !Sets.newHashSet(urlQueue).contains(driver.getCurrentUrl())
&& sourceChanged;
if (newPage) {
log.info("检测到新url:{} 加入队列 ", driver.getCurrentUrl());
urlQueue.add(driver.getCurrentUrl());
urlQueue = queueDuplicateRemoval(urlQueue);
}
reload();
}
if (ExpectedConditions.stalenessOf(element).apply(driver)) {
element = elements.get(attributes);
staleness = true;
}
}
long endTime = System.currentTimeMillis();
responseTime = endTime - startTime;
......@@ -249,10 +262,10 @@ public class SeleniumExecutor {
}
} catch (ElementNotInteractableException e) {
log.error("error:可操作范围之外的元素");
return null;
return new ActResult(staleness, null);
} catch (StaleElementReferenceException e) {
log.error("元素过期");
return null;
return new ActResult(staleness, null);
} catch (TimeoutException e) {
message = "页面超时";
success = false;
......@@ -272,11 +285,14 @@ public class SeleniumExecutor {
}
historyAttributes.add(attributes);
currentHistoryAttributes.add(attributes);
return new ElementDetail(
type,
(int) responseTime,
success,
message
return new ActResult(
staleness,
new ElementDetail(
type,
(int) responseTime,
success,
message
)
);
}
......@@ -321,7 +337,7 @@ public class SeleniumExecutor {
driver.get(currentUrl);
sleep();
currentSource = driver.getPageSource();
elements = getAllElements(driver);
elements = getAllElements(driver);
}
@SuppressWarnings("unused")
......@@ -334,25 +350,20 @@ public class SeleniumExecutor {
}
private Map<Attributes, WebElement> getAllElements(WebDriver driver) {
try {
List<WebElement> webElements = Lists.newArrayList();
driver.findElements(By.xpath("*"))
.forEach(element -> getSubElements(element, webElements));
Document document = Jsoup.parse(driver.getPageSource());
List<Element> jElements = document.getAllElements().stream()
.filter(element -> element.childrenSize() == 0)
.collect(Collectors.toList());
Map<Attributes, WebElement> result = Maps.newHashMap();
for (int i = 0; i < webElements.size(); i++) {
WebElement webElement = webElements.get(i);
Element jElement = jElements.get(i);
result.put(jElement.attributes(), webElement);
}
return result;
} catch (StaleElementReferenceException e) {
log.error("获取元素过程中遇到元素过期 重新开始获取");
return getAllElements(driver);
List<WebElement> webElements = Lists.newArrayList();
driver.findElements(By.xpath("*"))
.forEach(element -> getSubElements(element, webElements));
Document document = Jsoup.parse(driver.getPageSource());
List<Element> jElements = document.getAllElements().stream()
.filter(element -> element.childrenSize() == 0)
.collect(Collectors.toList());
Map<Attributes, WebElement> result = Maps.newHashMap();
for (int i = 0; i < webElements.size(); i++) {
WebElement webElement = webElements.get(i);
Element jElement = jElements.get(i);
result.put(jElement.attributes(), webElement);
}
return result;
}
private void getSubElements(WebElement element, List<WebElement> elements) {
......@@ -434,10 +445,10 @@ public class SeleniumExecutor {
boolean yMatch = (elementLocation.y) >= 0 && (elementLocation.y <= size.height);
return xMatch && yMatch;
} catch (StaleElementReferenceException e) {
log.error("判断位置布局时元素过期");
return true;
log.error("判断位置时出现元素过期");
return false;
} catch (Exception e) {
log.error("error:" + e);
log.error("判断位置时出现错误:" + e);
return false;
}
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论