chrome-options-and-desire-capabilities

Chrome Options & Desired Capabilities

Table of Contents

Introduction to Chrome Options Class:

The Chrome Options class is employed to manage Chrome Driver properties and works effectively when combined with Desired Capabilities.

In the following example, we demonstrate how the Chrome Options class is utilized to launch the Chrome browser in maximized mode.

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

Several frequently used arguments in the ChromeOptions class include:

  • start-maximized: Opens Chrome in maximized mode.
  • incognito: Launches Chrome in incognito mode.
  • headless: Operates Chrome in headless mode (without a graphical interface).
  • disable-extensions: Disables existing extensions on the Chrome browser.
  • disable-popup-blocking: Prevents pop-ups from appearing on the Chrome browser.
  • make-default-browser: Sets Chrome as the default browser.
  • version: Displays the Chrome browser version.
  • disable-infobars: Stops Chrome from displaying notifications.

Desired Capabilities Class:

The Desired Capabilities class is designed to modify web driver properties. It consists of key-value pairs to alter specific properties of the web driver, such as browser name or browser platform. The setCapability method is one of the commonly employed methods within the Desired Capabilities class.

Enabling SSL Certificates with Desired Capabilities:

In the provided example, Desired Capabilities is used to configure the Chrome browser to accept SSL certificates from websites.

DesiredCapabilities SSLCertificate = DesiredCapabilities.chrome();
SSLCertificate.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
WebDriver driver = new ChromeDriver(SSLCertificate);

Some frequently used predefined capability types include:

  • ACCEPT_SSL_CERTS: Instructs the browser to accept SSL certificates.
  • PLATFORM_NAME: Sets the Operating System platform.
  • BROWSER_NAME: Specifies the browser name.
  • VERSION: Sets the browser version.

Handling Adblocker Extension with Chrome Options:

To manage the ad blocker extension, Chrome Options and Desired Capabilities classes are employed. Here’s how to do it:

Step 1: Install the AdBlocker extension in the Chrome browser.

Step 2: Extract the CRX File using a tool like http://crxextractor.com/

Step 3: Provide the path of the downloaded CRX File to the Chrome Options class.

Step 4: Instantiate the web driver object using the Desired Capabilities class and the Chrome Options object.

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);

Using Chrome Options for Incognito Mode:

The pre-defined argument “incognito” allows us to use the incognito mode in Chrome Browser.

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("https://www.h2kinfosys.com/blog/");
driver.manage().window().maximize();

Using Chrome Options for Headless Chrome:

Headless Chrome runs in the background, without a visible GUI. To enable it, use the predefined argument “headless.”

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();

Chrome Options for Adblocker extension

Adblocker extension can be handled using Chrome Options and Desired Capabilities class. 

Steps to handle the adblocker extension are:

Step 1: First, you need to install the AdBlocker extension on the Chrome browser.
Step 2: Extract the CRX File through the link http://crxextractor.com/
Step 3: Pass the path of the downloaded CRX File to the Chrome Options class.
Step 4: Instantiate the object of web driver using the desired capabilities class and chrome options object

The below example shows how to activate the ad blocker extension on the Chrome browser.

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);

Chrome Options for Incognito mode

Using the pre-defined argument incognito we can use incognito mode in Chrome Browser.

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();
   }
}

Chrome Options for Headless Chrome

A Headless browser runs only in the background and cannot be seen on the browser GUI or the operations been operated on it.

To view Chrome Browser in headless mode with the help of predefined arguments headless.

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();
   }
}

Desired Capabilities Class in Selenium WebDriver for Firefox Browser

There are many ways to set desired capabilities in Selenium Web Driver for Firefox:

moz:firefoxOptions: This capability option helps in customizing or manipulating different properties of the Firefox browser. It is called or invoked as a member in any of the two ways, one is with the help of alwaysMatch, and the other is with the help of firstMatch entries.

The below example shows how to use Desired Capabilities in Selenium WebDriver for Firefox:

DesiredCapabilities capabilities = DesiredCapabilities.firefox();
FirefoxOptions options = new FirefoxOptions();
options.setHeadless(headless);
capabilities.merge(options);

Desired Capabilities Option in Selenium WebDriver for IE(Internet Explorer)

Similarly, Desired Capabilities Option can also be used in Internet Explorer as shown above for Chrome and Firefox Browser.

The below example shows the use of Desired Capability Option in IE:

DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability("requireWindowFocus", true);
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);

Some commonly used Capability Options for IE are:

  • ignoreZoomSetting(boolean): This capability helps to ignore checking the browser’s zoom level to 100%, and it takes a Boolean value as input. By default, its value is set to false.
  • initialBrowserUrl(string): This capability is used to decide the initial URL and then the website to which it should redirect when the IE browser starts, and it takes a string as input.
  • enableElementCacheCleanup(boolean): This capability checks for the obsolete elements from element cache, and if found any obsolete element, then the capability instructs the WebDriver to clean up.
  • requireWindowFocus(boolean): This capability instructs the driver to check that if the IE window has the focus on the web elements before performing any operations (like a mouse or keyboard events etc.) By default, its value is false.
Chrome Options & Desired Capabilities

3 Responses

  1. Where i can find all the list of arugument for chrome options
    for example. :
    –start-maximized:
    I want all the arguments which we can pass into AddArgument.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share this article
Subscribe
By pressing the Subscribe button, you confirm that you have read our Privacy Policy.
Need a Free Demo Class?
Join H2K Infosys IT Online Training
Enroll Free demo class