Skip to content

Commit a443604

Browse files
committed
Fixing "args" and "prefs" handling in Capabilities to FirefoxOptions converters
1 parent c1b7c17 commit a443604

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

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

+23-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,29 @@ public FirefoxOptions(Capabilities source) {
134134
if (that.profile != null) { setProfile(that.profile); }
135135
} else if (raw instanceof Map) {
136136
Map<?, ?> that = (Map<?, ?>) raw;
137-
if (that.containsKey("args")) { addArguments((String) that.get("args")); }
137+
if (that.containsKey("args")) {
138+
Object value = that.get("args");
139+
if (value instanceof String) {
140+
addArguments((String) that.get("args"));
141+
} else if (value instanceof List<?>) {
142+
addArguments((List<String>) that.get("args"));
143+
} else {
144+
// last resort
145+
addArguments(that.get("args").toString());
146+
}
147+
}
148+
if (that.containsKey("prefs")) {
149+
Map<String, Object> prefs = (Map<String, Object>) that.get("prefs");
150+
prefs.forEach((k, v) -> {
151+
if (v instanceof String) {
152+
stringPrefs.put(k, (String) v);
153+
} else if (v instanceof Number) {
154+
intPrefs.put(k, ((Number) v).intValue());
155+
} else if (v instanceof Boolean) {
156+
booleanPrefs.put(k, (Boolean) v);
157+
}
158+
});
159+
}
138160
if (that.containsKey("binary")) { setBinary((String) that.get("binary")); }
139161
if (that.containsKey("log")) {
140162
Map<?, ?> logStruct = (Map<?, ?>) that.get("log");

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

+37
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.junit.Test;
4444
import org.openqa.selenium.Capabilities;
4545
import org.openqa.selenium.ImmutableCapabilities;
46+
import org.openqa.selenium.MutableCapabilities;
4647
import org.openqa.selenium.PageLoadStrategy;
4748
import org.openqa.selenium.Platform;
4849
import org.openqa.selenium.WebDriverException;
@@ -57,6 +58,7 @@
5758
import java.nio.file.Path;
5859
import java.nio.file.Paths;
5960
import java.nio.file.attribute.PosixFilePermission;
61+
import java.util.Arrays;
6062
import java.util.Map;
6163

6264
public class FirefoxOptionsTest {
@@ -278,4 +280,39 @@ public void canBuildLogLevelFromStringRepresentation() {
278280
assertEquals(FirefoxDriverLogLevel.fromString("ERROR"), FirefoxDriverLogLevel.ERROR);
279281
}
280282

283+
@Test
284+
public void canConvertOptionsWithArgsToCapabilitiesAndRestoreBack() {
285+
FirefoxOptions options = new FirefoxOptions(
286+
new MutableCapabilities(new FirefoxOptions().addArguments("-a", "-b")));
287+
Object options2 = options.asMap().get(FirefoxOptions.FIREFOX_OPTIONS);
288+
assertNotNull(options2);
289+
assertEquals(((Map<String, Object>) options2).get("args"), Arrays.asList("-a", "-b"));
290+
}
291+
292+
@Test
293+
public void canConvertOptionsWithPrefsToCapabilitiesAndRestoreBack() {
294+
FirefoxOptions options = new FirefoxOptions(
295+
new MutableCapabilities(new FirefoxOptions()
296+
.addPreference("string.pref", "some value")
297+
.addPreference("int.pref", 42)
298+
.addPreference("boolean.pref", true)));
299+
Object options2 = options.asMap().get(FirefoxOptions.FIREFOX_OPTIONS);
300+
assertNotNull(options2);
301+
Object prefs = ((Map<String, Object>) options2).get("prefs");
302+
assertNotNull(prefs);
303+
assertEquals(((Map<String, Object>) prefs).get("string.pref"), "some value");
304+
assertEquals(((Map<String, Object>) prefs).get("int.pref"), 42);
305+
assertEquals(((Map<String, Object>) prefs).get("boolean.pref"), true);
306+
}
307+
308+
@Test
309+
public void canConvertOptionsWithBinaryToCapabilitiesAndRestoreBack() {
310+
FirefoxOptions options = new FirefoxOptions(
311+
new MutableCapabilities(new FirefoxOptions().setBinary(new FirefoxBinary())));
312+
Object options2 = options.asMap().get(FirefoxOptions.FIREFOX_OPTIONS);
313+
assertNotNull(options2);
314+
assertEquals(((Map<String, Object>) options2).get("binary"),
315+
new FirefoxBinary().getPath().replaceAll("\\\\", "/"));
316+
}
317+
281318
}

0 commit comments

Comments
 (0)