• Selenium Video Tutorials

Selenium WebDriver - Chrome Options



ChromeOptions is a specific class in Selenium Webdriver which helps to handle options which are only applicable to the Chrome driver. It helps to modify the settings and capabilities of the browser while running an automated test on Chrome. The ChromeOptions class extends another class known as the MutableCapabilities class.

The ChromeOptions class was added from the Selenium 3.6 version onwards. Selenium Webdriver begins with a fresh browser profile without any predefined settings on cookies, history, and so on by default.

Example 1 - Add Chrome Extensions Using ChromeOptions

Let us take an example, where we would launch the Chrome browser with a Selenium IDE extension using the ChromeOptions class. Every Chrome extension should have a .crx file.

Selenium Chrome Options 1

We would get the .crx file for Selenium IDE Chrome extension and place it under the Resources folder within our test project.

package org.example;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.io.File;
import java.util.concurrent.TimeUnit;

public class ChromeOptnsExtension {
   public static void main(String[] args) throws InterruptedException {

      // object of ChromeOptions
      ChromeOptions opt = new ChromeOptions();

      // adding .crx extension
      opt.addExtensions(new File("./Resources/SeleniumIDE.crx"));

      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver(opt);

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // Opening the webpage with Selenium IDE extension
      driver.get("https://www.tutorialspoint.com/selenium/practice/register.php");

   }
}

Output

Process finished with exit code 0

In the above example, we observed that the Chrome browser got launched with the Selenium IDE extension along with the information bar Chrome is being controlled by automated test software.

Selenium Chrome Options 2

Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.

Example 2 - Disable Infobar Using ChromeOptions

In the previous example, we obtained an information bar with the text Chrome is being controlled by automated test software, however we can disable this information bar using the ChromeOptions class.

package org.example;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.io.File;
import java.util.Collections;
import java.util.concurrent.TimeUnit;

public class ChromeOptns {
   public static void main(String[] args) throws InterruptedException {

      // object of ChromeOptions
      ChromeOptions opt = new ChromeOptions();

      // adding .crx extensions
      opt.addExtensions(new File("./Resources/SeleniumIDE.crx"));

      // disable information bar
      opt.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));


      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver(opt);

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // Opening the webpage with disabling information bar
      driver.get("https://www.tutorialspoint.com/selenium/practice/register.php");
   }
}

Output

Process finished with exit code 0

In the above example, we observed that the Chrome browser was launched with the Selenium IDE extension without the information bar Chrome is being controlled by automated test software.

Selenium Chrome Options 3

Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.

Example 3 - Open Maximized Browser Using ChromeOptions

In this example, we would open and launch an application in the browser maximized size using the ChromeOptions class.

package org.example;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.io.File;
import java.util.Collections;
import java.util.concurrent.TimeUnit;

public class ChromeOptnsMaximized {
   public static void main(String[] args) throws InterruptedException {

      // object of ChromeOptions
      ChromeOptions opt = new ChromeOptions();

      // adding .crx extensions
      opt.addExtensions(new File("./Resources/SeleniumIDE.crx"));

      // disable information bar
      opt.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));

      // open browser in maximized
      opt.addArguments("--start-maximized");

      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver(opt);

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // Opening the webpage with Selenium IDE extension
      driver.get("https://www.tutorialspoint.com/selenium/practice/register.php");

      // quitting browser
      driver.quit();
   }
}

Output

Process finished with exit code 0

In the above example, we observed that the Chrome browser was launched with the Selenium IDE extension without the information bar Chrome is being controlled by automated test software and in a maximized browser.

Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.

Example 4 - SSL Certificates Using ChromeOptions

To handle SSL certificates in Chrome, we have to use the ChromeOptions class along with the DesiredCapabilities class. In order to have the capabilities defined with the DesiredCapabilities to be available with the ChromeOptions we would need the help of the merge method from the ChromeOptions class. The SSL error shown in Chrome is Your connection is not private.

package org.example;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.util.concurrent.TimeUnit;

public class SSLErrorChrome {
   public static void main(String[] args) throws InterruptedException {
   
      // Desired Capabilities class to profile Chrome
      DesiredCapabilities dc = new DesiredCapabilities();
      dc.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);
      
      // Chrome Options class
      ChromeOptions opt = new ChromeOptions();
      
      // merging browser capabilities with options
      opt.merge(dc);
      
      // Initiate the Webdriver with options
      WebDriver driver = new ChromeDriver(opt);
      
      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
      
      // launch application with SSL error
      driver.get("https://expired.badssl.com");
      
      // get browser title
      System.out.println("Browser title in Chrome: " + driver.getTitle());
      
      // quiting the browser
      driver.quit();
   }
}

Output

Browser title in Chrome: Privacy error
Process finished with exit code 0

In the above example, we had the SSL certificate error in Chrome and launched the application and then obtained the browser title with the message in the console - Browser title in Chrome: Privacy error.

Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.

This concludes our comprehensive take on the tutorial on Selenium Webdriver - Chrome Options. We’ve started with describing a ChromeOptions class, and walked through examples of how to add extensions to Chrome browser, how to disable information bar, how to maximize the browser, and handle SSL certificates errors taking help of ChromeOptions along with Selenium Webdriver. This equips you with in-depth knowledge of the ChromeOptions class in Selenium Webdriver. It is wise to keep practicing what you’ve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.

Advertisements