Skip to content

Commit 93f5a23

Browse files
committed
[java] Update FirefoxDriver to handle serialized FirefoxOptions in DesiredCapabilities
Crazy that the java server needs to fully deserialize a request to process it. Fixing that will be a massive change, so this will have to do. Fixes #3115
1 parent eeed4e0 commit 93f5a23

2 files changed

Lines changed: 68 additions & 2 deletions

File tree

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import java.io.File;
6363
import java.io.IOException;
6464
import java.net.URI;
65+
import java.util.Map;
6566
import java.util.Set;
6667
import java.util.concurrent.TimeUnit;
6768

@@ -200,6 +201,15 @@ static Capabilities populateProfile(FirefoxProfile profile, Capabilities capabil
200201
}
201202

202203
Object rawOptions = capabilities.getCapability(FIREFOX_OPTIONS);
204+
if (rawOptions instanceof Map) {
205+
try {
206+
@SuppressWarnings("unchecked")
207+
Map<String, Object> map = (Map<String, Object>) rawOptions;
208+
rawOptions = FirefoxOptions.fromJsonMap(map);
209+
} catch (IOException e) {
210+
throw new WebDriverException(e);
211+
}
212+
}
203213
if (rawOptions == null) {
204214
rawOptions = capabilities.getCapability(OLD_FIREFOX_OPTIONS);
205215
}

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

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,12 @@
2323

2424
import com.google.common.base.Preconditions;
2525
import com.google.common.collect.ImmutableList;
26-
import com.google.common.collect.ImmutableMap;
2726
import com.google.gson.JsonArray;
2827
import com.google.gson.JsonElement;
2928
import com.google.gson.JsonObject;
3029
import com.google.gson.JsonPrimitive;
3130

32-
import org.openqa.selenium.logging.LogLevelMapping;
31+
import org.openqa.selenium.WebDriverException;
3332
import org.openqa.selenium.remote.DesiredCapabilities;
3433

3534
import java.io.File;
@@ -69,6 +68,63 @@ public class FirefoxOptions {
6968
private Map<String, String> stringPrefs = new HashMap<>();
7069
private Level logLevel = null;
7170

71+
/** INTERNAL ONLY: DO NOT USE */
72+
static FirefoxOptions fromJsonMap(Map<String, Object> map) throws IOException {
73+
FirefoxOptions options = new FirefoxOptions();
74+
75+
if (map.containsKey("binary")) {
76+
options.setBinary(getOption(map, "binary", String.class));
77+
}
78+
79+
if (map.containsKey("args")) {
80+
@SuppressWarnings("unchecked") // #YOLO
81+
List<String> list = (List) getOption(map, "args", List.class);
82+
options.addArguments(list);
83+
}
84+
85+
if (map.containsKey("profile")) {
86+
Object value = map.get("profile");
87+
if (value instanceof String) {
88+
options.setProfile(FirefoxProfile.fromJson((String) value));
89+
} else if (value instanceof FirefoxProfile) {
90+
options.setProfile((FirefoxProfile) value);
91+
} else {
92+
throw new WebDriverException(
93+
"In FirefoxOptions, don't know how to convert profile: " + map);
94+
}
95+
}
96+
97+
if (map.containsKey("prefs")) {
98+
@SuppressWarnings("unchecked") // #YOLO
99+
Map<String, Object> prefs = (Map) getOption(map, "prefs", Map.class);
100+
prefs.entrySet().forEach(entry -> {
101+
Object value = entry.getValue();
102+
if (value instanceof Boolean) {
103+
options.addPreference(entry.getKey(), (Boolean) value);
104+
} else if (value instanceof Integer) {
105+
options.addPreference(entry.getKey(), (Integer) value);
106+
} else if (value instanceof String) {
107+
options.addPreference(entry.getKey(), (String) value);
108+
} else {
109+
throw new WebDriverException(
110+
"Invalid Firefox preference value: " + entry.getKey() + "=" + value);
111+
}
112+
});
113+
}
114+
115+
return options;
116+
}
117+
118+
private static <T> T getOption(Map<String, Object> map, String key, Class<T> type) {
119+
Object value = map.get(key);
120+
if (type.isInstance(value)) {
121+
return type.cast(value);
122+
}
123+
throw new WebDriverException(
124+
String.format(
125+
"In FirefoxOptions, expected key '%s' to be a %s: %s", key, type.getSimpleName(), map));
126+
}
127+
72128
public FirefoxOptions setBinary(Path path) {
73129
return setBinary(checkNotNull(path).toString());
74130
}

0 commit comments

Comments
 (0)