提交 0d1a333e authored 作者: 黄承天's avatar 黄承天

完成遍历测试执行类与方法

上级 a2093f05
......@@ -17,7 +17,7 @@ public class ResourceController {
return "index.html";
}
@RequestMapping("/pic/{name}")
@RequestMapping("/screenshot/{name}")
public String picture(@PathVariable String name){
return name + ".png";
}
......
......@@ -16,6 +16,7 @@ import org.openqa.selenium.support.ui.WebDriverWait;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;
import static com.zjty.autotest.common.action.LabelType.*;
......@@ -58,6 +59,7 @@ public class SeleniumExecutor {
public Report execute(Project project) {
driver = WebDriverUtil.getWebDriver(project.getBrowser());
driver.manage().timeouts().implicitlyWait(10L, TimeUnit.SECONDS);
inputs = project.getInputs();
List<Measure> measures = Lists.newArrayList();
String os = "";
......@@ -108,8 +110,9 @@ public class SeleniumExecutor {
//如果出错 则提供截图
String screenshot = "";
if (!success) {
screenShot(currentUrl);
screenshot = "http://localhost:13500/screenshots/" + currentUrl;
String screenshotName = alterUrlName(currentUrl);
screenshot(screenshotName);
screenshot = String.format("http://localhost:13500/screenshots/%s", screenshotName);
}
log.info("遍历完毕...");
measure = new Measure(
......@@ -124,15 +127,12 @@ public class SeleniumExecutor {
}
private String traversal() {
String message = null;
StringBuilder message = new StringBuilder();
while (elementIndex < elements.size()) {
WebElement element = elements.get(elementIndex);
message = act(element);
if (nonNull(message)) {
break;
}
message.append(act(element));
}
return message;
return message.toString();
}
private String act(WebElement element) {
......@@ -162,15 +162,21 @@ public class SeleniumExecutor {
} catch (ElementNotInteractableException e) {
message = "异常的布局;";
log.error("error:在可操作范围之外的元素");
} catch (TimeoutException e) {
message = "第" + (elementIndex + 1) + "个元素操作超时;";
log.error("error:操作超时");
} catch (Exception e) {
message = "预料之外的异常:" + e.getMessage() + ";";
log.error("error:" + e.getMessage());
log.error("error:" + e);
}
return message;
}
private Boolean checkPage() {
boolean pageChange = !Objects.equals(currentUrl, driver.getCurrentUrl());
boolean pageChange = !Objects.equals(
getMainUrl(currentUrl),
getMainUrl(driver.getCurrentUrl())
);
if (pageChange) {
boolean newPage = !historyUrls.contains(driver.getCurrentUrl());
if (newPage) {
......@@ -183,6 +189,10 @@ public class SeleniumExecutor {
}
}
private String getMainUrl(String url) {
return url.substring(0, url.indexOf(url));
}
private void inputValue(WebElement element, List<Input> inputs) {
Predicate<Input> inputMatch = input -> Objects.equals(element.getAttribute(input.getAttrName()), input.getAttrValue());
Optional<Input> inputOptional = inputs.stream().filter(inputMatch).findAny();
......@@ -251,7 +261,19 @@ public class SeleniumExecutor {
}
private void screenShot(String name) {
private String alterUrlName(String fileName) {
// /\?*:"<>|
return fileName.replace("/", "")
.replace("\\", "")
.replace("?", "")
.replace("*", "")
.replace(":", "")
.replace("<", "")
.replace(">", "")
.replace("|", "");
}
private void screenshot(String name) {
byte[] bytes = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
FileUtil.output(bytes, FileUtil.WORK_PATH + "screenshots\\" + name + ".png");
}
......
......@@ -40,7 +40,6 @@ class AutotestApplicationTests {
caps.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
WebDriver driver = new ChromeDriver();
String url = "file:///C:/MyProjects/auto-test/wps/index.html";
long startTime = System.currentTimeMillis();
driver.get(url);
Project project = new Project(
null,
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论