net.thucydides.core.webdriver.SupportedWebDriver Maven / Gradle / Ivy
package net.thucydides.core.webdriver;
import com.google.common.base.Joiner;
import com.google.common.base.Optional;
import com.opera.core.systems.OperaDriver;
import io.appium.java_client.AppiumDriver;
import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.safari.SafariDriver;
/**
* The list of supported web drivers.
* These are the drivers that support screenshots.
*/
public enum SupportedWebDriver {
/**
* Firefox WebDriver driver.
*/
FIREFOX(FirefoxDriver.class),
/**
* Chrome WebDriver driver.
*/
CHROME(ChromeDriver.class),
/**
* Opera - use it with SauceLabs
*/
OPERA(OperaDriver.class),
/**
* HTMLUnit - fast, but no screenshots.
*/
HTMLUNIT(HtmlUnitDriver.class),
/**
* Phantom-JS driver - headless javascript.
*/
PHANTOMJS(PhantomJSDriver.class),
/**
* Remote web driver
*/
REMOTE(RemoteWebDriver.class),
/**
* Internet Explorer
*/
IEXPLORER(InternetExplorerDriver.class),
/**
* Safari
*/
SAFARI(SafariDriver.class),
/**
* Appium
*/
APPIUM(AppiumDriver.class),
/**
* A user-provided driver
*/
PROVIDED(ProvidedDriver.class);
private final Class webdriverClass;
private SupportedWebDriver(Class webdriverClass) {
this.webdriverClass = webdriverClass;
}
public Class getWebdriverClass() {
return webdriverClass;
}
/**
* HTMLUnit - mainly for testing, as this driver does not support screenshots or much AJAX.
*/
public static String listOfSupportedDrivers() {
return Joiner.on(", ").join(SupportedWebDriver.values());
}
public static SupportedWebDriver getClosestDriverValueTo(final String value) {
SupportedWebDriver closestDriver = null;
int closestDriverDistance = Integer.MAX_VALUE;
for (SupportedWebDriver supportedDriver : values()) {
int distance = StringUtils.getLevenshteinDistance(supportedDriver.toString(), value);
if (distance < closestDriverDistance) {
closestDriverDistance = distance;
closestDriver = supportedDriver;
}
}
return closestDriver;
}
public static SupportedWebDriver getDriverTypeFor(final String value) throws UnsupportedDriverException {
try {
return SupportedWebDriver.valueOf(value.toUpperCase());
} catch (IllegalArgumentException e) {
SupportedWebDriver closestMatchingDriver = getClosestDriverValueTo(value);
throw new UnsupportedDriverException("Unsupported browser type: " + value
+ ". Did you mean " + closestMatchingDriver.toString().toLowerCase()
+ "?", e);
}
}
public static SupportedWebDriver forClass(Class driverClass) {
for(SupportedWebDriver supportedWebDriver : values()) {
if (driverClass.equals(supportedWebDriver.getWebdriverClass())) {
return supportedWebDriver;
}
}
throw new IllegalArgumentException("Driver not supported: " + driverClass);
}
}