Skip to content

Instantly share code, notes, and snippets.

@shs96c
Last active August 31, 2017 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shs96c/42f336ba917d61f0a326696c55d9cb4f to your computer and use it in GitHub Desktop.
Save shs96c/42f336ba917d61f0a326696c55d9cb4f to your computer and use it in GitHub Desktop.
W3CCapabilities
package org.openqa.selenium.remote;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class W3CCapabilities implements Capabilities {
private Map<String, Object> metadata;
private Map<String, ?> alwaysMatch;
private final List<Map<String, ?>> firstMatch;
public W3CCapabilities() {
this.metadata = new HashMap<>();
this.firstMatch = new ArrayList<>();
setAlwaysMatch(new ImmutableCapabilities(new HashMap<>()));
}
public W3CCapabilities(Capabilities alwaysMatch, Capabilities... firstMatch) {
this();
setAlwaysMatch(alwaysMatch);
for (Capabilities caps : firstMatch) {
addFirstMatch(caps);
}
}
public W3CCapbilities addMetadata(String key, Object value) {
Preconditions.checkNotNull(key, "Key is not set");
Preconditions.checkNotNull(value, "Value is not set");
if ("capabilities".equals(key)) {
throw new IllegalArgumentException("No you don't");
}
metadata.put(key, value);
return this;
}
public W3CCapabilities setAlwaysMatch(Capabilities caps) {
this.alwaysMatch = Preconditions.checkNotNull(
caps,
"Always matching capabilities must not be null").asMap();
return validate();
}
public W3CCapabilities addFirstMatch(Capabilities caps) {
Preconditions.checkNotNull(caps, "First match capabilities must not be null");
firstMatch.add(caps.asMap());
return validate();
}
public Stream<Capabilities> stream() {
if (firstMatch.isEmpty()) {
return Stream.of(alwaysMatch).map(ImmutableCapabilities::new);
}
return firstMatch.stream()
.map(c -> ImmutableMap.<String, Object>builder().putAll(alwaysMatch).putAll(c).build())
.map(ImmutableCapabilities::new);
}
protected Map<String, Object> toJson() {
  ImmutableMap.Builder builder = ImmutableMap.builder();
builder.putAll(metadata);
builder.put(
"capabilities", ImmutableMap.of(
"alwaysMatch", alwaysMatch,
"firstMatch", firstMatch));
return builder.build();
}
@Override
public Map<String, ?> asMap() {
// Smoosh everything together
ImmutableMap.Builder<String, Object> toReturn = ImmutableMap.builder();
toReturn.putAll(alwaysMatch);
firstMatch.forEach(toReturn::putAll);
return toReturn.build();
}
@Override
public Object getCapability(String capabilityName) {
// Be consistent in what we return.
return asMap().get(capabilityName);
}
private W3CCapabilities validate() {
Set<String> alwaysKeys = alwaysMatch.keySet();
Set<Set<String>> firstMatchKeys = firstMatch.stream()
.map(Map::keySet)
.collect(Collectors.toSet());
Sets.SetView<String> intersection = Sets.intersection(alwaysKeys, firstMatchKeys);
if (!intersection.isEmpty()) {
throw new IllegalStateException(
"There are items that appear in alwaysMatch and firstMatch: " + intersection);
}
return this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment