Desired Capabilities in Selenium Webdriver
By Jash Unadkat, Technical Content Writer at BrowserStack - February 18, 2023
It’s a huge challenge for QA engineers to roll out robust web-applications that provide a seamless user experience across a variety of devices. Considering a large number of devices and browsers in the market, QAs need to test web-applications in different environments to ensure they meet market requirements.
These environments include different mobile devices, web-browsers, emulators, or simulators. To use the desired environment, QAs use DesiredCapabilities in test automation frameworks like Selenium. This article aims to explain in detail what DesiredCapabilities is and how it helps QAs test web-apps for cross browser compatibility testing across multiple devices and platforms.
Let’s get started.
What Are DesiredCapabilities in Selenium?
DesiredCapabilities are a set of key-value pairs encoded as a JSON object. It helps QAs define basic test requirements such as operating systems, browser combinations, browser versions, etc. within Selenium test scripts. The DesiredCapabilities Class has multiple methods that let QAs specify the required properties regarding the desired test environments.
One can quickly refer to the GitHub library for Desired Capabilities to understand all the available methods.
Why do QAs need DesiredCapabilities?
Each specific test case needs to be executed in a different environment (browser, mobile, or an operating system) as per the market trends. For example, a QA might need to test a Web-application on two different browsers (Chrome, Safari) installed on mobile devices running on different operating systems (Android, iOS).
This is where Desired Capabilities enables QAs to instruct the Selenium WebDriver regarding the environment to be used while performing the tests. Using the setCapabilities method from the DesiredCapabilities class, QAs can execute parallel tests on desired devices, browsers, and operating systems available on a Cloud Selenium Grid.
Read More: Selenium 4: Understanding Key Features
Methods in DesiredCapabilities for Selenium
Now let’s have a look at all the methods available in the DesiredCapabilities Class.
This method helps in retrieving the capabilities of the current system on which the tests are being performed.
public java.lang.Object getCapability(java.lang.String capabilityName)
The setCapability() method is used to declare the properties of test environments like device name, operating system name, operating system versions, browser, and browser versions.
The setCapability() method has the following declarations in Java:
setCapability :public void setCapability(java.lang.String capabilityName,boolean value) setCapability :public void setCapability(java.lang.String capabilityName,java.lang.String value) setCapability :public void setCapability(java.lang.String capabilityName,Platform value) setCapability :public void setCapability(java.lang.String key,java.lang.Object value).
This method helps in retrieving the browser name of the current system.
public java.lang.String getBrowserName()
This method is used to set the name of the browser on which tests are to be executed.
public void setBrowserName(java.lang.String browserName)
This method helps in retrieving the version of the browser or the operating system of the current system used for running the tests.
public java.lang.String getVersion()
This method helps in defining the version of the browser or the operating system for running the tests.
public void setVersion(java.lang.String version)
This method helps in retrieving the details of the operating system.
public Platform getPlatform()
This method helps in defining the desired operating system to be used.
public Platform setPlatform()
One can quickly refer to the complete documentation on Desired Capabilities in Selenium.
QAs can generate the required Selenium or Appium capabilities using the capability Generator by Browserstack. One just needs to select the desired platform to test on along with the preferred programming language. That’s it.
The image below represents the Capability Generator available on BrowserStack.
It’s vital for teams to test their web applications on a wide range of real devices and browsers. Here’s where teams can leverage platforms like BrowserStack, which provides a Cloud Selenium Grid for automated testing on 2000+ real devices and browsers. It is designed especially to help QAs streamline their test automation processes in order to save time and effort.
Understanding Desired Capabilities in Selenium Webdriver is of utmost importance. This is because QAs need to test their software across a large number of platforms, browsers, and devices – according to market trends. This ensures that their web-app is cross browser compatible and provides a robust user experience across the board.