Skip to content

Commit 9c30763

Browse files
committed
Add a Capabilities.merge method
Turns out we already had this in the `DesiredCapabilities`, which is nice.
1 parent 6dbc133 commit 9c30763

4 files changed

Lines changed: 114 additions & 5 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ java_library(name = 'core',
3131
'JavascriptExecutor.java',
3232
'DeviceRotation.java',
3333
'Keys.java',
34+
'ImmutableCapabilities.java',
3435
'OutputType.java',
3536
'Proxy.java',
3637
'Rotatable.java',

java/client/src/org/openqa/selenium/Capabilities.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package org.openqa.selenium;
1919

20+
import java.util.HashMap;
2021
import java.util.Map;
2122

2223

@@ -56,4 +57,16 @@ public interface Capabilities {
5657
* @return Whether or not the value is not null and not false.
5758
*/
5859
boolean is(String capabilityName);
60+
61+
/**
62+
* Merge two {@link Capabilities} together and return the union of the two as a new
63+
* {@link Capabilities} instance. Capabilities from {@code other} will override those in
64+
* {@code this}.
65+
*/
66+
default Capabilities merge(Capabilities other) {
67+
HashMap<String, Object> map = new HashMap<>();
68+
map.putAll(asMap());
69+
map.putAll(other.asMap());
70+
return new ImmutableCapabilities(map);
71+
}
5972
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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;
19+
20+
21+
import java.io.Serializable;
22+
import java.util.Collections;
23+
import java.util.HashMap;
24+
import java.util.Map;
25+
26+
class ImmutableCapabilities implements Capabilities, Serializable {
27+
28+
private static final long serialVersionUID = 665766108972704060L;
29+
30+
private final Map<String, Object> caps = new HashMap<>();
31+
32+
ImmutableCapabilities(Map<String, Object> capabilities) {
33+
capabilities.forEach((key, value) -> {
34+
if (value != null) {
35+
caps.put(key, value);
36+
}
37+
});
38+
}
39+
40+
@Override
41+
public String getBrowserName() {
42+
return String.valueOf(caps.getOrDefault("browserName", ""));
43+
}
44+
45+
@Override
46+
public Platform getPlatform() {
47+
Object rawPlatform = caps.get("platform");
48+
49+
if (rawPlatform == null) {
50+
return null;
51+
}
52+
53+
if (rawPlatform instanceof String) {
54+
return Platform.valueOf((String) rawPlatform);
55+
} else if (rawPlatform instanceof Platform) {
56+
return (Platform) rawPlatform;
57+
}
58+
59+
throw new IllegalStateException("Platform was neither a string or a Platform: " + rawPlatform);
60+
}
61+
62+
@Override
63+
public String getVersion() {
64+
return String.valueOf(caps.getOrDefault("version", ""));
65+
}
66+
67+
@Override
68+
public boolean isJavascriptEnabled() {
69+
Object raw = caps.getOrDefault("javascriptEnabled", true);
70+
if (raw instanceof String) {
71+
return Boolean.parseBoolean((String) raw);
72+
} else if (raw instanceof Boolean) {
73+
return (Boolean) raw;
74+
}
75+
76+
throw new IllegalStateException("Javascript-enabled capability was of invalid type: " + raw);
77+
}
78+
79+
@Override
80+
public Object getCapability(String capabilityName) {
81+
return caps.get(capabilityName);
82+
}
83+
84+
@Override
85+
public boolean is(String capabilityName) {
86+
Object cap = getCapability(capabilityName);
87+
if (cap == null) {
88+
return false;
89+
}
90+
return cap instanceof Boolean ? (Boolean) cap : Boolean.parseBoolean(String.valueOf(cap));
91+
}
92+
93+
@Override
94+
public Map<String, ?> asMap() {
95+
return Collections.unmodifiableMap(caps);
96+
}
97+
}

java/client/src/org/openqa/selenium/remote/DesiredCapabilities.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
import static org.openqa.selenium.remote.CapabilityType.SUPPORTS_JAVASCRIPT;
2525
import static org.openqa.selenium.remote.CapabilityType.VERSION;
2626

27-
import com.google.common.collect.Maps;
28-
2927
import org.openqa.selenium.Capabilities;
3028
import org.openqa.selenium.Platform;
3129
import org.openqa.selenium.WebDriverException;
@@ -174,8 +172,8 @@ public boolean is(String capabilityName) {
174172
* @param extraCapabilities Additional capabilities to be added.
175173
* @return DesiredCapabilities after the merge
176174
*/
177-
public DesiredCapabilities merge(
178-
org.openqa.selenium.Capabilities extraCapabilities) {
175+
@Override
176+
public DesiredCapabilities merge(Capabilities extraCapabilities) {
179177
if (extraCapabilities != null) {
180178
capabilities.putAll(extraCapabilities.asMap());
181179
}
@@ -286,7 +284,7 @@ public String toString() {
286284
}
287285

288286
private Map<String, Object> shortenMapValues(Map<String, Object> map) {
289-
Map<String, Object> newMap = Maps.newHashMap();
287+
Map<String, Object> newMap = new HashMap<>();
290288

291289
for (Map.Entry<String, Object> entry : map.entrySet()) {
292290
if (entry.getValue() instanceof Map) {

0 commit comments

Comments
 (0)