Chrome Options & Desired Capabilities in Selenium Webdriver

What is Chrome Options Class?

The Chromeoptions Class is a concept in Selenium WebDriver for manipulating various properties of the Chrome driver. The Chrome options class is generally used in conjunction with Desired Capabilities for customizing Chrome driver sessions. It helps you perform various operations like opening Chrome in maximized mode, disable existing extensions, disable pop-ups, etc.

Example:

Below example shows a way to open Chrome browser in maximized mode using ChromeOptions class. We need to pass an instance of ChromeOptions class to the web driver initialization.

ChromeOptions options = new ChromeOptions()
options.addArgument("start-maximized");
ChromeDriver driver = new ChromeDriver(options);

Below are the list of available and most commonly used arguments for ChromeOptions class

  • start-maximized: Opens Chrome in maximize mode
  • incognito: Opens Chrome in incognito mode
  • headless: Opens Chrome in headless mode
  • disable-extensions: Disables existing extensions on Chrome browser
  • disable-popup-blocking: Disables pop-ups displayed on Chrome browser
  • make-default-browser: Makes Chrome default browser
  • version: Prints chrome browser version
  • disable-infobars: Prevents Chrome from displaying the notification ‘Chrome is being controlled by automated software

Desired Capabilities class

Desired Capabilities Class is used to modify multiple properties of web drivers. It provides key-value pairs to change individual properties of web drivers such as browser name, browser platform, etc. A common method of Desired Capabilities class is the setCapability method. It is mostly used with Selenium Grid, where the same test case needs to be executed on different browsers.

Example:

Below example shows the way to enable chrome browser to accept SSL certificates on websites by default using Desired Capabilities for Chrome class.

// Create an object of desired capabilities class with Chrome driver
DesiredCapabilities SSLCertificate = DesiredCapabilities.chrome();
// Set the pre defined capability – ACCEPT_SSL_CERTS value to true
SSLCertificate.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
// Open a new instance of chrome driver with the desired capability
WebDriver driver = new ChromeDriver(SSLCertificate);

Below are the most commonly used pre-defined capability types.

Capability Name Description
ACCEPT_SSL_CERTS This property tells the browser to accept SSL Certificates by default
PLATFORM_NAME This property is used to set the operating system platform used to access the web site
BROWSER_NAME This property is used to set the browser name for a web driver instance
VERSION This property to used to set the browser version

Chrome Options for Adblocker extension

Adblocker extension of the Chrome browser can be handled using ChromeDriver Options and Desired Capabilities class. Below are the steps to access AdBlocker extension on the Chrome browser using Desired Capabilities class.

Step 1) AdBlocker extension must be installed on Chrome browser before using Chrome Options class

Step 2) Extract the CRX File corresponding to AdBlocker extension through http://crxextractor.com/

Step 3) Pass the downloaded CRX File path to Chrome Options class

Step 4) Instantiate the web driver using the desired capabilities class and Chrome Options in Selenium object

Example:

Below example demonstrates how to activate ad blocker extension on the Chrome browser using Chrome Options and Desired Capabilities class.

ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("Path to CRX File"));
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
ChromeDriver driver = new ChromeDriver(capabilities);

Extract CRX File

Below steps demonstrate the process of extracting CRX File through Ad Blocker through the web site – http://crxextractor.com/

Step 1) Go to http://crxextractor.com/ and click start button

Extract CRX File

Step 2) Enter the chrome extension – Ad Blocker URL under the textbox. URL for Adblock on Chrome web store is https://chrome.google.com/webstore/detail/adblock-%E2%80%94-best-ad-blocker/gighmmpiobklfepjocnamgkkbiglidom

and click ok

Extract CRX File

Step 3) On clicking the OK Button, the label of the button will change to Get .CRX as below. Click on Get .CRX button, CRX file corresponding to the extension will be downloaded

Extract CRX File

Step 4) Save the file onto the local machine, make a note of the path saved. The next step is to pass the saved path to Chrome Options class

Sample Code:

  1. You will see ads at http://demo.guru99.com/ as below

Extract CRX File

  1. With AdBlocker extension enabled on Chrome browser ads should be disabled
package adblock;
import java.io.File;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;


public class AdblockDemo {
public static void main(String[] args)  {
	
	System.setProperty("webdriver.chrome.driver","X://chromedriver.exe");		
	ChromeOptions options = new ChromeOptions();
	options.addExtensions(new File("X://extension_3_40_1_0.crx")); 
	DesiredCapabilities capabilities = new DesiredCapabilities();
	capabilities.setCapability(ChromeOptions.CAPABILITY, options);
	options.merge(capabilities);
	ChromeDriver driver = new ChromeDriver(options);
	driver.get("http://demo.guru99.com/test/simple_context_menu.html");
	driver.manage().window().maximize();
	//driver.quit();
	}
	
}

Code Explanation:

  1. Initially, you need to set the path to the chromedriver.exe file using set property method since you are using Chrome Browser for testing
  2. You need to set the path to CRX File to add extensions method
  3. Then you need to create an object of Chrome Desired Capabilities in Selenium class and pass it to web driver instance. From Selenium 3.8.1 version, driver capabilities class is deprecated and you need to merge capabilities object with Chrome Options object before passing the same as an argument to Chrome Driver constructor
  4. Open the URL – http://demo.guru99.com/test/simple_context_menu.html with Ad Blocker extension enabled
  5. Maximize and close the browser

NOTE: We are enabling AdBlocker extension on the Chrome browser through automation script instead of manually enabling Adblocker extension on the Chrome browser. CRX File is a way to access ad blocker extension using automation script

Output:

Chrome browser will be enabled with AdBlocker extension enabled as below without any ads

Extract CRX File

Chrome Options for Incognito mode

Chrome Options can be used for incognito mode by using the pre-defined argument –incognito.

Below is the sample code to accomplish the same.

Sample Code:

package test;
import java.io.File;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;

public class Incognito{
public static void main(String[] args) {
	// TODO Auto-generated method stub
	System.setProperty("webdriver.chrome.driver","X://chromedriver.exe");
	ChromeOptions options = new ChromeOptions();
	options.addArguments("--incognito");
	DesiredCapabilities capabilities = new DesiredCapabilities();
	capabilities.setCapability(ChromeOptions.CAPABILITY, options);
	options.merge(capabilities);
	ChromeDriver driver = new ChromeDriver(options);		
	driver.get("http://demo.guru99.com/test/simple_context_menu.html");
	driver.manage().window().maximize();		
	//driver.quit();
	}
	}

Code Explanation:

  1. Initially, you need to set the path to the chromedriver.exe file using set property method since you are using Chrome Browser for testing
  2. Then you need to create an object of Chrome Options class and pass it to web driver instance. Since we want to open Chrome browser in incognito mode, you need to pass the argument –incognito to Chrome Options class.
  3. Next, create an object of Desired Capabilities class and merge the Desired Capabilities class object with Chrome Options class object using merge method
  4. You need to create an object of Chrome Driver class and pass the Chrome Options object as an argument
  5. Finally, we need to pass the URL – http://demo.guru99.com/test/simple_context_menu.html to the driver.get method
  6. Maximize and close the browser

Output:

The chrome browser window will be opened in Incognito mode as below

Chrome Options For Incognito Mode

Chrome Options for Headless Chrome

A Headless browser runs in the background. You will not see the browser GUI or the operations been operated on it.

Chrome Options for running Chrome browser in headless mode can be accomplished by using the predefined arguments –headless.

Sample code to accomplish it is mentioned below.

Example:

package test;
import java.io.File;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;


public class HeadlessModeDemo {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.setProperty("webdriver.chrome.driver","X://chromedriver.exe");
		ChromeOptions options = new ChromeOptions();
		options.addArguments("--headless");
		DesiredCapabilities capabilities = new DesiredCapabilities();
		capabilities.setCapability(ChromeOptions.CAPABILITY, options);
		options.merge(capabilities);
		ChromeDriver driver = new ChromeDriver(options);		
		driver.get("http://demo.guru99.com/");
		driver.manage().window().maximize();
		String title = driver.getTitle();
		System.out.println("Page Title: " +title);
		driver.quit();
		}


}

Code Explanation:

  1. Initially, you need to set the path to the chromedriver.exe file using set property method since you are using Chrome Browser for testing
  2. Next, create an object of Chrome Options class and pass it to web driver instance. Since we want to open Chrome browser in headless mode, we need to pass the argument –headless to Chrome Options class.
  3. Create an object of DesiredCapabilities Chrome class and merge the Desired Capabilities class object with Chrome Options class object using merge method
  4. Create an object of Chrome Driver class and pass the Chrome Options Selenium object as an argument
  5. Finally, we need to pass the URL – http://demo.guru99.com/ to the driver.get method
  6. Print the page title and close the browser

Output

The browser will not be visible for the above code as Chrome will be working in Headless mode. Page title will be fetched and displayed as below.

Chrome Options For Headless Chrome

Summary

  • Selenium Chrome Options class is used to manipulate various properties of Chrome driver
  • Desired Chrome Capabilities class provides a set of key-value pairs to modify individual properties of web driver such as browser name, browser platform, etc.
  • To manipulate any extensions of Chrome browser, CRX File corresponding to the extension must be extracted and must be added to Chrome Options class
  • –incognito and –headless are predefined arguments provided by Chrome Options class for using Chrome browser in incognito mode and headless mode