net.thucydides.core.webdriver.CapabilitySet Maven / Gradle / Ivy
package net.thucydides.core.webdriver;
import com.google.common.base.CharMatcher;
import com.google.common.base.Splitter;
import net.serenitybdd.core.collect.NewList;
import java.util.HashMap;
import net.thucydides.core.util.EnvironmentVariables;
import org.apache.commons.lang3.StringUtils;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static net.thucydides.core.ThucydidesSystemProperty.SERENITY_DRIVER_CAPABILITIES;
/**
* A set of user-defined capabilities to be used to configure the WebDriver driver.
* Capabilities should be passed in as a semi-colon-separated list of key:value pairs, e.g.
* "build:build-1234; max-duration:300; single-window:true; tags:[tag1,tag2,tag3]"
*/
class CapabilitySet {
private final EnvironmentVariables environmentVariables;
private static final CharMatcher CAPABILITY_SEPARATOR = CharMatcher.anyOf(";");
CapabilitySet(EnvironmentVariables environmentVariables) {
this.environmentVariables = environmentVariables.copy();
}
public Map getCapabilities() {
Map capabilitiesMap = new HashMap();
String specifiedCapabilities = SERENITY_DRIVER_CAPABILITIES.from(environmentVariables);
if (StringUtils.isNotEmpty(specifiedCapabilities)) {
Iterable capabilityValues = Splitter.on(CAPABILITY_SEPARATOR).trimResults().split(specifiedCapabilities);
capabilitiesMap = addCapabilityMapValues(capabilityValues);
}
return capabilitiesMap;
}
private Map addCapabilityMapValues(Iterable capabilityValues) {
Map capabilitiesMap = new HashMap();
for(String capability : capabilityValues) {
CapabilityToken token = new CapabilityToken(capability);
if (token.isDefined()) {
capabilitiesMap.put(token.getName(), asObject(token.getValue()));
}
}
return capabilitiesMap;
}
private Object asObject(String value) {
if (StringUtils.isNumeric(value)) {
return Integer.parseInt(value);
}
if (value.toLowerCase().equals("true") || value.toLowerCase().equals("false")) {
return Boolean.parseBoolean(value);
}
if (isAList(value)) {
return asList(value);
}
return value;
}
private List