/
W3CRemoteDriverTest.java
341 lines (279 loc) · 11.9 KB
/
W3CRemoteDriverTest.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
// 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 static java.util.Collections.EMPTY_MAP;
import static java.util.Collections.singletonList;
import static java.util.Collections.singletonMap;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assume.assumeNotNull;
import static org.openqa.selenium.json.Json.MAP_TYPE;
import org.junit.Ignore;
import org.junit.Test;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.Platform;
import org.openqa.selenium.SessionNotCreatedException;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.GeckoDriverService;
import org.openqa.selenium.firefox.xpi.XpiDriverService;
import org.openqa.selenium.ie.InternetExplorerDriverService;
import org.openqa.selenium.ie.InternetExplorerOptions;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.json.JsonOutput;
import org.openqa.selenium.remote.service.DriverService;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.io.UncheckedIOException;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class W3CRemoteDriverTest {
@Test
public void mustSpecifyAtLeastOneSetOfOptions() {
assertThatExceptionOfType(SessionNotCreatedException.class)
.isThrownBy(() -> RemoteWebDriver.builder().build());
}
@Test
public void settingAGlobalCapabilityCountsAsAnOption() {
RemoteWebDriverBuilder builder = RemoteWebDriver.builder()
.setCapability("browserName", "cheese");
List<Capabilities> capabilities = listCapabilities(builder);
assertThat(capabilities).hasSize(1);
assertThat(capabilities.get(0).getBrowserName()).isEqualTo("cheese");
}
@Test
public void simpleCaseShouldBeADropIn() {
List<Capabilities> capabilities =
listCapabilities(RemoteWebDriver.builder().addAlternative(new FirefoxOptions()));
assertThat(capabilities).hasSize(1);
assertThat(capabilities.get(0).getBrowserName()).isEqualTo("firefox");
}
@Test
public void requireAllOptionsAreW3CCompatible() {
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> RemoteWebDriver.builder()
.addAlternative(new ImmutableCapabilities("unknownOption", "cake")));
}
@Test
public void shouldRejectOldJsonWireProtocolNames() {
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> RemoteWebDriver.builder()
.addAlternative(new ImmutableCapabilities("platform", Platform.getCurrent())));
}
@Test
public void ensureEachOfTheKeyOptionTypesAreSafe() {
// Only include the options where we expect to get a w3c session
Stream.of(
new ChromeOptions(),
new FirefoxOptions(),
new InternetExplorerOptions())
.map(options -> RemoteWebDriver.builder().addAlternative(options))
.forEach(this::listCapabilities);
}
@Test
public void shouldAllowMetaDataToBeSet() {
Map<String, String> expected = singletonMap("cheese", "brie");
RemoteWebDriverBuilder builder = RemoteWebDriver.builder()
.addAlternative(new InternetExplorerOptions())
.addMetadata("cloud:options", expected);
Map<String, Object> payload = getPayload(builder);
assertThat(payload.get("cloud:options")).isEqualTo(expected);
}
@Test
public void doesNotAllowFirstMatchToBeUsedAsAMetadataNameAsItIsConfusing() {
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> RemoteWebDriver.builder().addMetadata("firstMatch", EMPTY_MAP));
}
@Test
public void doesNotAllowAlwaysMatchToBeUsedAsAMetadataNameAsItIsConfusing() {
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> RemoteWebDriver.builder()
.addMetadata("alwaysMatch", singletonList(EMPTY_MAP)));
}
@Test
public void doesNotAllowCapabilitiesToBeUsedAsAMetadataName() {
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> RemoteWebDriver.builder()
.addMetadata("capabilities", singletonList(EMPTY_MAP)));
}
@Test
public void shouldAllowCapabilitiesToBeSetGlobally() {
RemoteWebDriverBuilder builder = RemoteWebDriver.builder()
.addAlternative(new FirefoxOptions())
.addAlternative(new ChromeOptions())
.setCapability("se:cheese", "brie");
// We expect the global to be in the "alwaysMatch" section for obvious reasons, but that's not
// a requirement. Get the capabilities and check each of them.
List<Capabilities> allCaps = listCapabilities(builder);
assertThat(allCaps).hasSize(2);
assertThat(allCaps.get(0).getCapability("se:cheese")).isEqualTo("brie");
assertThat(allCaps.get(1).getCapability("se:cheese")).isEqualTo("brie");
}
@Test
public void shouldSetCapabilityToOptionsAddedAfterTheCallToSetCapabilities() {
RemoteWebDriverBuilder builder = RemoteWebDriver.builder()
.addAlternative(new FirefoxOptions())
.setCapability("se:cheese", "brie")
.addAlternative(new ChromeOptions());
// We expect the global to be in the "alwaysMatch" section for obvious reasons, but that's not
// a requirement. Get the capabilities and check each of them.
List<Capabilities> allCaps = listCapabilities(builder);
assertThat(allCaps).hasSize(2);
assertThat(allCaps.get(0).getCapability("se:cheese")).isEqualTo("brie");
assertThat(allCaps.get(1).getCapability("se:cheese")).isEqualTo("brie");
}
@Test
public void additionalCapabilitiesOverrideOnesSetOnCapabilitiesAlready() {
ChromeOptions options = new ChromeOptions();
options.setCapability("se:cheese", "cheddar");
RemoteWebDriverBuilder builder = RemoteWebDriver.builder()
.addAlternative(options)
.setCapability("se:cheese", "brie");
List<Capabilities> allCaps = listCapabilities(builder);
assertThat(allCaps).hasSize(1);
assertThat(allCaps.get(0).getCapability("se:cheese")).isEqualTo("brie");
}
@Test
public void ifARemoteUrlIsGivenThatIsUsedForTheSession() throws MalformedURLException {
URL expected = new URL("http://localhost:3000/woohoo/cheese");
RemoteWebDriverBuilder builder = RemoteWebDriver.builder()
.addAlternative(new InternetExplorerOptions())
.url(expected.toExternalForm());
RemoteWebDriverBuilder.Plan plan = builder.getPlan();
assertThat(plan.isUsingDriverService()).isFalse();
assertThat(plan.getRemoteHost()).isEqualTo(expected);
}
static class FakeDriverService extends DriverService {
FakeDriverService() throws IOException {
super(new File("."), 0, DEFAULT_TIMEOUT, null, null);
}
}
@Test
public void shouldUseADriverServiceIfGivenOneRegardlessOfOtherChoices() throws IOException {
DriverService expected = new FakeDriverService();
RemoteWebDriverBuilder builder = RemoteWebDriver.builder()
.addAlternative(new InternetExplorerOptions())
.withDriverService(expected);
RemoteWebDriverBuilder.Plan plan = builder.getPlan();
assertThat(plan.isUsingDriverService()).isTrue();
assertThat(plan.getDriverService()).isEqualTo(expected);
}
@Test
public void settingBothDriverServiceAndUrlIsAnError() {
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> RemoteWebDriver.builder()
.addAlternative(new InternetExplorerOptions())
.url("http://example.com/cheese/peas/wd")
.withDriverService(new FakeDriverService()));
}
@Test
public void shouldDetectDriverServicesAndUseThoseIfNoOtherChoiceMade() {
// Make sure we have at least one of the services available
Capabilities caps = null;
Class<? extends DriverService> expectedServiceClass = null;
try {
InternetExplorerDriverService.createDefaultService();
caps = new InternetExplorerOptions();
expectedServiceClass = InternetExplorerDriverService.class;
} catch (IllegalStateException e) {
// Fall through
}
if (caps == null) {
try {
ChromeDriverService.createDefaultService();
caps = new ChromeOptions();
expectedServiceClass = ChromeDriverService.class;
} catch (IllegalStateException e) {
// Fall through
}
}
if (caps == null) {
try {
GeckoDriverService.createDefaultService();
caps = new FirefoxOptions();
expectedServiceClass = GeckoDriverService.class;
} catch (IllegalStateException e) {
// Fall through
}
}
assumeNotNull(caps, expectedServiceClass);
RemoteWebDriverBuilder.Plan plan = RemoteWebDriver.builder()
.addAlternative(caps)
.getPlan();
assertThat(plan.isUsingDriverService()).isTrue();
assertThat(plan.getDriverService().getClass()).isEqualTo(expectedServiceClass);
}
@Test
@Ignore
public void shouldPreferMarionette() {
// Make sure we have at least one of the services available
Capabilities caps = new FirefoxOptions();
RemoteWebDriverBuilder.Plan plan = RemoteWebDriver.builder()
.addAlternative(caps)
.getPlan();
assertThat(new XpiDriverService.Builder().score(caps)).isEqualTo(0);
assertThat(new GeckoDriverService.Builder().score(caps)).isEqualTo(1);
assertThat(plan.isUsingDriverService()).isTrue();
assertThat(plan.getDriverService().getClass()).isEqualTo(GeckoDriverService.class);
}
@Test
public void oneOfWillClearOutTheCurrentlySetCapabilities() {
RemoteWebDriverBuilder builder = RemoteWebDriver.builder()
.addAlternative(new ChromeOptions())
.oneOf(new FirefoxOptions());
List<Capabilities> allCaps = listCapabilities(builder);
assertThat(allCaps).hasSize(1);
assertThat(allCaps.get(0).getBrowserName()).isEqualTo("firefox");
}
private List<Capabilities> listCapabilities(RemoteWebDriverBuilder builder) {
Map<String, Object> value = getPayload(builder);
//noinspection unchecked
value = (Map<String, Object>) value.get("capabilities");
@SuppressWarnings("unchecked")
Map<String, Object> always =
(Map<String, Object>) value.getOrDefault("alwaysMatch", EMPTY_MAP);
Capabilities alwaysMatch = new ImmutableCapabilities(always);
@SuppressWarnings("unchecked")
Collection<Map<String, Object>> firstMatch =
(Collection<Map<String, Object>>)
value.getOrDefault("firstMatch", singletonList(EMPTY_MAP));
return firstMatch
.parallelStream()
.map(ImmutableCapabilities::new)
.map(alwaysMatch::merge)
.collect(Collectors.toList());
}
private Map<String, Object> getPayload(RemoteWebDriverBuilder builder) {
Json json = new Json();
try (Writer writer = new StringWriter();
JsonOutput jsonOutput = json.newOutput(writer)) {
builder.getPlan().writePayload(jsonOutput);
return json.toType(writer.toString(), MAP_TYPE);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}