Course
Selenium Tutorial for Beginners: Learn WebDriver in 7 Days
Training Summary Selenium is a popular open-source web-based automation tool. This online course is...
Chrome options class is used to manipulate various properties of Chrome driver. Chrome options class is generally used in conjunction with Desired Capabilities.
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
In this tutorial, you will learn
Desired capabilities class is used to modify multiple properties of web driver. Desired Capabilities class provides a set of key-value pairs to change individual properties of web driver such as browser name, browser platform, etc. Most commonly used method of Desired Capabilities class is setCapability method. Desired Capabilities is most frequently 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 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 |
Adblocker extension of the Chrome browser can be handled using Chrome 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 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);
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
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
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
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
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.gtupapers.com/test/simple_context_menu.html");
driver.manage().window().maximize();
//driver.quit();
}
}
Code Explanation:
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
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.gtupapers.com/test/simple_context_menu.html");
driver.manage().window().maximize();
//driver.quit();
}
}
Code Explanation:
Output:
The chrome browser window will be opened in Incognito mode as below
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.gtupapers.com/");
driver.manage().window().maximize();
String title = driver.getTitle();
System.out.println("Page Title: " +title);
driver.quit();
}
}
Code Explanation:
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.
Training Summary Selenium is a popular open-source web-based automation tool. This online course is...
Breakpoints are used to check the execution of your code. Whenever you implement a breakpoint in...
In this Selenium vs UFT tutorial, we are going to compare very popular automation tools - QTP vs...
In this tutorial, we will learn how to handle DropDown in Selenium and Multiple Select Operations....
There are two types of HTML tables published on the web- Static tables : Data is static i.e....
In this tutorial, we will learn How to deal with file uploads and downloads. Uploading Files For...