-
-
Notifications
You must be signed in to change notification settings - Fork 8k
/
DistributedCdpTest.java
111 lines (93 loc) · 4.13 KB
/
DistributedCdpTest.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
103
104
105
106
107
108
109
110
111
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you 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 org.openqa.selenium.grid.router;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assumptions.assumeThat;
import com.google.common.collect.ImmutableMap;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.HasDevTools;
import org.openqa.selenium.devtools.idealized.Network;
import org.openqa.selenium.grid.config.MapConfig;
import org.openqa.selenium.grid.config.TomlConfig;
import org.openqa.selenium.grid.router.DeploymentTypes.Deployment;
import org.openqa.selenium.grid.server.BaseServerOptions;
import org.openqa.selenium.grid.server.Server;
import org.openqa.selenium.netty.server.NettyServer;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.http.Contents;
import org.openqa.selenium.remote.http.HttpResponse;
import org.openqa.selenium.testing.drivers.Browser;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
public class DistributedCdpTest {
@BeforeAll
public static void ensureBrowserIsCdpEnabled() {
Browser browser = Objects.requireNonNull(Browser.detect());
assumeThat(browser.supportsCdp()).isTrue();
}
@Test
public void ensureBasicFunctionality() {
Browser browser = Browser.detect();
Deployment deployment = DeploymentTypes.DISTRIBUTED.start(
browser.getCapabilities(),
new TomlConfig(new StringReader(
"[node]\n" +
"driver-implementation = " + browser.displayName())));
Server<?> server = new NettyServer(
new BaseServerOptions(new MapConfig(ImmutableMap.of())),
req -> new HttpResponse().setContent(Contents.utf8String(
"<script>document.write(navigator.userAgent);</script><body><h1 id=header>Cheese</h1>")))
.start();
WebDriver driver = new RemoteWebDriver(
deployment.getServer().getUrl(), addBrowserPath(browser.getCapabilities()));
driver = new Augmenter().augment(driver);
try (DevTools devTools = ((HasDevTools) driver).getDevTools()) {
devTools.createSessionIfThereIsNotOne(driver.getWindowHandle());
Network<?, ?> network = devTools.getDomains().network();
network.setUserAgent("Cheese-Browser 4000");
driver.get(server.getUrl().toString());
String body = driver.findElement(By.tagName("body")).getText();
assertThat(body).contains("Cheese-Browser 4000");
}
}
private Capabilities addBrowserPath(Capabilities caps) {
if (Browser.detect() == Browser.CHROME) {
String binary = System.getProperty("webdriver.chrome.binary");
if (binary == null) {
return caps;
}
MutableCapabilities newCaps = new MutableCapabilities(caps);
@SuppressWarnings("unchecked")
Map<String, Object> rawOptions = (Map<String, Object>) newCaps.getCapability(ChromeOptions.CAPABILITY);
HashMap<String, Object> googOptions = new HashMap<>(rawOptions);
googOptions.put("binary", binary);
newCaps.setCapability(ChromeOptions.CAPABILITY, googOptions);
return newCaps;
}
return caps;
}
}