提交 3065a019 authored 作者: 黄承天's avatar 黄承天

增加元素位置、截图、响应时间相关逻辑

上级 7c55db22
......@@ -4,11 +4,9 @@ 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.util.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.By;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.*;
import org.springframework.stereotype.Service;
import java.util.*;
......@@ -40,8 +38,7 @@ public class SeleniumExecutor {
private String currentUrl;
public List<String> execute(String url) {
driver.get(url);
currentUrl = driver.getCurrentUrl();
currentUrl = url;
currentWindow = driver.getWindowHandle();
while (nonNull(currentWindow)) {
driver.switchTo().window(currentWindow);
......@@ -59,8 +56,13 @@ public class SeleniumExecutor {
private void testUrl(String currentUrl) {
if (!historyUrls.contains(currentUrl)) {
historyUrls.add(driver.getCurrentUrl());
log.info("当前URL:{} 开始进行遍历...", currentUrl);
long startTime = System.currentTimeMillis();
driver.get(currentUrl);
long endTime = System.currentTimeMillis();
long costTime = endTime - startTime;
log.info("当前URL:{} 响应时间 {} ms 开始进行遍历...", currentUrl, costTime);
log.info("正在获取当前网页所有元素...");
getAllElements(driver);
log.info("获取完毕...共{}个元素...", elements.size());
elementIndex = 0;
......@@ -85,7 +87,7 @@ public class SeleniumExecutor {
try {
log.info("正在操作第{}个元素 ------ text:[{}] ----- tag:[{}]", elementIndex + 1, element.getText(), element.getTagName());
elementIndex++;
if (isEnabledAndDisplayed(element)){
if (isEnabledAndDisplayed(element)) {
if (isEnabledInput(element)) {
String id = element.getAttribute("id");
String inputValue = inputs.get(id);
......@@ -98,6 +100,8 @@ public class SeleniumExecutor {
sleep(2000L);
} catch (StaleElementReferenceException e) {
reload();
} catch (ElementNotInteractableException e) {
log.error("error:在可操作范围之外的元素" );
} catch (Exception e) {
log.error("error:" + e.getMessage());
}
......@@ -148,7 +152,7 @@ public class SeleniumExecutor {
} else {
results.forEach(this::getSubElements);
}
} catch (StaleElementReferenceException e){
} catch (StaleElementReferenceException e) {
}
}
......@@ -166,6 +170,11 @@ public class SeleniumExecutor {
}
private void screenShot(String name) {
byte[] bytes = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
FileUtil.output(bytes, FileUtil.WORK_PATH + name + ".png");
}
//
// Setter
//
......
......@@ -14,7 +14,18 @@ import java.nio.charset.StandardCharsets;
@Slf4j
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) {
byte[] buffer = new byte[1024];
......
......@@ -3,52 +3,50 @@ package com.zjty.autotest;
import com.google.common.collect.Maps;
import com.google.common.collect.Queues;
import com.zjty.autotest.service.SeleniumExecutor;
import com.zjty.autotest.util.FileUtil;
import com.zjty.autotest.util.WebDriverUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.logging.LogEntries;
import org.openqa.selenium.logging.LogEntry;
import org.openqa.selenium.logging.LogType;
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.boot.test.context.SpringBootTest;
import java.util.*;
import java.util.logging.Level;
@Slf4j
@SpringBootTest
class AutotestApplicationTests {
@Autowired
SeleniumExecutor seleniumExecutor;
@Test
public void execute() {
Map<String, String> inputs = Maps.newHashMap();
inputs.put("name", "root");
inputs.put("password", "root");
System.setProperty("webdriver.firefox.driver", WebDriverUtil.FIRE_FOX_EXE);
FirefoxDriver driver = new FirefoxDriver();
String url = "https://qxmugen.com/";
DesiredCapabilities caps = DesiredCapabilities.firefox();
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.BROWSER, Level.ALL);
caps.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
WebDriver driver = new FirefoxDriver();
String url = "file:///C:/Users/C/Documents/WeChat%20Files/c18042003295/FileStorage/File/2020-03/test1.html";
long startTime = System.currentTimeMillis();
driver.get(url);
seleniumExecutor.setDriver(driver);
seleniumExecutor.setInputs(inputs);
seleniumExecutor.execute(url);
// String currentwindowHandle;
// Queue<String> 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();
// System.out.println("Current Url:"+driver.getCurrentUrl());
// System.out.println("has Window:"+driver.getWindowHandles().size());
//
//
// 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);
long endTime = System.currentTimeMillis();
long costTime = endTime - startTime;
log.info("当前URL:{} 响应时间 {} ms 开始进行遍历...", url, costTime);
driver.findElement(By.xpath("/html/body/div/span")).click();
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论