Desired Capabilities in Selenium WebDriver

Desired Capabilities Use in Selenium WebDriver: Currently Days, most companies are before releasing the application to end users. The application is tested under various environments like the Local environment, Release environment, production environment, BAU (Business As Usual ) environment, etc. like these environments, and the application is also tested on various devices like Android devices, iOS devices, Tablets, and Desktop devices. During the testing on different environments and devices, we keep the application’s behavior.

As we discussed earlier, we could test our application in a different environment with the help of the selenium grid in the same way we can set the different devices through the desired capabilities. So, we will see the different desired capabilities available in Selenium WebDriver.

If you have not checked yet about the complete Selenium WebDriver Tutorial Series, which can help both freshers and experienced users, then you can check that by following the article.

What are the Desired Capabilities?

These Desired Capabilities help us to set the properties of the WebDriver browsers. We have the Independent Browser capabilities like browser version, name, and acceptSslCerts, and browser-specific capabilities like chromeOptions for setting the chrome specific desired capabilities and firefox_binary for Mozilla firefox specific capabilities.

Use of Desired Capabilities

  • We can use it to configure the Selenium WebDriver driver instance.
  • We can configure all drivers like FirefoxDriver, ChromeDriver, and InternetExplorerDriver.
  • We can also set the browser properties like browser name, platform, and browser version settings.
  • With the help of Desired Capabilities, we can run several automation scripts in different operating systems and different versions.
  • As per the current standard, we need to test our application on every testing environment, whether a web browser, mobile device, or a mobile emulator or simulator, so we never miss a single bug to the end-user.
  • With The help of Desired capabilities, we can directly interact with the WebDriver, and in that, we can mention which will be used in our scripts.
  • During the cross-browser testing, you can easily set the configuration.

Desired Capabilities Methods

Here are some methods we use in the Desired Capabilities in Selenium WebDriver. The methods look like the Getter and Setter methods. By using this method, we can set the properties, and we can get their values as well. You can use the various methods below:

  • getBrowserName()
  • setBrowserName()
  • getPlatform()
  • setPlatform()
  • getVersion()
  • setVersion()
  • getCapability()
  • setCapability()
  • acceptInsecureCerts()
  • setJavaScriptEnabled()
  • setAcceptInsecureCerts()

To use this class, you first need to create an instance, and with that instance, you can use the methods mentioned above. Hence, you can declare the class instance like the one below.

DesiredCapabilities capability = new DesiredCapabilities();

DesiredCapabilities With Respect To Browser / Platform

To use the DesiredCapabilities in the automation script, you have to import the package

org.openqa.selenium.remote.DesiredCapabilities;

In the above, we have mentioned how you can create an instance of DesiredCapabilities. Still, we can also instantiate the DesiredCapabilities class directly with a specific browser without using the setBrowserName() or setCapability() method. you can instantiate like below:

DesiredCapabilities cap = DesiredCapabilities.chrome();

Capabilities Types

During our automation execution, we pass some of the capability types as an argument to the setCapability() method to perform some actions per our requirement. Below, we have mentioned how you can use those capability types and the different capability type methods:

DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);

List of Capability Types

  • ACCEPT_INSECURE_CERTS
  • BROWSER_VERSION
  • ACCEPT_SSL_CERTS
  • APPLICATION_NAME
  • BROWSER_NAME
  • TAKES_SCREENSHOT
  • PROXY
  • LOGGING_PREFS
  • ELEMENT_SCROLL_BEHAVIOUR
  • ENABLE_PERSISTENT_HOVERING
  • HAS_NATIVE_EVENTS
  • ENABLE_PROFILING_CAPABILITY
  • HAS_TOUCHSCREEN
  • SUPPORTS_APPLICATION_CACHE
  • PLATFORM
  • VERSION
  • PLATFORM_NAME

Here are some commonly used methods, but you can get more from this link.

Example Program:

public class Desired_Capability {

public static void main(String[] args) throws IOException {

// Path of geckodriver
System.setProperty("webdriver.gecko.driver","D:\\geckodriver.exe");

// Initialize WebDriver
WebDriver driver = new FirefoxDriver();

// Go to URL
driver.get("https://www.stqatools.com/");

// Created object of DesiredCapabilities class
DesiredCapabilities capabilities = new DesiredCapabilities();

// Set your device name
capabilities.setCapability("deviceName", "your Device Name");

// Set BROWSER_NAME desired capability.
capabilities.setCapability(CapabilityType.BROWSER_NAME, "Chrome");

// Set your mobile device's OS version.
capabilities.setCapability(CapabilityType.VERSION, "5.1");

// Set android platformName desired capability.
capabilities.setCapability("platformName", "Android");

}

}

I hope with this article, you can get an idea about the desired capabilities and how to use them in your script. If you want to know more about the desired capabilities, then you can check this link.

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment