Selenium Tutorials

Chrome Options & Desired Capabilities

Introduction to Chrome Options Class

Chrome Options class is used to control the properties of Chrome Driver and is used with combining Desired Capabilities.

The below example shows how the Chrome Options class is used to open the Chrome browser in maximized mode.

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

Some common used arguments used in ChromeOptions class are:

  • start-maximized: Used to open Chrome in maximize mode
  • incognito: Used to open Chrome in incognito mode
  • headless: Used to open Chrome in headless mode
  • disable-extensions: Used to disable existing extensions on Chrome browser
  • disable-popup-blocking: Used to disable pop-ups displayed on Chrome browser
  • make-default-browser: Used to make Chrome as default browser
  • version: Used to print chrome browser version
  • disable-infobars: Used to prevent Chrome from displaying the notification.

Desired Capabilities class

Desired capabilities class is used to modify the properties of the web driver. It contains a set of key-value pairs to change individual properties of web driver such as browser name, browser platform, etc. The most commonly used method of Desired Capabilities class is the setCapability method.

The below example shows how Desired Capabilities enables chrome browser to accept SSL certificates on websites.

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

Some commonly used pre-defined capability types are:

  • ACCEPT_SSL_CERTS:  This will tell the browser to accept SSL Certificates.
  • PLATFORM_NAME: This is used to set the Operating System platform.
  • BROWSER_NAME: This is used to set the browser name.
  • VERSION: This is used to set the browser version.

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.
Facebook Comments
Tags

Related Articles

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.

Back to top button
Close
Close