提交 17b82071 authored 作者: 黄承天's avatar 黄承天

增加对几个JS异常的检测

上级 9cffede2
......@@ -4,6 +4,7 @@ import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Queues;
import com.google.common.collect.Sets;
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;
......@@ -17,6 +18,8 @@ import org.jsoup.nodes.Attributes;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.openqa.selenium.*;
import org.openqa.selenium.logging.LogEntry;
import org.openqa.selenium.logging.LogType;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Sleeper;
import org.springframework.beans.factory.annotation.Value;
......@@ -24,10 +27,11 @@ import org.springframework.stereotype.Service;
import java.time.Duration;
import java.util.*;
import java.util.NoSuchElementException;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import static com.zjty.autotest.common.action.LabelType.INPUT;
import static com.zjty.autotest.common.action.Action.*;
import static java.util.Objects.nonNull;
/**
......@@ -87,7 +91,7 @@ public class SeleniumExecutor {
driver.switchTo().window(currentWindow);
while (nonNull(currentUrl)) {
Measure measure = testUrl(currentUrl);
if (nonNull(measure)){
if (nonNull(measure)) {
measures.add(measure);
}
currentUrl = urlQueue.poll();
......@@ -124,7 +128,7 @@ public class SeleniumExecutor {
//获取当前网页的所有元素并放入元素队列
Map<Attributes, WebElement> elements = getAllElements(driver);
log.info("获取完毕...共{}个元素...", elements.size());
//元素序号重置
//当前页面的历史元素记录重置
currentHistoryAttributes.clear();
//开始遍历操作元素队列中的元素 返回各个元素测试信息
List<ElementDetail> elementDetails = Lists.newArrayList();
......@@ -139,13 +143,15 @@ public class SeleniumExecutor {
screenshot = String.format("http://%s:%s/screenshots/%s", screenshotPrefixHost, screenshotPrefixPort, screenshotName);
}
//总合信息
HashSet<String> messages = Sets.newHashSet();
elementDetails.forEach(elementDetail -> messages.add(elementDetail.getMessage()));
Set<String> messages = elementDetails.stream()
.map(ElementDetail::getMessage)
.filter(Objects::nonNull)
.collect(Collectors.toSet());
StringBuilder message = new StringBuilder();
messages.forEach(message::append);
for (String msg : messages) {
message.append(msg);
message.append(';');
message.append(";");
}
log.info("遍历完毕...");
measure = new Measure(
......@@ -165,14 +171,14 @@ public class SeleniumExecutor {
for (Attributes attributes : elements.keySet()) {
WebElement element = elements.get(attributes);
if (!currentHistoryAttributes.contains(attributes) && isInputAble(element)) {
ElementDetail elementDetail = actInput(attributes, element);
ElementDetail elementDetail = act(attributes, element, INPUT);
elementDetails.add(elementDetail);
}
}
for (Attributes attributes : elements.keySet()) {
WebElement element = elements.get(attributes);
if (!currentHistoryAttributes.contains(attributes) && isClickAble(element)) {
ElementDetail elementDetail = actClick(attributes, element);
ElementDetail elementDetail = act(attributes, element, CLICK);
boolean urlChanged = checkURl();
boolean sourceChanged = checkSource();
if (urlChanged) {
......@@ -192,82 +198,56 @@ public class SeleniumExecutor {
}
}
private ElementDetail actInput(Attributes attributes, WebElement element) {
private ElementDetail act(Attributes attributes, WebElement element, String actType) {
String type = String.format("<%s>%s", element.getTagName(), element.getText());
String message = "";
Long responseTime = -1L;
String message = null;
long responseTime = 0L;
boolean success = false;
try {
if (isEnabled(element)) {
log.info("Input元素 ----- tag:[{}] ------ text:[{}] ---- attrs:[{}]", element.getTagName(), element.getText(), attributes);
long startTime = System.currentTimeMillis();
if (!inputs.isEmpty()) {
Input input = inputs.remove(0);
element.sendKeys(input.getValue());
if (Objects.equals(actType, INPUT)) {
log.info("Input操作 元素 ----- tag:[{}] ------ text:[{}] ---- attrs:[{}]", element.getTagName(), element.getText(), attributes);
if (!inputs.isEmpty()) {
Input input = inputs.remove(0);
element.sendKeys(input.getValue());
}
success = true;
} else if (Objects.equals(actType, CLICK)) {
log.info("Click操作 元素 ----- tag:[{}] ------ text:[{}] ---- attrs:[{}]", element.getTagName(), element.getText(), attributes);
element.click();
sleep();
success = true;
}
long endTime = System.currentTimeMillis();
responseTime = endTime - startTime;
success = true;
Alert alert = ExpectedConditions.alertIsPresent().apply(driver);
if (nonNull(alert)) {
message = "出现警告窗:" + alert.getText();
alert.accept();
success = false;
}
}
} catch (ElementNotInteractableException e) {
message = "异常的布局";
success = false;
log.error("error:存在可操作范围之外的元素");
} catch (TimeoutException e) {
message = "页面超时";
success = false;
log.error("error:存在可操作范围之外的元素");
} catch (WebDriverException e) {
message = "无法访问的地址";
success = false;
log.error("error:无法访问的地址");
} catch (Exception e) {
message = "出现预料之外的错误";
success = false;
log.error("error:" + e);
}
historyAttributes.add(attributes);
currentHistoryAttributes.add(attributes);
return new ElementDetail(
type,
responseTime.intValue(),
success,
message
);
}
private ElementDetail actClick(Attributes attributes, WebElement element) {
String type = String.format("<%s>%s", element.getTagName(), element.getText());
String message = "";
Long responseTIme = 0L;
boolean success = false;
try {
if (isEnabled(element)) {
log.info("Input元素 ----- tag:[{}] ------ text:[{}] ---- attrs:[{}]", element.getTagName(), element.getText(), attributes);
element.click();
sleep();
success = true;
Alert alert = ExpectedConditions.alertIsPresent().apply(driver);
if (nonNull(alert)) {
message = "出现警告窗:" + alert.getText();
alert.accept();
String jsMsg = checkJsError();
if (nonNull(jsMsg)) {
message = jsMsg;
success = false;
log.error("error:出现js异常:{}", jsMsg);
}
}
} catch (ElementNotInteractableException e) {
message = "异常的布局";
message = "布局错误";
success = false;
log.error("error:存在可操作范围之外的元素");
} catch (TimeoutException e) {
message = "页面超时";
success = false;
log.error("error:存在可操作范围之外的元素");
} catch (NoSuchElementException e) {
message = "依赖缺失";
success = false;
log.error("error:依赖缺失");
} catch (WebDriverException e) {
message = "无法访问的地址";
success = false;
......@@ -281,7 +261,7 @@ public class SeleniumExecutor {
currentHistoryAttributes.add(attributes);
return new ElementDetail(
type,
responseTIme.intValue(),
(int) responseTime,
success,
message
);
......@@ -372,7 +352,7 @@ public class SeleniumExecutor {
}
private Boolean isInputAble(WebElement element) {
return Objects.equals(element.getTagName(), INPUT);
return Objects.equals(element.getTagName(), LabelType.INPUT);
}
@SuppressWarnings("unused")
......@@ -445,4 +425,24 @@ public class SeleniumExecutor {
return result;
}
@SuppressWarnings("OptionalGetWithoutIsPresent")
private String checkJsError() {
try {
return analyseLogError(driver.manage().logs().get(LogType.BROWSER).getAll().stream().findFirst().get());
} catch (Exception e){
return null;
}
}
private String analyseLogError(LogEntry logEntry) {
if (logEntry.getMessage().contains("Uncaught TypeError:")) {
return "JS类型错误";
} else if (logEntry.getMessage().contains("Failed to load resource: net::ERR_FILE_NOT_FOUND")) {
return "资源加载错误";
} else if (logEntry.getMessage().contains("Failed to load resource: net::ERR_CONNECTION_RESET")) {
return "数据请求错误";
}
return null;
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论