Skip to content

Commit 14ef7bd

Browse files
committed
FirefoxOptions now picks up System property set values
Specifically for the profile, binary, and whether to use marionette. Closes #3649
1 parent 549996d commit 14ef7bd

File tree

5 files changed

+183
-30
lines changed

5 files changed

+183
-30
lines changed

java/client/src/org/openqa/selenium/firefox/FirefoxOptions.java

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public class FirefoxOptions {
8282
private Map<String, Integer> intPrefs = new HashMap<>();
8383
private Map<String, String> stringPrefs = new HashMap<>();
8484
private Level logLevel = null;
85-
private Boolean legacy;
85+
private boolean legacy;
8686
private DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
8787
private DesiredCapabilities requiredCapabilities = new DesiredCapabilities();
8888

@@ -143,6 +143,31 @@ private static <T> T getOption(Map<String, Object> map, String key, Class<T> typ
143143
"In FirefoxOptions, expected key '%s' to be a %s: %s", key, type.getSimpleName(), map));
144144
}
145145

146+
public FirefoxOptions() {
147+
// Read system properties and use those if they are set, allowing users to override them later
148+
// should they want to.
149+
150+
String binary = System.getProperty(FirefoxDriver.SystemProperty.BROWSER_BINARY);
151+
if (binary != null) {
152+
setBinary(binary);
153+
}
154+
155+
String forceMarionette = System.getProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE);
156+
if (forceMarionette != null) {
157+
setLegacy(!Boolean.getBoolean(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE));
158+
}
159+
160+
String profileName = System.getProperty(FirefoxDriver.SystemProperty.BROWSER_PROFILE);
161+
if (profileName != null) {
162+
this.profile = new ProfilesIni().getProfile(profileName);
163+
if (this.profile == null) {
164+
throw new WebDriverException(String.format(
165+
"Firefox profile '%s' named in system property '%s' not found",
166+
profileName, FirefoxDriver.SystemProperty.BROWSER_PROFILE));
167+
}
168+
}
169+
}
170+
146171
public FirefoxOptions setLegacy(boolean legacy) {
147172
this.legacy = legacy;
148173
desiredCapabilities.setCapability(MARIONETTE, !legacy);
@@ -151,15 +176,7 @@ public FirefoxOptions setLegacy(boolean legacy) {
151176
}
152177

153178
public boolean isLegacy() {
154-
String forceMarionette = System.getProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE);
155-
if (forceMarionette != null) {
156-
return !Boolean.valueOf(forceMarionette);
157-
}
158-
if (legacy != null) {
159-
return legacy;
160-
}
161-
162-
return false;
179+
return legacy;
163180
}
164181

165182
public FirefoxOptions setBinary(FirefoxBinary binary) {
@@ -256,6 +273,10 @@ public FirefoxOptions setProfile(FirefoxProfile profile) {
256273
}
257274

258275
public FirefoxProfile getProfile() {
276+
return getProfileOrNull().orElseGet(() -> fullyPopulateProfile(new FirefoxProfile()));
277+
}
278+
279+
public Optional<FirefoxProfile> getProfileOrNull() {
259280
FirefoxProfile profileToUse = profile;
260281
if (profileToUse == null) {
261282
profileToUse = extractProfile(requiredCapabilities);
@@ -264,29 +285,21 @@ public FirefoxProfile getProfile() {
264285
profileToUse = extractProfile(desiredCapabilities);
265286
}
266287
if (profileToUse == null) {
267-
String suggestedProfile = System.getProperty(FirefoxDriver.SystemProperty.BROWSER_PROFILE);
268-
if (suggestedProfile != null) {
269-
profileToUse = new ProfilesIni().getProfile(suggestedProfile);
270-
if (profileToUse == null) {
271-
throw new WebDriverException(String.format(
272-
"Firefox profile '%s' named in system property '%s' not found",
273-
suggestedProfile, FirefoxDriver.SystemProperty.BROWSER_PROFILE));
274-
}
275-
}
276-
}
277-
if (profileToUse == null) {
278-
profileToUse = new FirefoxProfile();
288+
return Optional.empty();
279289
}
280290

281-
populateProfile(profileToUse, desiredCapabilities);
282-
populateProfile(profileToUse, requiredCapabilities);
291+
return Optional.of(fullyPopulateProfile(profileToUse));
292+
}
293+
294+
private FirefoxProfile fullyPopulateProfile(FirefoxProfile profile) {
295+
populateProfile(profile, desiredCapabilities);
296+
populateProfile(profile, requiredCapabilities);
283297

284-
FirefoxProfile prefHolder = profileToUse;
285-
booleanPrefs.entrySet().forEach(pref -> prefHolder.setPreference(pref.getKey(), pref.getValue()));
286-
intPrefs.entrySet().forEach(pref -> prefHolder.setPreference(pref.getKey(), pref.getValue()));
287-
stringPrefs.entrySet().forEach(pref -> prefHolder.setPreference(pref.getKey(), pref.getValue()));
298+
booleanPrefs.entrySet().forEach(pref -> profile.setPreference(pref.getKey(), pref.getValue()));
299+
intPrefs.entrySet().forEach(pref -> profile.setPreference(pref.getKey(), pref.getValue()));
300+
stringPrefs.entrySet().forEach(pref -> profile.setPreference(pref.getKey(), pref.getValue()));
288301

289-
return profileToUse;
302+
return profile;
290303
}
291304

292305
private static void populateProfile(FirefoxProfile profile, Capabilities capabilities) {

java/client/src/org/openqa/selenium/firefox/internal/ProfilesIni.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,9 @@ protected File newProfile(String name, File appData, String path, boolean isRela
109109

110110
public FirefoxProfile getProfile(String profileName) {
111111
File profileDir = profiles.get(profileName);
112-
if (profileDir == null)
112+
if (profileDir == null) {
113113
return null;
114+
}
114115

115116
// Make a copy of the profile to use
116117
File tempDir = TemporaryFilesystem.getDefaultTmpFS().createTempDir("userprofile", "copy");

java/client/test/org/openqa/selenium/firefox/FirefoxOptionsTest.java

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,36 @@
1717

1818
package org.openqa.selenium.firefox;
1919

20+
import static java.nio.file.StandardOpenOption.DELETE_ON_CLOSE;
21+
import static org.hamcrest.Matchers.is;
22+
import static org.hamcrest.Matchers.nullValue;
2023
import static org.junit.Assert.assertEquals;
24+
import static org.junit.Assert.assertFalse;
25+
import static org.junit.Assert.assertTrue;
2126
import static org.junit.Assert.fail;
27+
import static org.junit.Assume.assumeNotNull;
28+
import static org.junit.Assume.assumeThat;
29+
import static org.openqa.selenium.firefox.FirefoxDriver.SystemProperty.BROWSER_BINARY;
30+
import static org.openqa.selenium.firefox.FirefoxDriver.SystemProperty.BROWSER_PROFILE;
31+
import static org.openqa.selenium.firefox.FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE;
2232

33+
import com.google.common.collect.ImmutableSet;
2334
import com.google.gson.JsonObject;
2435

2536
import org.junit.Test;
2637
import org.openqa.selenium.Capabilities;
38+
import org.openqa.selenium.WebDriverException;
39+
import org.openqa.selenium.firefox.internal.ProfilesIni;
2740
import org.openqa.selenium.remote.DesiredCapabilities;
41+
import org.openqa.selenium.testing.JreSystemProperty;
2842

2943
import java.io.IOException;
44+
import java.io.OutputStream;
45+
import java.nio.file.Files;
46+
import java.nio.file.Path;
3047
import java.nio.file.Paths;
48+
import java.nio.file.attribute.PosixFilePermission;
49+
import java.util.Optional;
3150

3251
public class FirefoxOptionsTest {
3352

@@ -72,4 +91,82 @@ public void pathBasedBinaryRemainsAbsoluteIfSetAsAbsolute() throws IOException {
7291

7392
assertEquals("/i/like/cheese", json.getAsJsonPrimitive("binary").getAsString());
7493
}
94+
95+
@Test
96+
public void shouldPickUpBinaryFromSystemPropertyIfSet() throws IOException {
97+
JreSystemProperty property = new JreSystemProperty(BROWSER_BINARY);
98+
String resetValue = property.get();
99+
100+
Path binary = Files.createTempFile("firefox", ".exe");
101+
try (OutputStream ignored = Files.newOutputStream(binary, DELETE_ON_CLOSE)) {
102+
Files.write(binary, "".getBytes());
103+
Files.setPosixFilePermissions(binary, ImmutableSet.of(PosixFilePermission.OWNER_EXECUTE));
104+
property.set(binary.toString());
105+
FirefoxOptions options = new FirefoxOptions();
106+
107+
FirefoxBinary firefoxBinary =
108+
options.getBinaryOrNull().orElseThrow(() -> new AssertionError("No binary"));
109+
110+
assertEquals(binary.toString(), firefoxBinary.getPath());
111+
} finally {
112+
property.set(resetValue);
113+
}
114+
}
115+
116+
@Test
117+
public void shouldPickUpLegacyValueFromSystemProperty() throws IOException {
118+
JreSystemProperty property = new JreSystemProperty(DRIVER_USE_MARIONETTE);
119+
String resetValue = property.get();
120+
121+
try {
122+
// No value should default to using Marionette
123+
property.set(null);
124+
FirefoxOptions options = new FirefoxOptions();
125+
assertFalse(options.isLegacy());
126+
127+
property.set("false");
128+
options = new FirefoxOptions();
129+
assertTrue(options.isLegacy());
130+
131+
property.set("true");
132+
options = new FirefoxOptions();
133+
assertFalse(options.isLegacy());
134+
} finally {
135+
property.set(resetValue);
136+
}
137+
}
138+
139+
@Test
140+
public void shouldPickUpProfileFromSystemProperty() throws IOException {
141+
FirefoxProfile defaultProfile = new ProfilesIni().getProfile("default");
142+
assumeNotNull(defaultProfile);
143+
144+
JreSystemProperty property = new JreSystemProperty(BROWSER_PROFILE);
145+
String resetValue = property.get();
146+
try {
147+
property.set("default");
148+
FirefoxOptions options = new FirefoxOptions();
149+
Optional<FirefoxProfile> profile = options.getProfileOrNull();
150+
151+
assertTrue(profile.isPresent());
152+
} finally {
153+
property.set(resetValue);
154+
}
155+
}
156+
157+
@Test(expected = WebDriverException.class)
158+
public void shouldThrowAnExceptionIfSystemPropertyProfileDoesNotExist() {
159+
String unlikelyProfileName = "this-profile-does-not-exist-also-cheese";
160+
FirefoxProfile foundProfile = new ProfilesIni().getProfile(unlikelyProfileName);
161+
assumeThat(foundProfile, is(nullValue()));
162+
163+
JreSystemProperty property = new JreSystemProperty(BROWSER_PROFILE);
164+
String resetValue = property.get();
165+
try {
166+
property.set(unlikelyProfileName);
167+
new FirefoxOptions();
168+
} finally {
169+
property.set(resetValue);
170+
}
171+
}
75172
}

java/client/test/org/openqa/selenium/testing/BUCK

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ java_library(name = 'helpers',
2222
srcs = [
2323
'DevMode.java',
2424
'InProject.java',
25+
'JreSystemProperty.java',
2526
],
2627
deps = [
2728
'//java/client/src/org/openqa/selenium:exceptions',
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.openqa.selenium.testing;
19+
20+
import com.google.common.base.Preconditions;
21+
22+
public class JreSystemProperty {
23+
24+
private String name;
25+
26+
public JreSystemProperty(String name) {
27+
this.name = Preconditions.checkNotNull(name);
28+
}
29+
30+
public String get() {
31+
return System.getProperty(name);
32+
}
33+
34+
public void set(String value) {
35+
if (value == null) {
36+
System.clearProperty(name);
37+
} else {
38+
System.setProperty(name, value);
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)