Highlight an element in selenium , Selenium interview questions

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.

Methods in DesiredCapabilities for Selenium

Now let’s have a look at all the methods available in the DesiredCapabilities Class.

1. getCapability();
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)

2. setCapability();
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).

3. getBrowserName();
This method helps in retrieving the browser name of the current system.

public java.lang.String getBrowserName()

4. setBrowserName();
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)

5. getVersion();
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()

6. setVersion();
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)

7. getPlatform();
This method helps in retrieving the details of the operating system.

public Platform getPlatform()

8. setPlatform();
This method helps in defining the desired operating system to be used.

public Platform setPlatform()