|
@@ -1,17 +1,22 @@
|
1
|
1
|
package com.shigongli.common;
|
2
|
2
|
|
3
|
3
|
import com.shigongli.config.ScreenShotConfig;
|
|
4
|
+import lombok.extern.slf4j.Slf4j;
|
|
5
|
+import org.checkerframework.checker.nullness.qual.Nullable;
|
4
|
6
|
import org.openqa.selenium.*;
|
5
|
7
|
import org.openqa.selenium.chrome.ChromeDriver;
|
6
|
8
|
import org.openqa.selenium.chrome.ChromeOptions;
|
|
9
|
+//import org.openqa.selenium.devtools.DevTools;
|
|
10
|
+//import org.openqa.selenium.devtools.network.Network;
|
7
|
11
|
import org.openqa.selenium.support.ui.ExpectedCondition;
|
8
|
12
|
import org.openqa.selenium.support.ui.WebDriverWait;
|
9
|
13
|
import org.springframework.beans.factory.annotation.Autowired;
|
10
|
14
|
import org.springframework.stereotype.Component;
|
11
|
15
|
|
12
|
16
|
import java.io.File;
|
13
|
|
-import java.util.concurrent.TimeUnit;
|
|
17
|
+//import java.util.Optional;
|
14
|
18
|
|
|
19
|
+@Slf4j
|
15
|
20
|
@Component
|
16
|
21
|
public class ScreenShotUtil {
|
17
|
22
|
@Autowired
|
|
@@ -20,11 +25,11 @@ public class ScreenShotUtil {
|
20
|
25
|
@Autowired
|
21
|
26
|
ScreenShotConfig config;
|
22
|
27
|
|
23
|
|
- public String shot(String url, String cssSelector) throws Exception {
|
|
28
|
+ public String shot(String url) throws Exception {
|
24
|
29
|
//驱动地址
|
25
|
30
|
System.setProperty("webdriver.chrome.driver", config.getDriver());
|
26
|
31
|
|
27
|
|
- WebDriver webDriver = null;
|
|
32
|
+ WebDriver driver = null;
|
28
|
33
|
ChromeOptions options=new ChromeOptions();
|
29
|
34
|
//设置 chrome 的无头模式
|
30
|
35
|
options.addArguments("--headless");
|
|
@@ -32,35 +37,59 @@ public class ScreenShotUtil {
|
32
|
37
|
options.addArguments("--no-sandbox");
|
33
|
38
|
options.addArguments("--disable-dev-shm-usage");
|
34
|
39
|
options.addArguments("--start-maximized");
|
|
40
|
+ options.addArguments("--disable-application-cache");
|
35
|
41
|
options.addArguments("--lang=zh");
|
36
|
42
|
options.addArguments(String.format("--window-size=%s,%s", config.getWidth(), config.getHeight()));
|
37
|
43
|
//启动一个 chrome 实例
|
38
|
|
- webDriver = new ChromeDriver(options);
|
|
44
|
+ driver = new ChromeDriver(options);
|
39
|
45
|
|
40
|
46
|
//访问网址
|
41
|
|
- webDriver.get(url);
|
|
47
|
+ log.info(String.format("访问网址: %s", url));
|
|
48
|
+// webDriver.get(url);
|
|
49
|
+
|
|
50
|
+// DevTools devTools = driver.getDevTools();
|
|
51
|
+// devTools.createSession();
|
|
52
|
+// devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.of(100000000)));
|
|
53
|
+// devTools.send(Network.setCacheDisabled(true));
|
|
54
|
+// devTools.addListener(Network.responseReceived(), responseReceived -> Assert.assertEquals(false, responseReceived.getResponse().getFromDiskCache()));
|
|
55
|
+
|
|
56
|
+ driver.get(url);
|
|
57
|
+
|
|
58
|
+// devTools.send(Network.clearBrowserCache());
|
42
|
59
|
|
43
|
60
|
// 设置超时等待
|
44
|
|
- WebDriverWait webDriverWait = new WebDriverWait(webDriver, config.getWait() / 1000);
|
45
|
|
- String finalCssSelector = null == cssSelector ? "body" : cssSelector;
|
46
|
|
- webDriverWait.until(new ExpectedCondition<WebElement>() {
|
47
|
|
- @Override
|
48
|
|
- public WebElement apply(WebDriver driver) {
|
49
|
|
- // 通过定位页面元素来确定页面确实正常加载了
|
50
|
|
- return driver.findElement(By.cssSelector(finalCssSelector));
|
51
|
|
- }
|
52
|
|
- });
|
|
61
|
+ try {
|
|
62
|
+ WebDriverWait webDriverWait = new WebDriverWait(driver, config.getWait() / 1000, 200);
|
|
63
|
+ webDriverWait.until(new ExpectedCondition<WebElement>() {
|
|
64
|
+ public @Nullable WebElement apply(@Nullable WebDriver driver) {
|
|
65
|
+ WebElement el = null;
|
|
66
|
+ try {
|
|
67
|
+ WebDriver wf = driver.switchTo().frame(0);
|
|
68
|
+ log.info("Got Web Iframe {}", wf);
|
|
69
|
+ el = wf.findElement(By.cssSelector("body"));
|
|
70
|
+ log.info("Got Web Element {}", el);
|
|
71
|
+ } catch (Exception e) {
|
|
72
|
+ e.printStackTrace();
|
|
73
|
+ }
|
|
74
|
+ return el;
|
|
75
|
+ }
|
|
76
|
+ });
|
|
77
|
+ } catch (Exception e) {
|
|
78
|
+ driver.quit();
|
|
79
|
+ throw e;
|
|
80
|
+ }
|
53
|
81
|
|
54
|
82
|
// webDriver.manage().timeouts().implicitlyWait(config.getWait(), TimeUnit.MILLISECONDS);
|
55
|
83
|
|
56
|
84
|
//截取全屏
|
57
|
|
- File f = ((TakesScreenshot)webDriver).getScreenshotAs(OutputType.FILE);
|
|
85
|
+ File f = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
|
58
|
86
|
|
59
|
87
|
// 存入 oss
|
60
|
88
|
String imageUrl = ossUtils.putObject(f);
|
|
89
|
+ log.info("Got image {}", imageUrl);
|
61
|
90
|
|
62
|
91
|
//退出
|
63
|
|
- webDriver.quit();
|
|
92
|
+ driver.quit();
|
64
|
93
|
|
65
|
94
|
return imageUrl;
|
66
|
95
|
}
|