Be more defensive in DesiredCapabilities
This diff does two things:
1/ Adds a null check to the constructor taking a `Map`,
so hopefully we'll have fewer NPEs.
2/ Make all the `setCapability` methods delegate down to
a single method, rather than spreading logic throughout
the class.
@@ -52,6 +52,10 @@ public DesiredCapabilities() {
}
public DesiredCapabilities(Map<String, ?> rawMap) {
+ if (rawMap == null) {
+ return;
+ }
+
capabilities.putAll(rawMap);
if (rawMap.containsKey(UNEXPECTED_ALERT_BEHAVIOUR)) {
capabilities.put(UNHANDLED_PROMPT_BEHAVIOUR, rawMap.get(UNEXPECTED_ALERT_BEHAVIOUR));
@@ -137,41 +141,36 @@ public Object getCapability(String capabilityName) {
*/
@Override
public DesiredCapabilities merge(Capabilities extraCapabilities) {
- if (extraCapabilities != null) {
- capabilities.putAll(extraCapabilities.asMap());
- if (extraCapabilities.getCapability(UNEXPECTED_ALERT_BEHAVIOUR) != null) {
- capabilities.put(UNHANDLED_PROMPT_BEHAVIOUR, extraCapabilities.getCapability(UNEXPECTED_ALERT_BEHAVIOUR));
- }
+ if (extraCapabilities == null) {
+ return this;
}
+
+ extraCapabilities.asMap().forEach(this::setCapability);
+
return this;
}
public void setCapability(String capabilityName, boolean value) {
- capabilities.put(capabilityName, value);
+ setCapability(capabilityName, (Object) value);
}
public void setCapability(String capabilityName, String value) {
- if (PLATFORM.equals(capabilityName)) {
- try {
- capabilities.put(capabilityName, Platform.fromString(value));
- } catch (WebDriverException ex) {
- capabilities.put(capabilityName, value);
- }
- } else {
- if (UNEXPECTED_ALERT_BEHAVIOUR.equals(capabilityName)) {
- capabilities.put(UNHANDLED_PROMPT_BEHAVIOUR, value);
- }
- capabilities.put(capabilityName, value);
- }
+ setCapability(capabilityName, (Object) value);
}
public void setCapability(String capabilityName, Platform value) {
- capabilities.put(capabilityName, value);
+ setCapability(capabilityName, (Object) value);
}
public void setCapability(String key, Object value) {
if (PLATFORM.equals(key) && value instanceof String) {
+ try {
capabilities.put(key, Platform.fromString((String) value));
+ } catch (WebDriverException e) {
+ capabilities.put(key, value);
+ }
+ } else if (UNEXPECTED_ALERT_BEHAVIOUR.equals(key)) {
+ capabilities.put(UNHANDLED_PROMPT_BEHAVIOUR, value);
} else {
capabilities.put(key, value);
}
