Launch Browser

Launch browser with default(anonymous) profile
To launch a browser, we need to at least:
1. specify path to the driver executable in System Properties
2. instantiate the corresponding webdriver.

Use firefox as an example:

System.setProperty(“webdriver.gecko.driver”,”C:\\Users\\yourUserName\\seleniumDownloads\\geckodriver.exe”);
FirefoxProfile profile = new FirefoxProfile();

Launch Browser with desired capabilities and options
Often we want to launch a browser with specific capabilities and option settings. We utilize DesiredCapabilities class and browser-specific options classes(ChromeOptions, EdgeOptions, FirefoxOptions…) for that.

Capabilities are defined in Key/Value pair, and then added to DesiredCapabilites by calling setCapability() method. And steps to create a browser session with specific capabilities:

  • Identify all of the capabilities that you want to arm your browser with.
  • Create a DesiredCapabilities class instance and set all of the capabilities to it.
  • Now, create an instance of WebDriver with all of the above capabilities passed to it.
File file = new File(“c:\\Users\\userName\\pathTo\\IEDriverServer.exe”);
System.setProperty(“webdriver.ie.driver”, file.getAbsolutePath());
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();

capabilities.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);
capabilities.setCapability(InternetExplorerDriver.NATIVE_EVENTS, false);

capabilities.setCapability(“unexpectedAlertBehaviour”, “accept”);
capabilities.setCapability(“ignoreProtectedModeSettings”, true);
capabilities.setCapability(“enablePersistentHover”, true);

driver = new InternetExplorerDriver(capabilities);

Set browser-specific options:

ChromeOptions options = new ChromeOptions();
options.addExtensions(new File(“/path/to/extension.crx”));
ChromeDriver driver = new ChromeDriver(options);

Or you can add the options to an already existing DesiredCapabilities object, which is useful when need to specify other WebDriver capabilities not specific to your chosen browser’s driver.

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
// Add the WebDriver proxy capability.
Proxy proxy = new Proxy();
proxy.setHttpProxy(“myhttpproxy:3337”); capabilities.setCapability(“proxy”, proxy);
// Add ChromeDriver-specific capabilities through ChromeOptions. ChromeOptions options = new ChromeOptions(); options.addExtensions(new File(“/path/to/extension.crx”)); capabilities.setCapability(ChromeOptions.CAPABILITY, options);
ChromeDriver driver = new ChromeDriver(capabilities);

Another example of using both capabilites and options class:

DesiredCapabilities caps = new FirefoxOptions()
.addPreference(“browser.startup.page”, 1)
.addPreference(“browser.startup.homepage”, “https://www.google.co.uk”)
.addTo(DesiredCapabilities.firefox());
WebDriver driver = new FirefoxDriver(caps);

Any new window opened by WebDriver is opened via the current profile. Once WebDriver is instantiated, it used the assigned profile. All cookies and sessions behave according to the profile settings

Where can I find a complete list of browser capabilities? What about browser-specific capabIlities & options?

To see the complete list of browser capabilities and browser-specific options:
https://github.com/SeleniumHQ/selenium/wiki/DesiredCapabilities

Starting from selenium 3.0, webdriver is developed&maintained by the browser themselves, so it is always a good idea to check browser’s update on its own webdriver info. Here are some useful links:

https://sites.google.com/a/chromium.org/chromedriver/capabilities
https://github.com/mozilla/geckodriver
https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/

WebDriver Support in Safari 10