提交 da9becd6 authored 作者: 孙洁清's avatar 孙洁清

Merge branch 'master' of git.yfzx.zjtys.com.cn:hct/auto-test

package com.zjty.autotest.controller; package com.zjty.autotest.controller;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author C
* 静态资源映射
*/
@SuppressWarnings("SpringMVCViewInspection") @SuppressWarnings("SpringMVCViewInspection")
@Controller @Controller
public class ResourceController { public class ResourceController {
...@@ -12,4 +17,8 @@ public class ResourceController { ...@@ -12,4 +17,8 @@ public class ResourceController {
return "index.html"; return "index.html";
} }
@RequestMapping("/pic/{name}")
public String picture(@PathVariable String name){
return name + ".png";
}
} }
...@@ -40,7 +40,6 @@ public class Measure { ...@@ -40,7 +40,6 @@ public class Measure {
/** /**
* 截图的url * 截图的url
*/ */
private String picture; private String screenshot;
} }
...@@ -42,15 +42,9 @@ public class Report { ...@@ -42,15 +42,9 @@ public class Report {
*/ */
private String os; private String os;
/**
* 测试结果信息
*/
private String message;
/** /**
* 每个路由测试详情 * 每个路由测试详情
*/ */
private List<Measure> measures; private List<Measure> measures;
} }
...@@ -5,6 +5,8 @@ import lombok.AllArgsConstructor; ...@@ -5,6 +5,8 @@ import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.util.List; import java.util.List;
/** /**
...@@ -18,7 +20,7 @@ public class Project { ...@@ -18,7 +20,7 @@ public class Project {
/** /**
* 主键 * 主键
*/ */
private String id; private Integer id;
/** /**
* 项目名称 * 项目名称
......
package com.zjty.autotest.service; package com.zjty.autotest.service.impl;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Queues; import com.google.common.collect.Queues;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.zjty.autotest.pojo.report.Measure;
import com.zjty.autotest.pojo.report.Report;
import com.zjty.autotest.pojo.test.Input;
import com.zjty.autotest.pojo.test.Project;
import com.zjty.autotest.util.FileUtil;
import com.zjty.autotest.util.WebDriverUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.By; import org.openqa.selenium.*;
import org.openqa.selenium.StaleElementReferenceException; import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.WebDriver; import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.WebElement;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.*; import java.util.*;
import java.util.function.Predicate;
import static com.zjty.autotest.common.action.LabelType.*; import static com.zjty.autotest.common.action.LabelType.*;
import static java.util.Objects.nonNull; import static java.util.Objects.nonNull;
/**
* @author C
*/
@SuppressWarnings("FieldCanBeLocal") @SuppressWarnings("FieldCanBeLocal")
@Slf4j @Slf4j
@Service @Service
...@@ -23,7 +31,7 @@ public class SeleniumExecutor { ...@@ -23,7 +31,7 @@ public class SeleniumExecutor {
private WebDriver driver; private WebDriver driver;
private Map<String, String> inputs = Maps.newHashMap(); private List<Input> inputs = Lists.newArrayList();
private Set<String> historyUrls = Sets.newHashSet(); private Set<String> historyUrls = Sets.newHashSet();
...@@ -33,77 +41,135 @@ public class SeleniumExecutor { ...@@ -33,77 +41,135 @@ public class SeleniumExecutor {
private List<WebElement> elements = Lists.newArrayList(); private List<WebElement> elements = Lists.newArrayList();
/**
* 元素序号记录
*/
private Integer elementIndex; private Integer elementIndex;
/**
* 当前进行的窗口页
*/
private String currentWindow; private String currentWindow;
/**
* 当前测试的url
*/
private String currentUrl; private String currentUrl;
public List<String> execute(String url) { public Report execute(Project project) {
driver.get(url); driver = WebDriverUtil.getWebDriver(project.getBrowser());
currentUrl = driver.getCurrentUrl(); inputs = project.getInputs();
List<Measure> measures = Lists.newArrayList();
String os = "";
currentUrl = project.getUrl();
currentWindow = driver.getWindowHandle(); currentWindow = driver.getWindowHandle();
while (nonNull(currentWindow)) { while (nonNull(currentWindow)) {
driver.switchTo().window(currentWindow); driver.switchTo().window(currentWindow);
while (nonNull(currentUrl)) { while (nonNull(currentUrl)) {
testUrl(currentUrl); Measure measure = testUrl(currentUrl);
measures.add(measure);
currentUrl = urlQueue.poll(); currentUrl = urlQueue.poll();
} }
currentWindow = windowQueue.poll(); currentWindow = windowQueue.poll();
driver.close(); driver.close();
checkWindow(); checkWindow();
} }
return Lists.newArrayList(historyUrls); return new Report(
null,
project.getId(),
project.getName(),
project.getBrowser(),
os,
measures
);
} }
private void testUrl(String currentUrl) { private Measure testUrl(String currentUrl) {
Measure measure = null;
//如果该网页是未曾进入过的网页则加入历史记录并进行遍历测试
if (!historyUrls.contains(currentUrl)) { if (!historyUrls.contains(currentUrl)) {
historyUrls.add(driver.getCurrentUrl()); historyUrls.add(driver.getCurrentUrl());
log.info("当前URL:{} 开始进行遍历...", currentUrl); //打开网页 记录响应时间
long startTime = System.currentTimeMillis();
driver.get(currentUrl);
long endTime = System.currentTimeMillis();
Long responseTime = endTime - startTime;
log.info("当前URL:{} 响应时间 {} ms 开始进行遍历...", currentUrl, responseTime);
log.info("正在获取当前网页所有元素..."); log.info("正在获取当前网页所有元素...");
//获取当前网页的所有元素并放入元素队列
getAllElements(driver); getAllElements(driver);
log.info("获取完毕...共{}个元素...", elements.size()); log.info("获取完毕...共{}个元素...", elements.size());
//元素序号重置
elementIndex = 0; elementIndex = 0;
traversal(); //开始遍历操作元素队列中的元素 返回错误信息
String message = traversal();
//如果无错误信息则是通过
Boolean success = Objects.equals(message, "");
//如果出错 则提供截图
String screenshot = "";
if (!success) {
screenShot(currentUrl);
screenshot = "http://localhost:13500/screenshots/" + currentUrl;
}
log.info("遍历完毕..."); log.info("遍历完毕...");
measure = new Measure(
currentUrl,
success,
responseTime.intValue(),
message,
screenshot
);
} }
return measure;
} }
private void traversal() { private String traversal() {
try { String message = null;
while (elementIndex < elements.size()) { while (elementIndex < elements.size()) {
WebElement element = elements.get(elementIndex); WebElement element = elements.get(elementIndex);
act(element); message = act(element);
checkPage(); if (nonNull(message)) {
break;
} }
} catch (Exception e) {
log.error("error:" + e.getMessage());
} }
return message;
} }
private void act(WebElement element) { private String act(WebElement element) {
String message = null;
try { try {
log.info("正在操作第{}个元素 ------ text:[{}] ----- tag:[{}]", elementIndex + 1, element.getText(), element.getTagName()); log.info("正在操作第{}个元素 ------ text:[{}] ----- tag:[{}]", elementIndex + 1, element.getText(), element.getTagName());
elementIndex++; elementIndex++;
if (isEnabledAndDisplayed(element)){ if (isEnabledAndDisplayed(element)) {
if (isEnabledInput(element)) { if (isEnabledInput(element)) {
String id = element.getAttribute("id"); inputValue(element, inputs);
String inputValue = inputs.get(id);
element.sendKeys(inputValue);
sleep(1000L);
} else if (isEnabledClick(element)) { } else if (isEnabledClick(element)) {
element.click(); element.click();
} }
} }
sleep(2000L); sleep();
} catch (StaleElementReferenceException e) { //一旦页面发生过跳转 通过重新打开回到原来的页面后需要重新获取元素
Boolean pageChanged = checkPage();
if (pageChanged) {
reload(); reload();
}
//警告窗口处理
Alert alert = getAlert();
if (nonNull(alert)) {
message = "出现警告窗:" + alert.getText();
alert.accept();
}
} catch (ElementNotInteractableException e) {
message = "异常的布局;";
log.error("error:在可操作范围之外的元素");
} catch (Exception e) { } catch (Exception e) {
message = "预料之外的异常:" + e.getMessage() + ";";
log.error("error:" + e.getMessage()); log.error("error:" + e.getMessage());
} }
return message;
} }
private void checkPage() { private Boolean checkPage() {
boolean pageChange = !Objects.equals(currentUrl, driver.getCurrentUrl()); boolean pageChange = !Objects.equals(currentUrl, driver.getCurrentUrl());
if (pageChange) { if (pageChange) {
boolean newPage = !historyUrls.contains(driver.getCurrentUrl()); boolean newPage = !historyUrls.contains(driver.getCurrentUrl());
...@@ -111,6 +177,18 @@ public class SeleniumExecutor { ...@@ -111,6 +177,18 @@ public class SeleniumExecutor {
urlQueue.add(driver.getCurrentUrl()); urlQueue.add(driver.getCurrentUrl());
} }
reload(); reload();
return true;
} else {
return false;
}
}
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();
if (inputOptional.isPresent()) {
Input input = inputOptional.get();
element.sendKeys(input.getValue());
} }
} }
...@@ -120,15 +198,26 @@ public class SeleniumExecutor { ...@@ -120,15 +198,26 @@ public class SeleniumExecutor {
} }
} }
private Alert getAlert() {
WebDriverWait wait = new WebDriverWait(driver, 4);
return wait.until((ExpectedCondition<Alert>) theDriver -> {
try {
return theDriver.switchTo().alert();
} catch (NoAlertPresentException e) {
return null;
}
});
}
private void reload() { private void reload() {
driver.get(currentUrl); driver.get(currentUrl);
getAllElements(driver); getAllElements(driver);
traversal(); traversal();
} }
private void sleep(Long time) { private void sleep() {
try { try {
Thread.sleep(time); Thread.sleep(2000L);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
...@@ -141,16 +230,12 @@ public class SeleniumExecutor { ...@@ -141,16 +230,12 @@ public class SeleniumExecutor {
} }
private void getSubElements(WebElement element) { private void getSubElements(WebElement element) {
try {
List<WebElement> results = element.findElements(By.xpath("*")); List<WebElement> results = element.findElements(By.xpath("*"));
if (results.isEmpty()) { if (results.isEmpty()) {
elements.add(element); elements.add(element);
} else { } else {
results.forEach(this::getSubElements); results.forEach(this::getSubElements);
} }
} catch (StaleElementReferenceException e){
}
} }
private Boolean isEnabledInput(WebElement element) { private Boolean isEnabledInput(WebElement element) {
...@@ -166,15 +251,9 @@ public class SeleniumExecutor { ...@@ -166,15 +251,9 @@ public class SeleniumExecutor {
} }
// private void screenShot(String name) {
// Setter byte[] bytes = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
// FileUtil.output(bytes, FileUtil.WORK_PATH + "screenshots\\" + name + ".png");
public void setDriver(WebDriver driver) {
this.driver = driver;
}
public void setInputs(Map<String, String> inputs) {
this.inputs = inputs;
} }
} }
...@@ -14,7 +14,18 @@ import java.nio.charset.StandardCharsets; ...@@ -14,7 +14,18 @@ import java.nio.charset.StandardCharsets;
@Slf4j @Slf4j
public class FileUtil { public class FileUtil {
private final static String WORK_PATH = System.getProperty("user.dir") + "\\"; public final static String WORK_PATH = System.getProperty("user.dir") + "\\";
public static void output(byte[] bytes,String path) {
File file = new File(path);
try {
OutputStream output = new FileOutputStream(file);
output.write(bytes);
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void output(String text, OutputStream os) { public static void output(String text, OutputStream os) {
byte[] buffer = new byte[1024]; byte[] buffer = new byte[1024];
......
package com.zjty.autotest; package com.zjty.autotest;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.google.common.collect.Queues; import com.zjty.autotest.pojo.report.Measure;
import com.zjty.autotest.service.SeleniumExecutor; import com.zjty.autotest.pojo.report.Report;
import com.zjty.autotest.pojo.test.Project;
import com.zjty.autotest.service.impl.SeleniumExecutor;
import com.zjty.autotest.util.WebDriverUtil; import com.zjty.autotest.util.WebDriverUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.openqa.selenium.By; import org.openqa.selenium.*;
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebElement; import org.openqa.selenium.logging.LogType;
import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.logging.LoggingPreferences;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import java.util.*; import java.util.*;
import java.util.logging.Level;
@Slf4j
@SpringBootTest @SpringBootTest
class AutotestApplicationTests { class AutotestApplicationTests {
@Autowired @Autowired
SeleniumExecutor seleniumExecutor; SeleniumExecutor seleniumExecutor;
@Test @Test
public void execute() { public void execute() {
Map<String, String> inputs = Maps.newHashMap(); Map<String, String> inputs = Maps.newHashMap();
inputs.put("name", "root"); inputs.put("name", "root");
inputs.put("password", "root"); inputs.put("password", "root");
System.setProperty("webdriver.firefox.driver", WebDriverUtil.FIRE_FOX_EXE); System.setProperty("webdriver.firefox.driver", WebDriverUtil.FIRE_FOX_EXE);
FirefoxDriver driver = new FirefoxDriver(); DesiredCapabilities caps = DesiredCapabilities.chrome();
String url = "https://qxmugen.com/"; LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.BROWSER, Level.ALL);
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); driver.get(url);
seleniumExecutor.setDriver(driver); Project project = new Project(
seleniumExecutor.setInputs(inputs); null,
seleniumExecutor.execute(url); null,
// String currentwindowHandle; "chrome",
// Queue<String> windowHandles = Queues.newLinkedBlockingDeque(driver.getWindowHandles()); url,
// System.out.println(windowHandles); Lists.newArrayList()
// driver.findElement(By.xpath("/html/body/div[5]/div[3]/div[1]/div/div/div[2]/div/div[4]/a")).click(); );
// System.out.println("Current Url:"+driver.getCurrentUrl()); Report report = seleniumExecutor.execute(project);
// System.out.println("has Window:"+driver.getWindowHandles().size()); for (Measure measure : report.getMeasures()) {
// System.out.println(measure);
// }
// windowHandles = Queues.newLinkedBlockingDeque(driver.getWindowHandles());
// System.out.println(windowHandles);
//
// driver.findElement(By.xpath("/html/body/div[5]/div[3]/div[1]/div/div/div[2]/div/div[4]/a")).click();
//
// windowHandles = Queues.newLinkedBlockingDeque(driver.getWindowHandles());
// System.out.println(windowHandles);
} }
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论