/
FirefoxOptionsTest.java
405 lines (341 loc) · 14.7 KB
/
FirefoxOptionsTest.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
// 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.firefox;
import static java.nio.file.StandardOpenOption.DELETE_ON_CLOSE;
import static java.util.Collections.emptyMap;
import static java.util.Collections.singleton;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assumptions.assumeThat;
import static org.assertj.core.api.InstanceOfAssertFactories.LIST;
import static org.assertj.core.api.InstanceOfAssertFactories.MAP;
import static org.openqa.selenium.PageLoadStrategy.EAGER;
import static org.openqa.selenium.firefox.FirefoxDriver.Capability.BINARY;
import static org.openqa.selenium.firefox.FirefoxDriver.Capability.MARIONETTE;
import static org.openqa.selenium.firefox.FirefoxDriver.SystemProperty.BROWSER_BINARY;
import static org.openqa.selenium.firefox.FirefoxDriver.SystemProperty.BROWSER_PROFILE;
import static org.openqa.selenium.firefox.FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE;
import static org.openqa.selenium.firefox.FirefoxDriverLogLevel.DEBUG;
import static org.openqa.selenium.firefox.FirefoxDriverLogLevel.ERROR;
import static org.openqa.selenium.firefox.FirefoxDriverLogLevel.WARN;
import static org.openqa.selenium.firefox.FirefoxOptions.FIREFOX_OPTIONS;
import static org.openqa.selenium.remote.CapabilityType.ACCEPT_INSECURE_CERTS;
import static org.openqa.selenium.remote.CapabilityType.PAGE_LOAD_STRATEGY;
import com.google.common.collect.ImmutableMap;
import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.testing.TestUtilities;
import org.openqa.selenium.testing.UnitTests;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFilePermission;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
@Category(UnitTests.class)
public class FirefoxOptionsTest {
@Test
public void canInitFirefoxOptionsWithCapabilities() {
FirefoxOptions options = new FirefoxOptions(new ImmutableCapabilities(
MARIONETTE, false,
PAGE_LOAD_STRATEGY, PageLoadStrategy.EAGER,
ACCEPT_INSECURE_CERTS, true));
assertThat(options.isLegacy()).isTrue();
assertThat(options.getCapability(PAGE_LOAD_STRATEGY)).isEqualTo(EAGER);
assertThat(options.getCapability(ACCEPT_INSECURE_CERTS)).isEqualTo(true);
}
@Test
public void canInitFirefoxOptionsWithCapabilitiesThatContainFirefoxOptions() {
FirefoxOptions options = new FirefoxOptions().setLegacy(true).merge(
new ImmutableCapabilities(PAGE_LOAD_STRATEGY, PageLoadStrategy.EAGER));
Capabilities caps = new ImmutableCapabilities(FIREFOX_OPTIONS, options);
FirefoxOptions options2 = new FirefoxOptions(caps);
assertThat(options2.isLegacy()).isTrue();
assertThat(options2.getCapability(PAGE_LOAD_STRATEGY)).isEqualTo(EAGER);
}
@Test
public void canInitFirefoxOptionsWithCapabilitiesThatContainFirefoxOptionsAsMap() {
FirefoxProfile profile = new FirefoxProfile();
Capabilities caps = new ImmutableCapabilities(
FIREFOX_OPTIONS, ImmutableMap.of("profile", profile));
FirefoxOptions options = new FirefoxOptions(caps);
assertThat(options.getProfile()).isSameAs(profile);
}
@Test
public void binaryPathNeedNotExist() {
new FirefoxOptions().setBinary("does/not/exist");
}
@Test
public void shouldKeepRelativePathToBinaryAsIs() {
FirefoxOptions options = new FirefoxOptions().setBinary("some/path");
assertThat(options.getCapability(BINARY)).isEqualTo("some/path");
}
@Test
public void shouldConvertPathToBinaryToUseForwardSlashes() {
FirefoxOptions options = new FirefoxOptions().setBinary("some\\path");
assertThat(options.getCapability(BINARY)).isEqualTo("some/path");
}
@Test
public void shouldKeepWindowsDriveLetterInPathToBinary() {
FirefoxOptions options = new FirefoxOptions().setBinary("F:\\some\\path");
assertThat(options.getCapability(BINARY)).isEqualTo("F:/some/path");
}
@Test
public void canUseForwardSlashesInWindowsPaths() {
FirefoxOptions options = new FirefoxOptions().setBinary("F:\\some\\path");
assertThat(options.getCapability(BINARY)).isEqualTo("F:/some/path");
}
@Test
public void shouldKeepWindowsNetworkFileSystemRootInPathToBinary() {
FirefoxOptions options = new FirefoxOptions().setBinary("\\\\server\\share\\some\\path");
assertThat(options.getCapability(BINARY)).isEqualTo("//server/share/some/path");
}
@Test
public void shouldKeepAFirefoxBinaryAsABinaryIfSetAsOne() throws IOException {
File fakeExecutable = Files.createTempFile("firefox", ".exe").toFile();
fakeExecutable.deleteOnExit();
FirefoxBinary binary = new FirefoxBinary(fakeExecutable);
FirefoxOptions options = new FirefoxOptions().setBinary(binary);
assertThat(options.getCapability(BINARY)).isEqualTo(binary);
assertThat(options.getBinary()).isEqualTo(binary);
}
@Test
public void stringBasedBinaryRemainsAbsoluteIfSetAsAbsolute() {
Map<String, Object> json = new FirefoxOptions().setBinary("/i/like/cheese").asMap();
assertThat(json.get(FIREFOX_OPTIONS))
.asInstanceOf(InstanceOfAssertFactories.MAP)
.containsEntry("binary", "/i/like/cheese");
}
@Test
public void pathBasedBinaryRemainsAbsoluteIfSetAsAbsolute() {
Map<String, Object> json = new FirefoxOptions().setBinary(Paths.get("/i/like/cheese")).asMap();
assertThat(json.get(FIREFOX_OPTIONS))
.asInstanceOf(InstanceOfAssertFactories.MAP)
.containsEntry("binary", "/i/like/cheese");
}
@Test
public void shouldPickUpBinaryFromSystemPropertyIfSet() throws IOException {
JreSystemProperty property = new JreSystemProperty(BROWSER_BINARY);
Path binary = Files.createTempFile("firefox", ".exe");
try (OutputStream ignored = Files.newOutputStream(binary, DELETE_ON_CLOSE)) {
Files.write(binary, "".getBytes());
if (! TestUtilities.getEffectivePlatform().is(Platform.WINDOWS)) {
Files.setPosixFilePermissions(binary, singleton(PosixFilePermission.OWNER_EXECUTE));
}
property.set(binary.toString());
FirefoxOptions options = new FirefoxOptions();
FirefoxBinary firefoxBinary =
options.getBinaryOrNull().orElseThrow(() -> new AssertionError("No binary"));
assertThat(firefoxBinary.getPath()).isEqualTo(binary.toString());
} finally {
property.reset();
}
}
@Test
public void shouldPickUpLegacyValueFromSystemProperty() {
JreSystemProperty property = new JreSystemProperty(DRIVER_USE_MARIONETTE);
try {
// No value should default to using Marionette
property.set(null);
FirefoxOptions options = new FirefoxOptions();
assertThat(options.isLegacy()).isFalse();
property.set("false");
options = new FirefoxOptions();
assertThat(options.isLegacy()).isTrue();
property.set("true");
options = new FirefoxOptions();
assertThat(options.isLegacy()).isFalse();
} finally {
property.reset();
}
}
@Test
public void settingMarionetteToFalseAsASystemPropertyDoesNotPrecedence() {
JreSystemProperty property = new JreSystemProperty(DRIVER_USE_MARIONETTE);
try {
Capabilities caps = new ImmutableCapabilities(MARIONETTE, true);
property.set("false");
FirefoxOptions options = new FirefoxOptions().merge(caps);
assertThat(options.isLegacy()).isFalse();
} finally {
property.reset();
}
}
@Test
public void shouldPickUpProfileFromSystemProperty() {
FirefoxProfile defaultProfile = new ProfilesIni().getProfile("default");
assumeThat(defaultProfile).isNotNull();
JreSystemProperty property = new JreSystemProperty(BROWSER_PROFILE);
try {
property.set("default");
FirefoxOptions options = new FirefoxOptions();
FirefoxProfile profile = options.getProfile();
assertThat(profile).isNotNull();
} finally {
property.reset();
}
}
@Test
public void shouldThrowAnExceptionIfSystemPropertyProfileDoesNotExist() {
String unlikelyProfileName = "this-profile-does-not-exist-also-cheese";
FirefoxProfile foundProfile = new ProfilesIni().getProfile(unlikelyProfileName);
assumeThat(foundProfile).isNull();
JreSystemProperty property = new JreSystemProperty(BROWSER_PROFILE);
try {
property.set(unlikelyProfileName);
assertThatExceptionOfType(WebDriverException.class)
.isThrownBy(FirefoxOptions::new);
} finally {
property.reset();
}
}
@Test
public void callingToStringWhenTheBinaryDoesNotExistShouldNotCauseAnException() {
FirefoxOptions options =
new FirefoxOptions().setBinary("there's nothing better in life than cake or peas.");
options.toString();
// The binary does not exist on this machine, but could do elsewhere. Be chill.
}
@Test
public void logLevelStringRepresentationIsLowercase() {
assertThat(DEBUG.toString()).isEqualTo("debug");
}
@Test
public void canBuildLogLevelFromStringRepresentation() {
assertThat(FirefoxDriverLogLevel.fromString("warn")).isEqualTo(WARN);
assertThat(FirefoxDriverLogLevel.fromString("ERROR")).isEqualTo(ERROR);
}
@Test
public void canConvertOptionsWithArgsToCapabilitiesAndRestoreBack() {
FirefoxOptions options = new FirefoxOptions(
new MutableCapabilities(new FirefoxOptions().addArguments("-a", "-b")));
Object options2 = options.asMap().get(FirefoxOptions.FIREFOX_OPTIONS);
assertThat(options2)
.asInstanceOf(InstanceOfAssertFactories.MAP)
.containsEntry("args", Arrays.asList("-a", "-b"));
}
@Test
public void canConvertOptionsWithPrefsToCapabilitiesAndRestoreBack() {
FirefoxOptions options = new FirefoxOptions(
new MutableCapabilities(
new FirefoxOptions()
.addPreference("string.pref", "some value")
.addPreference("int.pref", 42)
.addPreference("boolean.pref", true)));
Object options2 = options.asMap().get(FirefoxOptions.FIREFOX_OPTIONS);
assertThat(options2)
.asInstanceOf(InstanceOfAssertFactories.MAP)
.extractingByKey("prefs")
.asInstanceOf(InstanceOfAssertFactories.MAP)
.containsEntry("string.pref", "some value")
.containsEntry("int.pref", 42)
.containsEntry("boolean.pref", true);
}
@Test
public void canConvertOptionsWithBinaryToCapabilitiesAndRestoreBack() {
FirefoxOptions options = new FirefoxOptions(
new MutableCapabilities(new FirefoxOptions().setBinary(new FirefoxBinary())));
Object options2 = options.asMap().get(FirefoxOptions.FIREFOX_OPTIONS);
assertThat(options2)
.asInstanceOf(InstanceOfAssertFactories.MAP)
.containsEntry("binary", new FirefoxBinary().getPath().replaceAll("\\\\", "/"));
}
@Test
public void roundTrippingToCapabilitiesAndBackWorks() {
FirefoxOptions expected = new FirefoxOptions()
.setLegacy(true)
.addPreference("cake", "walk");
// Convert to a Map so we can create a standalone capabilities instance, which we then use to
// create a new set of options. This is the round trip, ladies and gentlemen.
FirefoxOptions seen = new FirefoxOptions(new ImmutableCapabilities(expected.asMap()));
assertThat(seen).isEqualTo(expected);
}
@Test
public void optionsAsMapShouldBeImmutable() {
Map<String, Object> options = new FirefoxOptions().asMap();
assertThatExceptionOfType(UnsupportedOperationException.class)
.isThrownBy(() -> options.put("browserName", "chrome"));
Map<String, Object> mozOptions = (Map<String, Object>) options.get(FIREFOX_OPTIONS);
assertThatExceptionOfType(UnsupportedOperationException.class)
.isThrownBy(() -> mozOptions.put("prefs", emptyMap()));
Map<String, Object> prefs = (Map<String, Object>) mozOptions.get("prefs");
assertThatExceptionOfType(UnsupportedOperationException.class)
.isThrownBy(() -> prefs.put("x", true));
List<String> args = (List<String>) mozOptions.get("args");
assertThatExceptionOfType(UnsupportedOperationException.class)
.isThrownBy(() -> args.add("-help"));
}
@Test
public void mergingOptionsMergesArguments() {
FirefoxOptions one = new FirefoxOptions().addArguments("verbose");
FirefoxOptions two = new FirefoxOptions().addArguments("silent");
FirefoxOptions merged = one.merge(two);
assertThat(merged.asMap()).asInstanceOf(MAP)
.extractingByKey(FirefoxOptions.FIREFOX_OPTIONS).asInstanceOf(MAP)
.extractingByKey("args").asInstanceOf(LIST)
.containsExactly("verbose", "silent");
}
@Test
public void mergingOptionsMergesPreferences() {
FirefoxOptions one = new FirefoxOptions()
.addPreference("opt1", "val1")
.addPreference("opt2", "val2");
FirefoxOptions two = new FirefoxOptions()
.addPreference("opt2", "val4")
.addPreference("opt3", "val3");
FirefoxOptions merged = one.merge(two);
assertThat(merged.asMap()).asInstanceOf(MAP)
.extractingByKey(FirefoxOptions.FIREFOX_OPTIONS).asInstanceOf(MAP)
.extractingByKey("prefs").asInstanceOf(MAP)
.containsEntry("opt1", "val1")
.containsEntry("opt2", "val4")
.containsEntry("opt3", "val3");
}
private static class JreSystemProperty {
private final String name;
private final String originalValue;
public JreSystemProperty(String name) {
this.name = Require.nonNull("Name", name);
this.originalValue = System.getProperty(name);
}
public String get() {
return System.getProperty(name);
}
public void set(String value) {
if (value == null) {
System.clearProperty(name);
} else {
System.setProperty(name, value);
}
}
public void reset() {
set(originalValue);
}
}
}