Manage driver via a common class: Config
public final class Config {
private static final String DEFAULT_BROWSER = "ie";
private static Logger log = LoggerFactory.getLogger(Config.class);
private static ThreadLocal<RemoteWebDriver> drivers = new ThreadLocal<RemoteWebDriver>();
public static RemoteWebDriver driver() {
return Config.drivers.get();
}
public static void setupRemoteWebDriver(final String url, final DesiredCapabilities capabilities) throws MalformedURLException {
Config.drivers.set(new RemoteWebDriver(new URL(url + "/wd/hub"), capabilities));
public static void setupWebDriver(String browser, final String url) throws Exception {
if (StringUtils.isEmpty(browser)) {
browser = Config.DEFAULT_BROWSER;
}
if (url != null) {// remote browser
if (browser.equalsIgnoreCase("ie")) {
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
capabilities.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);
capabilities.setCapability(InternetExplorerDriver.NATIVE_EVENTS, false);
capabilities.setCapability(InternetExplorerDriver.ENABLE_PERSISTENT_HOVERING, true);
setupRemoteWebDriver(url, capabilities);
} else if (browser.equalsIgnoreCase("firefox")) {
setupRemoteWebDriver(url, DesiredCapabilities.firefox());
} else if (browser.equalsIgnoreCase("chrome")) {
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
//options.addArguments("--disable-extensions");
options.setExperimentalOption("useAutomationExtension", false);
//options.addArguments("disable-infobars");
// options.addArguments("--enable-precise-memory-info");
// options.addArguments("--disable-popup-blocking");
// options.addArguments("--disable-default-apps");
// options.addArguments("test-type=browser");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
setupRemoteWebDriver(url, capabilities);
} else {
Config.log.error("Browser do not support: " + browser);
}
Config.drivers.set(driver());
Config.drivers.get().manage().window().maximize();
} else {// local browser
RemoteWebDriver driver = null;
if (browser.equalsIgnoreCase("ie")) {
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
System.setProperty("webdriver.ie.driver",
"C:/xxxx/IEDriverServer.exe");
driver = new InternetExplorerDriver(capabilities);
} else if (browser.equalsIgnoreCase("firefox")) {
// FirefoxDriverManager.getInstance().version("0.14").setup();
// driver = new FirefoxDriver();
System.setProperty("webdriver.gecko.driver",
"C:/xxxxx/geckodriver-v0.17.0-win64/geckodriver.exe");
driver = new FirefoxDriver();
} else if (browser.equalsIgnoreCase("chrome")) {
// ChromeDriverManager.getInstance().setup();
System.setProperty("webdriver.chrome.driver",
"C:/xxxxxx/chromedriver_win32_v2.34/chromedriver.exe");
driver = new ChromeDriver();
} else {
throw new IllegalArgumentException("Browser not support: " + browser);
}
Config.drivers.set(driver);
Config.drivers.get().manage().window().maximize();
}
}
}