ChromeOptions to configure Chrome Sessions in Selenium

This tutorial is in continuation to our previous tutorial which is on use of DesiredCapabilities in Selenium WebDriver to set the browser’s properties. The Desired Capabilities class is not used anymore to handle the browser’s properties in Selenium which is most probably due to change in the architecture of the latest versions of the browsers. If you want to set the capability properties for your browser then you need to use browser specific options properties, that is why today we are going to discuss the ChromeOptions class of Selenium remote library.

Let’s start today with the options properties for chrome, further, we will discuss remaining browser options class for the rest of the web browsers in subsequent tutorials.

When we use DesiredCapabilities class to handle the browser-based session (say Chrome here) then it underlines the ChromeDriver() which indicates that it has been deprecated in Java programming language. However, it is available to use in Python, Ruby and other programming languages. See image below:

ChromeOptions alternative to DesiredCapabilitiesWhat is ChromeOptions in Selenium WebDriver?

ChromeOptions is a class in Selenium which is used to set the capability and customization and configuration of the ChromeDriver session as well.  This class inherits the MutableCapabilities class. Here is the library path:

org.openqa.selenium.MutableCapabilities

Click here to know more about it.

MutableCapabilities class first came in existence with Selenium 3.6.0 and now all the options classes for respective browsers extends this class in Java. We need to import below package to work with Chrome Options in Java to customize the Chrome sessions.

import org.openqa.selenium.chrome.ChromeOptions;

What are the uses of ChromeOptions in Selenium?

ChromeOptions has all the uses which DesiredCapabilities was doing, but, it is only dedicated to one browser. Let’s have a look over some of the uses of the Chrome Options.

  • It is used to get and set capabilities in Chrome
  • Chrome Options supports to add the extension to the browser at runtime
  • It helps to add binary at runtime
  • ChromeOptions is used to handle the proxy while running test automation
  • We use ChromeOptions in Selenium Grid to remotely set the Chrome session in node machines

We will further discuss the implementation of the above uses in a separate article at inviul.com.

Methods of ChromeOptions class

Here is the list of some of the methods of the ChromeOptions class which we use them whenever required as per the criteria.

  • addArguments(arguments)
  • addEncodedExtensions()
  • addExtensions()
  • getCapability()
  • setCapability()
  • getPlatform()
  • setAcceptInsecureCerts()
  • setBinary()
  • setProxy()
  • setExperiementalOption()
  • setPageLoadStrategy()
  • setUnhandledPromptBehaviour()
  • getBrowserName()
  • getVersion()

How to set capability with ChromeOptions in Selenium WebDriver?

Following program uses Chrome Options class to set the capability to the ChromeDriver. Here we are setting browser name and Windows 10 as the platform with the help of Chrome options. Further, we set the instance of ChromeOptions as the argument in ChromeDriver().

Let’s have an eye to the programming part.

package Test;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;

public class ChromeOptionsDesiredCapabilitiesDemo {

	public static void main(String[] args) {
		
		ChromeOptions option = new ChromeOptions();
				
		option.setCapability(CapabilityType.BROWSER_NAME, "CHROME");
		
		option.setCapability(CapabilityType.PLATFORM, Platform.WIN10);
		
		System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver.exe");	
		 
		WebDriver  driver  = new ChromeDriver(option);
		
		 driver.get("http://www.inviul.com/");
	  	  
	  	  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		  
	  	  driver.manage().window().maximize();
	  	  
	  	  System.out.println("Platform is- "+option.getPlatform());
	  	 System.out.println(driver.getTitle());
	  	 
	  	 driver.close();
	  	 
	  	 driver.quit();
		

	}

}

This was all about setting ChromeOptions as the capability in Selenium WebDriver. If you have any queries, suggestions or feedback then feel free to write below in the comment form and don’t miss to join our Facebook group for quick and latest updates on Selenium WebDriver and Automation testing as well.

Join Inviul fb group

Leave a Reply