com.version1.webdriver.configuration.driver.ConfiguredChromeDriver Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of selenium-java-test-automation Show documentation
Show all versions of selenium-java-test-automation Show documentation
A simple Selenium framework offering externalised configuration, a good selection of libraries for supporting
test data, simple WebDriver browser binary resolution and an opinionated approach for WebDriver test design.
package com.version1.webdriver.configuration.driver;
import com.fasterxml.jackson.databind.JsonNode;
import com.version1.webdriver.configuration.BrowserType;
import com.version1.webdriver.configuration.TestConfigHelper;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class ConfiguredChromeDriver implements ConfiguredDriver {
/**
*
*
* @return WebDriver representing RemoteWebDriver grid
*/
public WebDriver getRemoteDriver() throws IOException {
return new RemoteWebDriver(
TestConfigHelper.get().getGridConfig().getGridUrl(), this.getOptions());
}
/**
*
* @return WebDriver representing RemoteWebDriver grid
* @throws IOException if log directory doesn't exist
*/
public WebDriver getLocalDriver() throws IOException {
createLogDirectory();
System.setProperty("webdriver.chrome.logfile", "logs/chrome-driver.log");
WebDriverManager.chromedriver().setup();
return new ChromeDriver(this.getOptions());
}
/**
*
* @return configured options object for target browser driver
*/
public ChromeOptions getOptions() throws IOException {
ChromeOptions chromeOptions = new ChromeOptions();
Map chromePrefs = new HashMap<>();
Iterator> browserPreferences = TestConfigHelper.get()
.getBrowserPreferences(BrowserType.CHROME)
.fields();
while (browserPreferences.hasNext()) {
Map.Entry entry = browserPreferences.next();
JsonNode value = entry.getValue();
String key = entry.getKey();
switch (value.getNodeType()) {
case BOOLEAN:
chromePrefs.put(key, value.asBoolean());
break;
case NUMBER:
chromePrefs.put(key, value.asInt());
break;
default:
if (key.equals("download.default_directory")) {
chromePrefs.put(key, createFileDownloadDirectory(value.asText()));
} else {
chromePrefs.put(key, value.asText());
}
}
}
chromeOptions.setExperimentalOption("prefs", chromePrefs);
chromeOptions.setHeadless(TestConfigHelper.get().isHeadless());
return chromeOptions;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy