Skip to content

Commit 8bfa175

Browse files
committed
Support FirefoxOptions in the java binding
Which is nice.
1 parent 9ac53fa commit 8bfa175

File tree

4 files changed

+226
-0
lines changed

4 files changed

+226
-0
lines changed

java/client/src/org/openqa/selenium/firefox/BUCK

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ java_library(name = 'firefox',
1212
':prefs',
1313
'//java/client/src/org/openqa/selenium:selenium',
1414
'//java/client/src/org/openqa/selenium/remote:remote',
15+
'//third_party/java/gson:gson',
1516
'//third_party/java/guava:guava',
1617
],
1718
visibility = [ 'PUBLIC' ],
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
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.firefox;
19+
20+
import static com.google.common.base.Preconditions.checkNotNull;
21+
import static org.openqa.selenium.firefox.FirefoxDriver.BINARY;
22+
import static org.openqa.selenium.firefox.FirefoxDriver.PROFILE;
23+
24+
import com.google.common.collect.ImmutableList;
25+
import com.google.gson.JsonArray;
26+
import com.google.gson.JsonElement;
27+
import com.google.gson.JsonObject;
28+
import com.google.gson.JsonPrimitive;
29+
30+
import org.openqa.selenium.remote.DesiredCapabilities;
31+
32+
import java.io.File;
33+
import java.io.IOException;
34+
import java.nio.file.Path;
35+
import java.util.ArrayList;
36+
import java.util.HashMap;
37+
import java.util.List;
38+
import java.util.Map;
39+
40+
public class FirefoxOptions {
41+
42+
private String binary;
43+
private FirefoxProfile profile;
44+
private List<String> args = new ArrayList<>();
45+
private Map<String, Boolean> booleanPrefs = new HashMap<>();
46+
private Map<String, Integer> intPrefs = new HashMap<>();
47+
private Map<String, String> stringPrefs = new HashMap<>();
48+
49+
public FirefoxOptions setBinary(Path path) {
50+
return setBinary(checkNotNull(path).toString());
51+
}
52+
53+
public FirefoxOptions setBinary(String binary) {
54+
this.binary = checkNotNull(binary);
55+
return this;
56+
}
57+
58+
public FirefoxOptions setProfile(FirefoxProfile profile) {
59+
this.profile = checkNotNull(profile);
60+
return this;
61+
}
62+
63+
public FirefoxOptions addArguments(String... arguments) {
64+
addArguments(ImmutableList.copyOf(arguments));
65+
return this;
66+
}
67+
68+
public FirefoxOptions addArguments(List<String> arguments) {
69+
args.addAll(arguments);
70+
return this;
71+
}
72+
73+
public FirefoxOptions addPreference(String key, boolean value) {
74+
booleanPrefs.put(checkNotNull(key), value);
75+
return this;
76+
}
77+
78+
public FirefoxOptions addPreference(String key, int value) {
79+
intPrefs.put(checkNotNull(key), value);
80+
return this;
81+
}
82+
83+
public FirefoxOptions addPreference(String key, String value) {
84+
stringPrefs.put(checkNotNull(key), checkNotNull(value));
85+
return this;
86+
}
87+
88+
public DesiredCapabilities addTo(DesiredCapabilities capabilities) {
89+
Object priorBinary = capabilities.getCapability(BINARY);
90+
if (binary != null && priorBinary != null && !binary.equals(priorBinary)) {
91+
throw new IllegalStateException(
92+
"Binary already set in capabilities, but is different from the one in these options");
93+
}
94+
95+
Object priorProfile = capabilities.getCapability(PROFILE);
96+
if (priorProfile != null) {
97+
if (!booleanPrefs.isEmpty() || !intPrefs.isEmpty() || !stringPrefs.isEmpty()) {
98+
throw new IllegalStateException(
99+
"Unable to determine if preferences set on this option " +
100+
"are the same as the profile in the capabilities");
101+
}
102+
if (!priorProfile.equals(profile)) {
103+
throw new IllegalStateException(
104+
"Profile has been set on both the capabilities and these options, but they're " +
105+
"different. Unable to determine which one you want to use.");
106+
}
107+
}
108+
109+
capabilities.setCapability("firefoxOptions", this);
110+
111+
if (binary != null) {
112+
FirefoxBinary actualBinary = new FirefoxBinary(new File(binary));
113+
actualBinary.addCommandLineOptions(args.toArray(new String[args.size()]));
114+
capabilities.setCapability(BINARY, actualBinary);
115+
}
116+
117+
if (profile != null) {
118+
capabilities.setCapability(PROFILE, profile);
119+
}
120+
121+
return capabilities;
122+
}
123+
124+
public JsonElement toJson() throws IOException {
125+
JsonObject options = new JsonObject();
126+
127+
if (binary != null) {
128+
options.addProperty("binary", binary);
129+
}
130+
131+
if (profile != null) {
132+
for (Map.Entry<String, Boolean> pref : booleanPrefs.entrySet()) {
133+
profile.setPreference(pref.getKey(), pref.getValue());
134+
}
135+
for (Map.Entry<String, Integer> pref : intPrefs.entrySet()) {
136+
profile.setPreference(pref.getKey(), pref.getValue());
137+
}
138+
for (Map.Entry<String, String> pref : stringPrefs.entrySet()) {
139+
profile.setPreference(pref.getKey(), pref.getValue());
140+
}
141+
options.addProperty("profile", profile.toJson());
142+
} else {
143+
JsonObject allPrefs = new JsonObject();
144+
for (Map.Entry<String, Boolean> pref : booleanPrefs.entrySet()) {
145+
allPrefs.add(pref.getKey(), new JsonPrimitive(pref.getValue()));
146+
}
147+
for (Map.Entry<String, Integer> pref : intPrefs.entrySet()) {
148+
allPrefs.add(pref.getKey(), new JsonPrimitive(pref.getValue()));
149+
}
150+
for (Map.Entry<String, String> pref : stringPrefs.entrySet()) {
151+
allPrefs.add(pref.getKey(), new JsonPrimitive(pref.getValue()));
152+
}
153+
options.add("prefs", allPrefs);
154+
}
155+
156+
JsonArray arguments = new JsonArray();
157+
for (String arg : args) {
158+
arguments.add(new JsonPrimitive(arg));
159+
}
160+
options.add("args", arguments);
161+
162+
return options;
163+
}
164+
165+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
FirefoxCapabilitiesTest.class,
3232
FirefoxDriverTest.class,
3333
FirefoxProfileTest.class,
34+
MarionetteTest.class,
3435
NewProfileExtensionConnectionTest.class,
3536
PreferencesTest.class,
3637
SocketLockTest.class,
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.firefox;
19+
20+
import static org.junit.Assert.assertEquals;
21+
import static org.openqa.selenium.testing.Driver.FIREFOX;
22+
23+
import com.google.common.base.Function;
24+
25+
import org.junit.After;
26+
import org.junit.Test;
27+
import org.openqa.selenium.JavascriptExecutor;
28+
import org.openqa.selenium.WebDriver;
29+
import org.openqa.selenium.remote.DesiredCapabilities;
30+
import org.openqa.selenium.support.ui.ExpectedConditions;
31+
import org.openqa.selenium.testing.Ignore;
32+
import org.openqa.selenium.testing.JUnit4TestBase;
33+
34+
@Ignore(FIREFOX)
35+
public class MarionetteTest extends JUnit4TestBase {
36+
37+
private WebDriver driver;
38+
39+
@After
40+
public void quitDriver() {
41+
if (driver != null) {
42+
driver.quit();
43+
}
44+
}
45+
46+
@Test
47+
public void shouldUseFirefoxOptions() throws InterruptedException {
48+
DesiredCapabilities caps = new FirefoxOptions()
49+
.addPreference("browser.startup.page", 1)
50+
.addPreference("browser.startup.homepage", pages.xhtmlTestPage)
51+
.addTo(DesiredCapabilities.firefox());
52+
53+
driver = new FirefoxDriver(caps);
54+
55+
shortWait.until(
56+
input -> "XHTML Test Page".equals(
57+
((JavascriptExecutor) driver).executeScript("return document.title")));
58+
}
59+
}

0 commit comments

Comments
 (0)