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

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

上级 7c55db22
...@@ -4,11 +4,9 @@ import com.google.common.collect.Lists; ...@@ -4,11 +4,9 @@ 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.google.common.collect.Queues;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.zjty.autotest.util.FileUtil;
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.WebDriver;
import org.openqa.selenium.WebElement;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.*; import java.util.*;
...@@ -40,8 +38,7 @@ public class SeleniumExecutor { ...@@ -40,8 +38,7 @@ public class SeleniumExecutor {
private String currentUrl; private String currentUrl;
public List<String> execute(String url) { public List<String> execute(String url) {
driver.get(url); currentUrl = url;
currentUrl = driver.getCurrentUrl();
currentWindow = driver.getWindowHandle(); currentWindow = driver.getWindowHandle();
while (nonNull(currentWindow)) { while (nonNull(currentWindow)) {
driver.switchTo().window(currentWindow); driver.switchTo().window(currentWindow);
...@@ -59,8 +56,13 @@ public class SeleniumExecutor { ...@@ -59,8 +56,13 @@ public class SeleniumExecutor {
private void testUrl(String currentUrl) { private void testUrl(String currentUrl) {
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 costTime = endTime - startTime;
log.info("当前URL:{} 响应时间 {} ms 开始进行遍历...", currentUrl, costTime);
log.info("正在获取当前网页所有元素..."); log.info("正在获取当前网页所有元素...");
getAllElements(driver); getAllElements(driver);
log.info("获取完毕...共{}个元素...", elements.size()); log.info("获取完毕...共{}个元素...", elements.size());
elementIndex = 0; elementIndex = 0;
...@@ -85,7 +87,7 @@ public class SeleniumExecutor { ...@@ -85,7 +87,7 @@ public class SeleniumExecutor {
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"); String id = element.getAttribute("id");
String inputValue = inputs.get(id); String inputValue = inputs.get(id);
...@@ -98,6 +100,8 @@ public class SeleniumExecutor { ...@@ -98,6 +100,8 @@ public class SeleniumExecutor {
sleep(2000L); sleep(2000L);
} catch (StaleElementReferenceException e) { } catch (StaleElementReferenceException e) {
reload(); reload();
} catch (ElementNotInteractableException e) {
log.error("error:在可操作范围之外的元素" );
} catch (Exception e) { } catch (Exception e) {
log.error("error:" + e.getMessage()); log.error("error:" + e.getMessage());
} }
...@@ -148,7 +152,7 @@ public class SeleniumExecutor { ...@@ -148,7 +152,7 @@ public class SeleniumExecutor {
} else { } else {
results.forEach(this::getSubElements); results.forEach(this::getSubElements);
} }
} catch (StaleElementReferenceException e){ } catch (StaleElementReferenceException e) {
} }
} }
...@@ -166,6 +170,11 @@ public class SeleniumExecutor { ...@@ -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 // Setter
// //
......
...@@ -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];
......
...@@ -3,52 +3,50 @@ package com.zjty.autotest; ...@@ -3,52 +3,50 @@ package com.zjty.autotest;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.google.common.collect.Queues; import com.google.common.collect.Queues;
import com.zjty.autotest.service.SeleniumExecutor; import com.zjty.autotest.service.SeleniumExecutor;
import com.zjty.autotest.util.FileUtil;
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.firefox.FirefoxDriver; 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.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.firefox();
String url = "https://qxmugen.com/"; 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); driver.get(url);
seleniumExecutor.setDriver(driver); long endTime = System.currentTimeMillis();
seleniumExecutor.setInputs(inputs); long costTime = endTime - startTime;
seleniumExecutor.execute(url); log.info("当前URL:{} 响应时间 {} ms 开始进行遍历...", url, costTime);
// String currentwindowHandle; driver.findElement(By.xpath("/html/body/div/span")).click();
// 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);
} }
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论