forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RemoteWebDriverBuilder.java
533 lines (459 loc) · 18.2 KB
/
RemoteWebDriverBuilder.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
// 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.remote;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableSet;
import org.openqa.selenium.Beta;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.Credentials;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.SessionNotCreatedException;
import org.openqa.selenium.UsernameAndPassword;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverInfo;
import org.openqa.selenium.internal.Either;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.remote.http.ClientConfig;
import org.openqa.selenium.remote.http.DumpHttpExchangeFilter;
import org.openqa.selenium.remote.http.Filter;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.http.HttpHandler;
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;
import org.openqa.selenium.remote.service.DriverService;
import java.io.Closeable;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.TreeMap;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import static java.util.logging.Level.WARNING;
import static org.openqa.selenium.internal.Debug.getDebugLogLevel;
import static org.openqa.selenium.remote.DriverCommand.QUIT;
import static org.openqa.selenium.remote.http.HttpMethod.DELETE;
/**
* Create a new Selenium session using the W3C WebDriver protocol. This class will not generate any
* data expected by the original JSON Wire Protocol, so will fail to create sessions as expected if
* used against a server that only implements that protocol.
* <p>
* Expected usage is something like:
* <pre>
* WebDriver driver = RemoteWebDriver.builder()
* .addAlternative(new FirefoxOptions())
* .addAlternative(new ChromeOptions())
* .addMetadata("cloud:key", "hunter2")
* .setCapability("proxy", new Proxy())
* .build();
* </pre>
* In this example, we ask for a session where the browser will be either Firefox or Chrome (we
* don't care which), but where either browser will use the given {@link org.openqa.selenium.Proxy}.
* In addition, we've added some metadata to the session, setting the "{@code cloud.key}" to be the
* secret passphrase of our account with the cloud "Selenium as a Service" provider.
* <p>
* If no call to {@link #withDriverService(DriverService)} or {@link #address(URI)} is made, the
* builder will use {@link ServiceLoader} to find all instances of {@link WebDriverInfo} and will
* call {@link WebDriverInfo#createDriver(Capabilities)} for the first supported set of
* capabilities.
*/
@Beta
public class RemoteWebDriverBuilder {
private static final Logger LOG = Logger.getLogger(RemoteWebDriverBuilder.class.getName());
private static final Set<String> ILLEGAL_METADATA_KEYS = ImmutableSet.of(
"alwaysMatch",
"capabilities",
"desiredCapabilities",
"firstMatch");
private final List<Capabilities> requestedCapabilities = new ArrayList<>();
private final Map<String, Object> additionalCapabilities = new TreeMap<>();
private final Map<String, Object> metadata = new TreeMap<>();
private Function<ClientConfig, HttpHandler> handlerFactory =
config -> {
HttpClient.Factory factory = HttpClient.Factory.createDefault();
HttpClient client = factory.createClient(config);
return client.with(
next -> req -> {
try {
return client.execute(req);
} finally {
if (req.getMethod() == DELETE) {
HttpSessionId.getSessionId(req.getUri()).ifPresent(id -> {
if (("/session/" + id).equals(req.getUri())) {
try {
client.close();
} catch (UncheckedIOException e) {
LOG.log(WARNING, "Swallowing exception while closing http client", e);
}
factory.cleanupIdleClients();
}
});
}
}
});
};
private ClientConfig clientConfig = ClientConfig.defaultConfig();
private URI remoteHost = null;
private DriverService driverService;
private Credentials credentials = null;
private boolean useCustomConfig;
private Augmenter augmenter = new Augmenter();
RemoteWebDriverBuilder() {
// Access through RemoteWebDriver.builder
}
/**
* Clears the current set of alternative browsers and instead sets the list of possible choices to
* the arguments given to this method.
*/
public RemoteWebDriverBuilder oneOf(Capabilities maybeThis, Capabilities... orOneOfThese) {
Require.nonNull("Capabilities to use", maybeThis);
if (!requestedCapabilities.isEmpty()) {
LOG.log(getDebugLogLevel(), "Removing existing requested capabilities: " + requestedCapabilities);
requestedCapabilities.clear();
}
addAlternative(maybeThis);
for (Capabilities caps : orOneOfThese) {
Require.nonNull("Capabilities to use", caps);
addAlternative(caps);
}
return this;
}
/**
* Add to the list of possible configurations that might be asked for. It is possible to ask for
* more than one type of browser per session. For example, perhaps you have an extension that is
* available for two different kinds of browser, and you'd like to test it).
*/
public RemoteWebDriverBuilder addAlternative(Capabilities options) {
Require.nonNull("Capabilities to use", options);
requestedCapabilities.add(new ImmutableCapabilities(options));
return this;
}
/**
* Adds metadata to the outgoing new session request, which can be used by intermediary of end
* nodes for any purpose they choose (commonly, this is used to request additional features from
* cloud providers, such as video recordings or to set the timezone or screen size). Neither
* parameter can be {@code null}.
*/
public RemoteWebDriverBuilder addMetadata(String key, Object value) {
Require.nonNull("Metadata key", key);
Require.nonNull("Metadata value", value);
if (ILLEGAL_METADATA_KEYS.contains(key)) {
throw new IllegalArgumentException(String.format("Cannot add %s as metadata key", key));
}
Object previous = metadata.put(key, value);
if (previous != null) {
LOG.log(
getDebugLogLevel(),
String.format("Overwriting metadata %s. Previous value %s, new value %s", key, previous, value));
}
return this;
}
/**
* Sets a capability for every single alternative when the session is created. These capabilities
* are only set once the session is created, so this will be set on capabilities added via
* {@link #addAlternative(Capabilities)} or {@link #oneOf(Capabilities, Capabilities...)} even
* after this method call.
*/
public RemoteWebDriverBuilder setCapability(String capabilityName, Object value) {
Require.nonNull("Capability name", capabilityName);
Require.nonNull("Capability value", value);
Object previous = additionalCapabilities.put(capabilityName, value);
if (previous != null) {
LOG.log(
getDebugLogLevel(),
() -> String.format("Overwriting capability %s. Previous value %s, new value %s",
capabilityName, previous, value));
}
return this;
}
/**
* @see #address(URI)
*/
public RemoteWebDriverBuilder address(String uri) {
Require.nonNull("Address", uri);
try {
return address(new URI(uri));
} catch (URISyntaxException e) {
throw new IllegalArgumentException("Unable to create URI from " + uri);
}
}
/**
* @see #address(URI)
*/
public RemoteWebDriverBuilder address(URL url) {
Require.nonNull("Address", url);
try {
return address(url.toURI());
} catch (URISyntaxException e) {
throw new IllegalArgumentException("Unable to create URI from " + url);
}
}
/**
* Set the URI of the remote server. If this URI is not set, then it assumed that a local running
* remote webdriver session is needed. It is an error to call this method and also
* {@link #withDriverService(DriverService)}.
*/
public RemoteWebDriverBuilder address(URI uri) {
Require.nonNull("URI", uri);
if (driverService != null || (clientConfig.baseUri() != null && !clientConfig.baseUri().equals(uri))) {
throw new IllegalArgumentException(
"Attempted to set the base uri on both this builder and the http client config. " +
"Please set in only one place. " + uri);
}
remoteHost = uri;
return this;
}
public RemoteWebDriverBuilder authenticateAs(UsernameAndPassword usernameAndPassword) {
Require.nonNull("User name and password", usernameAndPassword);
this.credentials = usernameAndPassword;
return this;
}
/**
* Allows precise control of the {@link ClientConfig} to use with remote
* instances. If {@link ClientConfig#baseUri(URI)} has been called, then
* that will be used as the base URI for the session.
*/
public RemoteWebDriverBuilder config(ClientConfig config) {
Require.nonNull("HTTP client config", config);
if (config.baseUri() != null) {
if (remoteHost != null || driverService != null) {
throw new IllegalArgumentException("Base URI has already been set. Cannot also set it via client config");
}
}
this.clientConfig = config;
this.useCustomConfig = true;
return this;
}
/**
* Use the given {@link DriverService} to set up the webdriver instance. It is an error to set
* both this and also call {@link #address(URI)}.
*/
public RemoteWebDriverBuilder withDriverService(DriverService service) {
Require.nonNull("Driver service", service);
if (clientConfig.baseUri() != null || remoteHost != null) {
throw new IllegalArgumentException("Base URI has already been set. Cannot also set driver service.");
}
this.driverService = service;
return this;
}
@VisibleForTesting
RemoteWebDriverBuilder connectingWith(Function<ClientConfig, HttpHandler> handlerFactory) {
Require.nonNull("Handler factory", handlerFactory);
this.handlerFactory = handlerFactory;
return this;
}
/**
* @param augmenter The {@link Augmenter} to use when creating the {@link WebDriver} instance.
*/
public RemoteWebDriverBuilder augmentUsing(Augmenter augmenter) {
Require.nonNull("Augmenter", augmenter);
this.augmenter = augmenter;
return this;
}
@VisibleForTesting
WebDriver getLocalDriver() {
if (remoteHost != null || clientConfig.baseUri() != null || driverService != null) {
return null;
}
Set<WebDriverInfo> infos = StreamSupport.stream(
ServiceLoader.load(WebDriverInfo.class).spliterator(),
false)
.filter(WebDriverInfo::isAvailable)
.collect(Collectors.toSet());
Capabilities additional = new ImmutableCapabilities(additionalCapabilities);
Optional<Supplier<WebDriver>> first = requestedCapabilities.stream()
.map(caps -> caps.merge(additional))
.flatMap(caps ->
infos.stream()
.filter(WebDriverInfo::isAvailable)
.filter(info -> info.isSupporting(caps))
.map(info -> (Supplier<WebDriver>) () -> info.createDriver(caps)
.orElseThrow(() -> new SessionNotCreatedException("Unable to create session with " + caps))))
.findFirst();
if (!first.isPresent()) {
throw new SessionNotCreatedException("Unable to find matching driver for capabilities");
}
WebDriver localDriver = first.get().get();
if (localDriver != null && this.useCustomConfig) {
throw new IllegalArgumentException("ClientConfig instances do not work for Local Drivers");
}
return localDriver;
}
/**
* Actually create a new WebDriver session. The returned webdriver is not guaranteed to be a
* {@link RemoteWebDriver}.
*/
public WebDriver build() {
if (requestedCapabilities.isEmpty() && additionalCapabilities.isEmpty()) {
throw new SessionNotCreatedException("One set of browser options must be specified");
}
Set<String> clobberedCapabilities = getClobberedCapabilities();
if (!clobberedCapabilities.isEmpty()) {
throw new IllegalArgumentException(String.format(
"Unable to create session. Additional capabilities %s overwrite capabilities in requested options",
clobberedCapabilities));
}
WebDriver driver = getLocalDriver();
if (driver == null) {
driver = getRemoteDriver();
}
return augmenter.augment(driver);
}
private WebDriver getRemoteDriver() {
startDriverServiceIfNecessary();
ClientConfig driverClientConfig = clientConfig;
URI baseUri = getBaseUri();
if (baseUri != null) {
driverClientConfig = driverClientConfig.baseUri(baseUri);
}
if (credentials != null) {
driverClientConfig = driverClientConfig.authenticateAs(credentials);
}
HttpHandler client = handlerFactory.apply(driverClientConfig);
HttpHandler handler = Require.nonNull("Http handler", client)
.with(new CloseHttpClientFilter(client)
.andThen(new AddWebDriverSpecHeaders())
.andThen(new ErrorFilter())
.andThen(new DumpHttpExchangeFilter()));
Either<SessionNotCreatedException, ProtocolHandshake.Result> result;
try {
result = new ProtocolHandshake().createSession(handler, getPayload());
} catch (IOException e) {
throw new SessionNotCreatedException("Unable to create new remote session.", e);
}
if (result.isRight()) {
CommandExecutor executor = result.map(res -> createExecutor(handler, res));
return new RemoteWebDriver(executor, new ImmutableCapabilities());
} else {
throw result.left();
}
}
private URI getBaseUri() {
if (remoteHost != null) {
return remoteHost;
}
if (driverService != null && driverService.isRunning()) {
try {
return driverService.getUrl().toURI();
} catch (URISyntaxException e) {
throw new IllegalStateException("Unable to get driver service URI", e);
}
}
return clientConfig.baseUri();
}
private DriverService startDriverServiceIfNecessary() {
if (driverService == null) {
return null;
}
try {
driverService.start();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return driverService;
}
private CommandExecutor createExecutor(HttpHandler handler, ProtocolHandshake.Result result) {
Dialect dialect = result.getDialect();
Function<Command, HttpRequest> commandEncoder = dialect.getCommandCodec()::encode;
Function<HttpResponse, Response> responseDecoder = dialect.getResponseCodec()::decode;
Response newSessionResponse = result.createResponse();
String id = newSessionResponse.getSessionId();
CommandExecutor baseExecutor = cmd -> commandEncoder.andThen(handler::execute).andThen(responseDecoder).apply(cmd);
CommandExecutor handleNewSession = cmd -> {
if (DriverCommand.NEW_SESSION.equals(cmd.getName())) {
return newSessionResponse;
}
return baseExecutor.execute(cmd);
};
CommandExecutor addSessionId = cmd -> {
Response res = handleNewSession.execute(cmd);
if (res.getSessionId() == null) {
res.setSessionId(id);
}
return res;
};
CommandExecutor stopService = cmd -> {
try {
return addSessionId.execute(cmd);
} finally {
if (driverService != null && QUIT.equals(cmd.getName())) {
try {
driverService.stop();
} catch (Exception e) {
// Fall through.
}
}
}
};
return stopService;
}
private Set<String> getClobberedCapabilities() {
Set<String> names = additionalCapabilities.keySet();
return requestedCapabilities.stream()
.map(Capabilities::getCapabilityNames)
.flatMap(Collection::stream)
.filter(names::contains)
.collect(Collectors.toSet());
}
private NewSessionPayload getPayload() {
Map<String, Object> roughPayload = new TreeMap<>(metadata);
Map<String, Object> w3cCaps = new TreeMap<>();
w3cCaps.put("alwaysMatch", additionalCapabilities);
if (!requestedCapabilities.isEmpty()) {
w3cCaps.put("firstMatch", requestedCapabilities);
}
roughPayload.put("capabilities", w3cCaps);
return NewSessionPayload.create(roughPayload);
}
private static class CloseHttpClientFilter implements Filter {
private final HttpHandler client;
CloseHttpClientFilter(HttpHandler client) {
this.client = Require.nonNull("Http client", client);
}
@Override
public HttpHandler apply(HttpHandler next) {
return req -> {
try {
return next.execute(req);
} finally {
if (req.getMethod() == DELETE && client instanceof Closeable) {
HttpSessionId.getSessionId(req.getUri()).ifPresent(id -> {
if (("/session/" + id).equals(req.getUri())) {
try {
((Closeable) client).close();
} catch (IOException e) {
LOG.log(WARNING, "Exception swallowed while closing http client", e);
}
}
});
}
}
};
}
}
}