-
-
Notifications
You must be signed in to change notification settings - Fork 669
/
DockerChromeCdpTest.java
102 lines (89 loc) · 3.63 KB
/
DockerChromeCdpTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
* (C) Copyright 2023 Boni Garcia (https://bonigarcia.github.io/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package io.github.bonigarcia.wdm.test.docker;
import static io.github.bonigarcia.wdm.WebDriverManager.isDockerAvailable;
import static java.lang.invoke.MethodHandles.lookup;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assumptions.assumeThat;
import static org.slf4j.LoggerFactory.getLogger;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.Base64;
import java.util.Optional;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.HasDevTools;
import org.openqa.selenium.devtools.v123.dom.model.Rect;
import org.openqa.selenium.devtools.v123.page.Page;
import org.openqa.selenium.devtools.v123.page.Page.GetLayoutMetricsResponse;
import org.openqa.selenium.devtools.v123.page.model.Viewport;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.slf4j.Logger;
import io.github.bonigarcia.wdm.WebDriverManager;
@Disabled
class DockerChromeCdpTest {
static final Logger log = getLogger(lookup().lookupClass());
WebDriver driver;
DevTools devTools;
WebDriverManager wdm = WebDriverManager.chromedriver().browserInDocker()
.dockerNetworkHost();
@BeforeEach
void setupTest() throws Exception {
assumeThat(isDockerAvailable()).isTrue();
driver = new Augmenter().augment(wdm.create());
devTools = ((HasDevTools) driver).getDevTools();
devTools.createSession();
}
@AfterEach
void teardown() {
if (devTools != null) {
devTools.close();
}
if (wdm != null) {
wdm.quit();
}
}
@Test
void test() throws Exception {
driver.get(
"https://bonigarcia.dev/selenium-webdriver-java/long-page.html");
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.presenceOfNestedElementsLocatedBy(
By.className("container"), By.tagName("p")));
GetLayoutMetricsResponse metrics = devTools
.send(Page.getLayoutMetrics());
Rect contentSize = metrics.getContentSize();
String screenshotBase64 = devTools
.send(Page.captureScreenshot(Optional.empty(), Optional.empty(),
Optional.of(new Viewport(0, 0, contentSize.getWidth(),
contentSize.getHeight(), 1)),
Optional.empty(), Optional.of(true),
Optional.of(false)));
Path destination = Paths.get("fullpage-screenshot.png");
Files.write(destination, Base64.getDecoder().decode(screenshotBase64));
assertThat(destination).exists();
}
}