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

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

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