The browser preferences and behavior can be set to specific defaults when the driver is created, set on the fly using optional parameters, or set as system properties. Preferences can be set for different languages, geolocations, focus, download folders, and so on. This section will cover the basics of how to set default preferences and capabilities in the driver method.
Using preferences to support browsers and platforms
Browser preferences
- Firefox: Preferences for this browser are set using the FirefoxProfile class, the FirefoxOptions class, and Desired Capabilities. The list of preferences and options set in the profile are then passed to the driver as DesiredCapabilites. The following example shows various profile preferences passed into the driver as default settings using both profile preferences and Desired Capabilities:
switch (browser) {
case "firefox":
caps = DesiredCapabilities.firefox();
FirefoxOptions ffOpts = new FirefoxOptions();
FirefoxProfile ffProfile = new FirefoxProfile();
ffProfile.setPreference("browser.autofocus",
true);
caps.setCapability(FirefoxDriver.PROFILE,
ffProfile);
caps.setCapability("marionette",
true);
webDriver.set(new FirefoxDriver(caps));
// Selenium 3.7.x
// webDriver.set(new FirefoxDriver(ffOpts.merge(caps)));
}
break;
}
accessibility.AOM.enabled; false
accessibility.accesskeycausesactivation; true
accessibility.blockautorefresh; false
...
-
Chrome: Preferences for this browser are set using the ChromeOptions class and Desired Capabilities. The list of preferences and/or arguments are then passed to the driver as DesiredCapabilites. The following example shows various preferences and arguments passed into the driver as default settings using both preferences and Desired Capabilities:
switch (browser) {
case "chrome":
caps = DesiredCapabilities.chrome();
ChromeOptions chOptions = new ChromeOptions();
Map<String, Object> chromePrefs =
new HashMap<String, Object>();
chromePrefs.put("credentials_enable_service",
false);
chOptions.setExperimentalOption("prefs",
chromePrefs);
chOptions.addArguments("--disable-plugins",
"--disable-extensions",
"--disable-popup-blocking");
caps.setCapability(ChromeOptions.CAPABILITY,
chOptions);
caps.setCapability("applicationCacheEnabled",
false);
webDriver.set(new ChromeDriver(caps));
// Selenium 3.7.x
// webDriver.set(new ChromeDriver(chOptions.merge(caps)));
break;
}
- Internet Explorer, Safari, and Microsoft Edge:Â Preferences for these browsers are also set using the InternetExplorerOptions, SafariOptions, EdgeOptions classes, and Desired Capabilities. Users can query for the available options and capabilities for each of these browsers. The following code sample shows an abbreviated case for each.
For Internet Explorer:
switch (browser) {
case "internet explorer":
caps = DesiredCapabilities.internetExplorer();
InternetExplorerOptions ieOpts =
new InternetExplorerOptions();
ieOpts.requireWindowFocus();
ieOpts.merge(caps);
caps.setCapability("requireWindowFocus",
true);
webDriver.set(new InternetExplorerDriver(caps));
// Selenium 3.7.x
// webDriver.set(new InternetExplorerDriver(
ieOpts.merge(caps)));
break;
}
For Safari:
switch (browser) {
case "safari":
caps = DesiredCapabilities.safari();
SafariOptions safariOpts = new SafariOptions();
safariOpts.setUseCleanSession(true);
caps.setCapability(SafariOptions.CAPABILITY,
safariOpts);
caps.setCapability("autoAcceptAlerts",
true);
webDriver.set(new SafariDriver(caps));
// Selenium 3.7.x
// webDriver.set(new SafariDriver(safariOpts.merge(caps)));
break;
}
For Microsoft Edge:
switch(browser) {
case "microsoftedge":
caps = DesiredCapabilities.edge();
EdgeOptions edgeOpts = new EdgeOptions();
edgeOpts.setPageLoadStrategy("normal");
caps.setCapability(EdgeOptions.CAPABILITY,
edgeOpts);
caps.setCapability("requireWindowFocus",
true);
webDriver.set(new EdgeDriver(caps));
// Selenium 3.7.x
// webDriver.set(new EdgeDriver(edgeOpts.merge(caps)));
break;
}
- Internet Explorer options can be found at https://seleniumhq.github.io/selenium/docs/api/dotnet/html/T_OpenQA_Selenium_IE_InternetExplorerOptions.htm
- Safari options can be found at https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/safari/SafariOptions.html
- Edge options can be found at https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/edge/EdgeOptions.html
Platforms
There are some specific system properties that need to be set for each driver; specifically, the path to the local driver in the GIT repository of the project. By storing the driver in the project, users will not have to download or install the drivers for each browser when testing locally from their IDE. The path also depends on the OS of the development platform. The following examples are for Windows platforms:
- Firefox:
System.setProperty("webdriver.gecko.driver","gecko_driver_windows_path/geckodriver.exe"); - Chrome:
System.setProperty("webdriver.chrome.driver","chrome_driver_windows_path/chromedriver.exe"); - IE:
System.setProperty("webdriver.ie.driver","ie_driver_windows_path/IEDriverServer.exe"); - Edge:
System.setProperty("webdriver.edge.driver","edge_driver_windows_path/MicrosoftWebDriver.exe"); - Safari: The Safari driver is now built into the browser by Apple