Introduction
In the ever-evolving world of software testing, Selenium has established itself as the go-to tool for browser automation. Whether you’re an aspiring tester or a seasoned QA professional, understanding Chrome Options & Desired Capabilities is crucial for optimizing your test automation strategy. These concepts help customize browser behavior, boost performance, and handle various testing scenarios effectively.
In this guide, we’ll dive deep into these advanced configurations, their real-world applications, and how mastering them can enhance your Selenium Automation Testing journey. By the end of this post, you’ll have the knowledge to leverage these features for better test execution and robust browser automation.
What Are Chrome Options & Desired Capabilities?
Understanding Chrome Options
Chrome Options is a class in Selenium WebDriver that allows testers to modify the default Chrome browser settings before launching it. These configurations help in handling pop-ups, maximizing performance, running tests in headless mode, and more.
Key Features of Chrome Options
- Headless Execution: Runs tests without opening the browser UI, speeding up execution.
- Disabling Notifications: Prevents pop-ups and alerts from interrupting test execution.
- Adding Extensions: Allows the use of Chrome extensions during testing.
- Setting Window Size: Customizes browser dimensions for responsive testing.
- Handling Insecure Websites: Enables testing on sites with invalid SSL certificates.
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);
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.
Desired Capabilities are a set of key-value pairs used in Selenium to define the behavior of the WebDriver session. They help configure browser properties, proxy settings, and more.
Key Features of Desired Capabilities
- Cross-Browser Testing: Enables running tests on different browsers and versions.
- Mobile Testing: Supports automation on mobile browsers.
- Proxy and Network Settings: Allows configuration for secure and restricted environments.
- Platform-Specific Execution: Runs tests on specific operating systems and environments.
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.
Practical Implementation with Code Examples
Using Chrome Options in Selenium
Let’s look at a simple example of how to use Chrome Options in Selenium WebDriver.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
# Create Chrome Options object
chrome_options = Options()
# Run Chrome in headless mode
chrome_options.add_argument("--headless")
# Disable notifications
chrome_options.add_argument("--disable-notifications")
# Set a custom window size
chrome_options.add_argument("--window-size=1200,800")
# Initialize WebDriver with Chrome Options
driver = webdriver.Chrome(options=chrome_options)
driver.get("https://www.example.com")
print(driver.title)
driver.quit()
Using Desired Capabilities in Selenium
Below is an example demonstrating how Desired Capabilities are used in Selenium WebDriver.
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
# Set Desired Capabilities for Chrome
capabilities = DesiredCapabilities().CHROME
capabilities["acceptInsecureCerts"] = True
# Initialize WebDriver with Desired Capabilities
driver = webdriver.Chrome(desired_capabilities=capabilities)
driver.get("https://self-signed.badssl.com/")
print("Title:", driver.title)
driver.quit()
Real-World Applications of Chrome Options & Desired Capabilities
1. Running Tests in CI/CD Pipelines
When integrating Selenium tests into Jenkins, GitHub Actions, or other CI/CD tools, using headless mode ensures tests run without a UI, improving efficiency.
2. Handling SSL Certificate Errors
Testing secure websites often requires bypassing SSL certificate warnings. Using Desired Capabilities, we can configure WebDriver to accept insecure certificates.
3. Enhancing Performance for Large-Scale Testing
By disabling images, pop-ups, and unwanted extensions, Chrome Options help in executing tests faster in large-scale test suites.
4. Multi-Platform and Cross-Browser Testing
Desired Capabilities allow running tests across different platforms like Windows, macOS, and Linux, making automation truly scalable.
Industry Insights and Statistics
- 90% of companies using Selenium for test automation leverage browser options for better efficiency.
- According to a recent study, using headless execution can reduce test execution time by up to 40%.
- Large-scale projects with thousands of test cases rely on customized browser settings to avoid unnecessary failures.
Best Practices for Optimizing Selenium Tests with Chrome Options & Desired Capabilities
- Use Headless Mode Where Possible – It speeds up execution, especially in CI/CD pipelines.
- Disable Unnecessary Features – Turn off notifications, images, and extensions to streamline testing.
- Specify Browser and OS Compatibility – Ensure compatibility for smooth execution across environments.
- Log and Debug Efficiently – Use Chrome logs and debugging tools to identify issues.
- Combine Chrome Options & Desired Capabilities – For maximum flexibility and control over test execution.
Conclusion
Understanding and using Chrome Options & Desired Capabilities is essential for any selenium automation testing. These configurations help fine-tune browser behavior, improve execution speed, and make tests more robust. Whether you’re testing web applications, working with CI/CD pipelines, or handling complex browser configurations, mastering these concepts will significantly boost your automation skills.
Are you ready to take your Selenium automation testing skills to the next level? Enroll in H2K Infosys’ Selenium course today and get hands-on experience with industry-relevant tools and techniques!
3 Responses
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.