All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.thucydides.core.webdriver.CapabilitySet Maven / Gradle / Ivy

There is a newer version: 4.2.1
Show newest version
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 asList(String value) {
        String listContents = StringUtils.removeEnd(StringUtils.removeStart(value, "["), "]");
        List items = Splitter.on(",").trimResults().splitToList(listContents);
        return items.stream()
                .map(this::asObject)
                .collect(Collectors.toList());
    }

    private boolean isAList(String value) {
        return value.startsWith("[") && value.endsWith("]");
    }

    private static class CapabilityToken {
        private final String name;
        private final String value;

        private CapabilityToken(String capability) {
            int colonIndex = capability.indexOf(":");
            if (colonIndex >= 0)  {
                name = capability.substring(0, colonIndex);
                value = capability.substring(colonIndex + 1);
            } else {
                name = capability;
                value = null;
            }
        }

        public String getName() {
            return name;
        }

        public String getValue() {
            return value;
        }

        public boolean isDefined() {
            return (StringUtils.isNotEmpty(name) && StringUtils.isNotEmpty(value));
        }
    }
}