Skip to content

Commit 7113b47

Browse files
committed
Rework the RemoteWebDriverBuilder
This version is more robust than the previous one, and delegates to existing code where possible. It allows us to set the HTTP `ClientConfig` with ease, add metadata to the outgoing new session request, and makes sure we add the headers we need where possible.
1 parent 2118f4a commit 7113b47

21 files changed

+819
-725
lines changed

java/server/src/org/openqa/selenium/grid/web/AddWebDriverSpecHeaders.java renamed to java/client/src/org/openqa/selenium/remote/AddWebDriverSpecHeaders.java

+6-11
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
package org.openqa.selenium.grid.web;
18+
package org.openqa.selenium.remote;
1919

2020
import org.openqa.selenium.json.Json;
2121
import org.openqa.selenium.remote.http.Filter;
@@ -26,19 +26,14 @@ public class AddWebDriverSpecHeaders implements Filter {
2626
@Override
2727
public HttpHandler apply(HttpHandler next) {
2828
return req -> {
29-
HttpResponse res = next.execute(req);
30-
if (res == null) {
31-
return res;
29+
if (req.getHeader("Content-Type") == null) {
30+
req.addHeader("Content-Type", Json.JSON_UTF_8);
3231
}
33-
34-
if (res.getHeader("Content-Type") == null) {
35-
res.addHeader("Content-Type", Json.JSON_UTF_8);
36-
}
37-
if (res.getHeader("Cache-Control") == null) {
38-
res.addHeader("Cache-Control", "none");
32+
if (req.getHeader("Cache-Control") == null) {
33+
req.addHeader("Cache-Control", "no-cache");
3934
}
4035

41-
return res;
36+
return next.execute(req);
4237
};
4338
}
4439
}

java/client/src/org/openqa/selenium/remote/BUILD.bazel

+18-9
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,7 @@ java_library(
4848
java_export(
4949
name = "remote",
5050
srcs = [
51-
"AddRotatable.java",
52-
"Augmenter.java",
53-
"AugmenterProvider.java",
54-
"RemoteRotatable.java",
5551
"RemoteTags.java",
56-
"html5/AddApplicationCache.java",
57-
"html5/AddLocationContext.java",
58-
"html5/AddWebStorage.java",
59-
"mobile/AddNetworkConnection.java",
6052
],
6153
hides = [
6254
"org.openqa.selenium.remote.internal",
@@ -83,7 +75,7 @@ java_export(
8375
"//java/client/src/org/openqa/selenium/remote/tracing",
8476
"//java/client/src/org/openqa/selenium/remote/tracing/opentelemetry",
8577
],
86-
deps = [
78+
runtime_deps = [
8779
":api",
8880
":capabilities",
8981
":remote-lib",
@@ -96,17 +88,28 @@ java_export(
9688
artifact("com.google.guava:guava"),
9789
artifact("net.bytebuddy:byte-buddy"),
9890
],
91+
deps = [
92+
"//java/client/src/org/openqa/selenium:core",
93+
"//java/client/src/org/openqa/selenium/remote:api",
94+
"//java/client/src/org/openqa/selenium/remote/tracing",
95+
],
9996
)
10097

10198
java_library(
10299
name = "remote-lib",
103100
srcs = [
104101
"AbstractDriverOptions.java",
105102
"AcceptedW3CCapabilityKeys.java",
103+
"AddWebDriverSpecHeaders.java",
104+
"AddRotatable.java",
105+
"Augmenter.java",
106+
"AugmenterProvider.java",
106107
"CommandCodec.java",
107108
"CommandInfo.java",
108109
"Dialect.java",
109110
"DriverCommand.java",
111+
"ErrorCodec.java",
112+
"ErrorFilter.java",
110113
"ErrorHandler.java",
111114
"ExecuteMethod.java",
112115
"FileDetector.java",
@@ -122,6 +125,7 @@ java_library(
122125
"RemoteKeyboard.java",
123126
"RemoteLogs.java",
124127
"RemoteMouse.java",
128+
"RemoteRotatable.java",
125129
"RemoteStatus.java",
126130
"RemoteTouchScreen.java",
127131
"RemoteWebDriver.java",
@@ -131,11 +135,15 @@ java_library(
131135
"UnreachableBrowserException.java",
132136
"UselessFileDetector.java",
133137
"W3CHandshakeResponse.java",
138+
"html5/AddApplicationCache.java",
139+
"html5/AddLocationContext.java",
140+
"html5/AddWebStorage.java",
134141
"html5/RemoteApplicationCache.java",
135142
"html5/RemoteLocalStorage.java",
136143
"html5/RemoteLocationContext.java",
137144
"html5/RemoteSessionStorage.java",
138145
"html5/RemoteWebStorage.java",
146+
"mobile/AddNetworkConnection.java",
139147
"mobile/RemoteNetworkConnection.java",
140148
] + glob([
141149
"codec/*.java",
@@ -159,6 +167,7 @@ java_library(
159167
"//java/client/src/org/openqa/selenium/remote/http",
160168
"//java/client/src/org/openqa/selenium/remote/session",
161169
artifact("com.google.guava:guava"),
170+
artifact("net.bytebuddy:byte-buddy"),
162171
],
163172
)
164173

java/server/src/org/openqa/selenium/grid/web/ErrorCodec.java renamed to java/client/src/org/openqa/selenium/remote/ErrorCodec.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
package org.openqa.selenium.grid.web;
18+
package org.openqa.selenium.remote;
1919

2020
import com.google.common.base.Throwables;
2121
import com.google.common.collect.ImmutableMap;
@@ -47,7 +47,6 @@
4747
import org.openqa.selenium.interactions.InvalidCoordinatesException;
4848
import org.openqa.selenium.interactions.MoveTargetOutOfBoundsException;
4949
import org.openqa.selenium.internal.Require;
50-
import org.openqa.selenium.remote.ScreenshotException;
5150

5251
import java.lang.reflect.Constructor;
5352
import java.util.Map;

java/server/src/org/openqa/selenium/grid/web/ErrorFilter.java renamed to java/client/src/org/openqa/selenium/remote/ErrorFilter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
package org.openqa.selenium.grid.web;
18+
package org.openqa.selenium.remote;
1919

2020
import org.openqa.selenium.internal.Require;
2121
import org.openqa.selenium.json.Json;

java/client/src/org/openqa/selenium/remote/ProtocolHandshake.java

+11-13
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,8 @@
1717

1818
package org.openqa.selenium.remote;
1919

20-
import static com.google.common.net.HttpHeaders.CONTENT_LENGTH;
21-
import static com.google.common.net.HttpHeaders.CONTENT_TYPE;
22-
import static com.google.common.net.MediaType.JSON_UTF_8;
23-
import static java.nio.charset.StandardCharsets.UTF_8;
24-
import static org.openqa.selenium.remote.CapabilityType.PROXY;
25-
import static org.openqa.selenium.remote.http.Contents.string;
26-
2720
import com.google.common.io.CountingOutputStream;
2821
import com.google.common.io.FileBackedOutputStream;
29-
3022
import org.openqa.selenium.Capabilities;
3123
import org.openqa.selenium.ImmutableCapabilities;
3224
import org.openqa.selenium.Proxy;
@@ -35,7 +27,7 @@
3527
import org.openqa.selenium.internal.Require;
3628
import org.openqa.selenium.json.Json;
3729
import org.openqa.selenium.json.JsonException;
38-
import org.openqa.selenium.remote.http.HttpClient;
30+
import org.openqa.selenium.remote.http.HttpHandler;
3931
import org.openqa.selenium.remote.http.HttpMethod;
4032
import org.openqa.selenium.remote.http.HttpRequest;
4133
import org.openqa.selenium.remote.http.HttpResponse;
@@ -52,12 +44,18 @@
5244
import java.util.logging.Logger;
5345
import java.util.stream.Stream;
5446

47+
import static com.google.common.net.HttpHeaders.CONTENT_LENGTH;
48+
import static com.google.common.net.HttpHeaders.CONTENT_TYPE;
49+
import static java.nio.charset.StandardCharsets.UTF_8;
50+
import static org.openqa.selenium.json.Json.JSON_UTF_8;
51+
import static org.openqa.selenium.remote.CapabilityType.PROXY;
52+
import static org.openqa.selenium.remote.http.Contents.string;
53+
5554
public class ProtocolHandshake {
5655

5756
private static final Logger LOG = Logger.getLogger(ProtocolHandshake.class.getName());
5857

59-
public Result createSession(HttpClient client, Command command)
60-
throws IOException {
58+
public Result createSession(HttpHandler client, Command command) throws IOException {
6159
Capabilities desired = (Capabilities) command.getParameters().get("desiredCapabilities");
6260
desired = desired == null ? new ImmutableCapabilities() : desired;
6361

@@ -91,15 +89,15 @@ public Result createSession(HttpClient client, Command command)
9189
desired));
9290
}
9391

94-
private Optional<Result> createSession(HttpClient client, InputStream newSessionBlob, long size) {
92+
Optional<Result> createSession(HttpHandler client, InputStream newSessionBlob, long size) {
9593
// Create the http request and send it
9694
HttpRequest request = new HttpRequest(HttpMethod.POST, "/session");
9795

9896
HttpResponse response;
9997
long start = System.currentTimeMillis();
10098

10199
request.setHeader(CONTENT_LENGTH, String.valueOf(size));
102-
request.setHeader(CONTENT_TYPE, JSON_UTF_8.toString());
100+
request.setHeader(CONTENT_TYPE, JSON_UTF_8);
103101
request.setContent(() -> newSessionBlob);
104102

105103
response = client.execute(request);

java/client/src/org/openqa/selenium/remote/RemoteWebDriver.java

+27-25
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,8 @@
1717

1818
package org.openqa.selenium.remote;
1919

20-
import static java.nio.charset.StandardCharsets.UTF_8;
21-
import static java.util.concurrent.TimeUnit.SECONDS;
22-
import static org.openqa.selenium.remote.CapabilityType.LOGGING_PREFS;
23-
import static org.openqa.selenium.remote.CapabilityType.PLATFORM;
24-
import static org.openqa.selenium.remote.CapabilityType.PLATFORM_NAME;
25-
import static org.openqa.selenium.remote.CapabilityType.SUPPORTS_JAVASCRIPT;
26-
2720
import com.google.common.collect.ImmutableMap;
2821
import com.google.common.collect.ImmutableSet;
29-
import java.net.URL;
30-
import java.util.Base64;
31-
import java.time.Duration;
32-
import java.util.Collection;
33-
import java.util.Collections;
34-
import java.util.Date;
35-
import java.util.HashSet;
36-
import java.util.LinkedHashSet;
37-
import java.util.List;
38-
import java.util.Map;
39-
import java.util.Set;
40-
import java.util.concurrent.TimeUnit;
41-
import java.util.logging.Level;
42-
import java.util.logging.Logger;
43-
import java.util.stream.Collectors;
44-
import java.util.stream.Stream;
4522
import org.openqa.selenium.Alert;
4623
import org.openqa.selenium.Beta;
4724
import org.openqa.selenium.By;
@@ -83,6 +60,30 @@
8360
import org.openqa.selenium.virtualauthenticator.VirtualAuthenticator;
8461
import org.openqa.selenium.virtualauthenticator.VirtualAuthenticatorOptions;
8562

63+
import java.net.URL;
64+
import java.time.Duration;
65+
import java.util.Base64;
66+
import java.util.Collection;
67+
import java.util.Collections;
68+
import java.util.Date;
69+
import java.util.HashSet;
70+
import java.util.LinkedHashSet;
71+
import java.util.List;
72+
import java.util.Map;
73+
import java.util.Set;
74+
import java.util.concurrent.TimeUnit;
75+
import java.util.logging.Level;
76+
import java.util.logging.Logger;
77+
import java.util.stream.Collectors;
78+
import java.util.stream.Stream;
79+
80+
import static java.nio.charset.StandardCharsets.UTF_8;
81+
import static java.util.concurrent.TimeUnit.SECONDS;
82+
import static org.openqa.selenium.remote.CapabilityType.LOGGING_PREFS;
83+
import static org.openqa.selenium.remote.CapabilityType.PLATFORM;
84+
import static org.openqa.selenium.remote.CapabilityType.PLATFORM_NAME;
85+
import static org.openqa.selenium.remote.CapabilityType.SUPPORTS_JAVASCRIPT;
86+
8687
@Augmentable
8788
public class RemoteWebDriver implements WebDriver, JavascriptExecutor, HasInputDevices,
8889
HasCapabilities, Interactive, TakesScreenshot,
@@ -231,8 +232,9 @@ protected void startSession(Capabilities capabilities) {
231232

232233
Map<String, Object> rawCapabilities = (Map<String, Object>) responseValue;
233234
MutableCapabilities returnedCapabilities = new MutableCapabilities(rawCapabilities);
234-
String platformString = (String) rawCapabilities.getOrDefault(PLATFORM,
235-
rawCapabilities.get(PLATFORM_NAME));
235+
String platformString = (String) rawCapabilities.getOrDefault(
236+
PLATFORM,
237+
rawCapabilities.get(PLATFORM_NAME));
236238
Platform platform;
237239
try {
238240
if (platformString == null || "".equals(platformString)) {

0 commit comments

Comments
 (0)