Rather than hardcoding default URLs, paths, revisions, mobile device settings, and so on into the driver class itself, it makes more sense to encapsulate all those settings into a properties file. This way, users do not have to traverse through code to change a setting, driver version, or any paths required to support running the driver across platforms such as Windows, iOS, and Linux. Also, different sets of properties can be stored in the file for different environments such as local, remote, or third-party grids. Properties can be stored and retrieved in Java using the Properties class. The following code examples show property file formats, and the use of properties files in the Selenium driver class:
// Properties Class
public class CreateDriver {
private Properties driverProps = new Properties();
private static final String propertyFile = new File
("../myProject/com/path/selenium.properties").getAbsolutePath();
@SafeVarargs
public final void setDriver(String browser,
String environment,
String platform,
Map<String, Object>... optPreferences)
throws Exception {
DesiredCapabilities caps = null;
// load properties from file...
driverProps.load(new FileInputStream(propertyFile));
switch (browser) {
case "firefox":
caps = DesiredCapabilities.firefox();
// see previous example for caps...
if ( environment.equalsIgnoreCase("local") ) {
if ( platform.toLowerCase().contains("windows") ) {
System.setProperty("webdriver.gecko.driver",
driverProps.getProperty(
"gecko.driver.windows.path"));
}
webDriver.set(new FirefoxDriver(caps));
}
break;
}
}
Here is the selenium.properties file:
// selenium.properties file
# Selenium 3 WebDriver/AppiumDriver Properties File
# Revisions
selenium.revision=3.4.0
chrome.revision=2.30
safari.revision=2.48.0
gecko.revision=0.17.1
# Firefox Settings
gecko.driver.windows.path=../path/geckodriver-v0.17.1-win64/geckodriver.exe
gecko.driver.linux.path=../path/geckodriver-v0.17.1-linux64/geckodriver
gecko.driver.mac.path=../path/geckodriver-v0.17.1-macos/geckodriver