Desired Capabilities in Selenium Webdriver



Ware are Desired Capabilities?

The Desired Capabilities in Selenium help set the browser properties for automating web application testing. The capabilities are stored as key-value pairs encoded in a JSON object. The browser properties include the browser name, settings, version, and platform to customize the test environment.

In Selenium 4 and the latest versions, the desired capabilities class is deprecated. It is replaced with the Options class. You can use the Options class alone or combine the Desired Capabilities with Options.

FirefoxOptions options = new FirefoxOptions();
//Accept all the certificate
options.setAcceptInsecureCerts(true);
FirefoxDriver driver = new FirefoxDriver(options);
//Initiate the URL
driver.get(“https://www.google.com”);

Why do QA’s need Desired Capabilities?

Quality Assurance professionals use Desired Capabilities in  Selenium to ensure that their automated tests are running properly. Desired Capabilities allow QA teams to customize the behavior of their automated tests, specify the browser type, set timeouts, and more.

This is especially important for web applications, as the browser and environment used can have a significant impact on the results of the test. By setting up desired capabilities, QA engineers can make sure that their tests are running in the exact environment they need them to be.

This helps them save time and resources while ensuring that their automated tests are running as expected. Furthermore, they can identify any potential issues or bugs in the application quickly and accurately.

Methods in Desired Capabilities for Selenium

The Desired Capabilities Class has various methods that allow QA to specify the required properties regarding the desired test environments. Now let’s look at all the methods available in the Desired Capabilities Class.

  • getCapability();

The getCapability() method helps retrieve the current system’s capabilities on which the tests are being executed.

public java.lang.Object getCapability(java.lang.String capabilityName)

  • setCapability();

The setCapability() method helps declare the test environments’ properties like device name, name of the operating system, operating system versions, browser name, and browser versions.

public java.lang.Object setCapability(java.lang.String capabilityName)

  • getBrowserName();

The getBrowserName() method helps retrieve the name of the browser of the current system.

public java.lang.String getBrowserName()

  • setBrowserName();

The setBrowserName() method helps set the browser name over which the tests will be performed.

public void setBrowserName(java.lang.String browserName)

  • getVersion();

The getVersion() method helps retrieve the browser or operating system version of the current system used for executing the tests.

public java.lang.String getVersion()

  • setVersion();

The setVersion() method helps define the browser version or the operating system for executing the tests.

public void setVersion(java.lang.String version)

  • getPlatform();

The getPlatform() method helps retrieve the platform details.

public Platform getPlatform()

  • setPlatform();

The setPlatform() method helps define the details of the platform.

public Platform setPlatform()

WebDriver driver;
String baseUrl , nodeUrl;
baseUrl = “https://www.facebook.com”;
nodeUrl = “http://192.168.10.21:5568/wd/hub”;

DesiredCapabilities capability = DesiredCapabilities.Chrome();
capability.setBrowserName(“chrome”);
capability.setPlatform(Platform.WIN8_1);

driver = new RemoteWebDriver(new URL(nodeUrl),capability);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(2, TimeUnit.MINUTES);
Desired Capabilities in Selenium WebDriver for the Chrome Browser

ChromeDriver is a standalone server used by the Selenium WebDriver to control the Chrome browser. Now we will discuss the desired capabilities the ChromeDriver supports and how to use them for the Chrome browser.

You can set the desired capabilities for ChromeDriver using these two methods,

  • ChromeOptions
  • Desired Capabilities
What is ChromeOptions?

ChromeOptions is a class that you can use in conjunction with the desired capabilities class in order to customize the different properties of the Chrome browser.

These are some of the common arguments from ChromeOptions class,

  • Disable-popup-blocking: It helps disable all the popups that appear on the Chrome browser.
  • Disable-infobars: It helps prevent Chrome from showing notifications such as “Chrome is being controlled by automated test software.
  • Make-default-browser: It helps set Chrome as the default browser.
  • Headless: It helps open the browser in headless mode.
  • Incognito: It opens Chrome in incognito mode
  • start-maximized: It helps opens the browser in maximized mode

Chrome Options for Adblocker

You can handle the Chrome extension using Desired Capabilities class and ChromeDriver Options. Follow the below steps to do so,

  1. You must install the AdBlocker extension on Chrome before using the Chrome Options class.
  1. Then extract the CRX File of the AdBlocker extension using any file extractor.
  1. Now, pass the file path of the downloaded CRX to the Chrome Options class.
  1. Instantiate the WebDriver using the capabilities and Chrome Option.

Code Snippet

The below code depicts the above steps for activating Adblocker
//Setting up capabilities to run our test script
ChromeOptions options = new ChromeOptions();
//Add Adblocker extension to Chrome
options.addExtensions(new File(“C:\Users\Downloads\adblocker.crx”));
DesiredCapabilities capabilities = new DesiredCapabilities();
//Using Chrome Options with Desired Capabilities Class
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
ChromeDriver driver = new ChromeDriver(capabilities);
Tags: , , ,
Leave a comment

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

Subscribe now

Receive weekly newsletter with educational materials, new courses, most popular posts, popular books and much more!

https://bridgejunks.com/ https://crownmakesense.com/ https://brithaniabookjudges.com/ https://hughesroyality.com/ https://rhythmholic.com/ https://bandar89.simnasfikpunhas.com/ https://www.100calshop.co.il/products/thailand/ https://myasociados.com/ https://solyser.com/ http://konfidence.cz/ https://muscadinepdx.com/ https://bandar89.parajesandinos.com.ve/ https://goremekoop.com/ https://oncoswisscenter.com/ https://www.turunclifehotel.com/bandar89/ https://www.houseofproducts.biz/ https://taimoormphotography.com/
BIJI18 BIJI18 BIJI18