Chrome Options for running WebDriver Tests

ChromeOptions class extends MutableCapabilities. We can use ChromeOptions class to manage options specific to ChromeDriver.

MutableCapabilities was introduced for the first time with Selenium v3.6.0 (Java Client). All the Option classes now extends MutableCapbilities. Refer the JavaDoc link for org.openqa.selenium.MutableCapabilities.

And from Selenium v3.7.0, DesiredCapabilities was migrated to either MutableCapabilities or (preferably) ImmutableCapabilities.

We have to create an instance of ChromeOptions to set ChromeDriver specific capabilities and then pass the ChromeOptions object to the WebDriver/RemoteWebDriver constructor. If you are using latest version of Selenium, Start making Option classes instances of Capabilities which allows us to do like below :-

 ChromeOptions options = new ChromeOptions()
 options.addExtensions(new File("/path/to/extension.crx"))
 options.setBinary(new File("/path/to/chrome"));
 // For use with ChromeDriver:
 ChromeDriver driver = new ChromeDriver(options);

If you want to pass any capabilities, try to use ChromeOptions. if not you can still use DesiredCapabilities and then merge within ChromeOptions like below. We cannot directly pass capabilities to ChromeDriver constructor as Constructor ChromeDriver(Capabilities capabilities) is deprecated now.

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("something", true);

ChromeOptions options = new ChromeOptions();
options.addArguments("disable-infobars");
options.merge(capabilities);
 ChromeDriver driver = new ChromeDriver(options);

Earlier To run our tests with Selenium Grid, we have used RemoteWebDriver with DesiredCapabilities object to define which browser, version of the browser and platform (OS - Windows / LINUX etc) that we want to run our tests. But now with selenium version v3.6.0, we should start using like below :

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
driver = new RemoteWebDriver(new URL("http://10.x.x.x:4444/wd/hub"), capabilities);

So now with the ChromeOptions, it should looks like below : -

ChromeOptions options = new ChromeOptions();
driver = new RemoteWebDriver(new URL("http://10.x.x.x:4444/wd/hub"), options);

How to define capabilities using ChromeOptions for RemoteWebDriver

Once you create an instance of ChromeOptions, we can use 'setCapability()' method for setting ChromeDriver-specific capabilities and pass ChromeOptions object into the RemoteWebDriver constructor in the place of Capabilities.

Below are the setCapability methods which are inherited from class org.openqa.selenium.MutableCapabilities

public void setCapability(java.lang.String capabilityName, boolean value)
public void setCapability(java.lang.String capabilityName, java.lang.String value)
public void setCapability(java.lang.String capabilityName, Platform value)
public void setCapability(java.lang.String key, java.lang.Object value)

Below is the example to define ChromeDriver-specific capabilities using setCapability method:

ChromeOptions options = new ChromeOptions();
options.setCapability(CapabilityType.PLATFORM_NAME, Platform.WINDOWS);
options.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.ACCEPT);
options.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
driver = new RemoteWebDriver(new URL("http://10.x.x.x:4444/wd/hub"), options);
driver.manage().window().maximize();

Earlier to accept all SSL certs by defaults, we have enabled below capabilities using DesiredCapabilities like below :-

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(CapabilityType.SUPPORTS_ALERTS, true);
capabilities.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.ACCEPT);
WebDriver driver = new ChromeDriver(capabilities)

Now the same can be achieved by using ChromeOptions class like below: -

ChromeOptions options = new ChromeOptions();
options.setAcceptInsecureCerts(true);
options.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.ACCEPT);
WebDriver driver = new ChromeDriver(options)

If you see a log message as "Using `new ChromeOptions()` is preferred to `DesiredCapabilities.chrome()`", it is now recommended to use new ChromeOptions as above.

Below are few examples on how to use Chrome options when automating webdriver tests on chrome browser.

Chrome Automation Info-bar

When Chrome browser is launched using selenium, We can see an info bar with message 'Chrome is being controlled by automated test software'. If we want to hide this info bar, we have to pass "disable-infobars" argument to ChromeOptions.

Syntax:

//Adds additional command line arguments to be used when starting Chrome.
ChromeOptions  -   addArguments(java.util.List<java.lang.String> arguments)

Example usage:

ChromeOptions options = new ChromeOptions();
//disable automation info bar
options.addArguments("disable-infobars");
WebDriver driver = new ChromeDriver(options);

Load Default/Custom Chrome Profile :-

Whenever we launch chrome browser using selenium, a new instance/a temp profile will be created for each new session. If we want to load the default chrome browser or custom chrome profile, we can pass 'user-data-dir' argument to ChromeOptions which is Chrome command-line switch to tell Chrome which profile to use. If the path doesn’t exist, chrome will create a new profile in the specified path.

Syntax:-

 
ChromeOptions options = new ChromeOptions();
options.addArgument("user-data-dir=/path/to/your/custom/profile");

Example usage:-

ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=C:/Users/username/AppData/Local/Google/Chrome/User Data");
WebDriver driver = new ChromeDriver(options);

Using a Chrome executable in a non-standard location :-

If you have installed Chrome browser in any other location (non-standard location), we can use ChromeOptions to launch chrome executable by passing the chrome binary in ChromeOptions.

For Windows 8 and Windows 10, this is Chrome’s installed location: ‘C:\Program Files\Google\Chrome\Application’ by default. If the chrome browser is installed in any other directory, We have to setBinary options and pass the path of the Chrome executable in a non-standard location.

Syntax :-

ChromeOptions options = new ChromeOptions();
options.setBinary("/path/to/other/chrome/binary");

Headless Chrome

To run Chrome browser in a headless environment, we need send an aurgument "--headless" to ChromeOptions. This option will tell Google Chrome to execute in headless mode.

Earlier most of the headless options were standalone tools and were seperate from the browsers, such as HTMLUnit or PhantomJS. Now Google has implemented a headless option for Chrome using all the modern web platform features provided by Chromium and Blink.

Note:- Chrome Headless is available on Mac and Linux from Chrome v59 and for Windows it is from Chrome v60.

ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
options.addArguments("--disable-gpu");

Now you can also use a single line command options.setHeadless(true) which does the same job of adding the above two arguments.

Load Extensions

By default, when we launch the browser using Selenium, no extensions/addons will be added to it. If we want to install/load any extension/add-on to the browser on startup, we should use addExtensions option to launch Chrome browser with extensions.

Syntax:-


//Adds a new Chrome extension to install on browser startup.
ChromeOptions - addExtensions(java.util.List paths)

Example:-

// Add ChromeDriver-specific capabilities through ChromeOptions.
String extFilePath = "D:/extensions/Google-Keep-Chrome-Extension_v3.1.16302.1110.crx";
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File(extFilePath));

I have download google keep chrome browser extension and ad the file path to chrome options. Now when we launch chrome browser, we can see google keep icon displayed on the top-right corner of your browser-window, next to the address bar.

Selenium Tutorials: 

Add new comment