January 24, 2020

Desired Capabilities in Test Automation with Selenium

Overview of Desired Capabilities

By the term “Desired Capabilities” in automation testing, what comes first in our mind? If the first thing that strikes our mind is might be DesiredCapabilities is a predefined class that can help us set up our test environment according to our test requirement, then our thoughts are actually on target. Moving forward with the test environment, it gives us many capabilities such as browserName, version, platform etc. These capabilities tell the WebDriver on which configuration to run the test on.

As mentioned, it is a predefined class in “org.openqa.selenium.remote.DesiredCapabilities” package.

Desired Capabilities is used when we are running our test on Selenium Grid whether it is a cloud-based Grid or internally structured Grid.

Selenium Grid is very useful when we want to run our test on different configurations(browserName, version, platform) parallely.

These capabilities are not only used with Selenium but with Appium as well. Software Testing Services can even set mobile application testing configuration with the help of Desired Capabilities.

We store these capabilities as a key/pair values to set the test properties.

Below are few methods provided by DesiredCapabilities class:

  • getBrowserName() : public java.lang.String getBrowserName()

This method can be used to get the name of the browser.

  • setBrowserName() : public void setBrowserName(java.lang.String browserName)

This method can be used to set the name of the browser.

  • getVersion() : public java.lang.String getVersion()

This method can be used to get the browser version

  • setVersion() : public void setVersion(java.lang.String version)

This method can be used to set browser version

  • getPlatform() : public Platform getPlatform()

This method can be used to get platform/operating system name

  • setPlatform() : public Platform setPlatform()

This method can be used to set platform/operating system name

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

This method is used to get capabilities which are currently used by the system to run automation test.

  • setCapability() : This method is used to set browserName, version, platform/os, browser logging preferences, apk etc.

To make this clearer, let's have a look at the below example:

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setBrowserName("chrome");
 capabilities.setVersion("79");
 capabilities.setPlatform(Platform.WINDOWS);
WebDriver driver = new RemoteWebDriver (new URL("http://localhost:4444/wd/hub"), capabilities);

The above example would run our test on Windows platform with browser Chrome version 79. Similarly, if you have setup the Similarly Grid, you can run your test on different configurations that too in a parallel manner.

Desired Capabilities for Chrome

Desired Capabilities not only help in setting up the browserName, version and platform but also with configuring the browser preferences. These preferences can be:

  • headless - Opens browser in headless mode
  • start-maximized - maximize the browser
  • incognito - opens browser in incognito mode
  • disable-popup-blocking - enables the popup to appear
  • make-default-browsers - makes chrome as default browser
  • disable-gpu - disable graphic processing unit
  • window-size=1200x600 - set the desired window size
  • verbose - to get chrome test logs
  • ignore-certificate-errors - ignores web app certificate errors

Example:

ChromeOptions options = new ChromeOptions();

options.addArguments("incognito");

options.addArguments("start-maximized");

DesiredCapabilities capabilities = DesiredCapabilities.chrome();

capabilities.setCapability(ChromeOptions.CAPABILITY, options);

options.merge(capabilities);

WebDriver driver = new ChromeDriver(options);

Code Walkthrough:

In the above sample code we have “options” as an instance of ChromeOptions class. Using this object, we are adding the arguments to open the chrome browser in incognito and maximized mode. Once we have added the arguments, we need to merge our desired capabilities with chrome options. We can pass the ChromeOptions instance to ChromeDriver constructor.

Desired Capabilities for Firefox

Similar to ChromeOptions, we have FirefoxOptions that can help setting up the properties for Firefox browser while running our automation test. To modify the preferences of Firefox browser in our test automation, we have FirefoxProfile. This can help us setting up our own profile with specific bookmarks, plugins etc if required. Initially, when you run our automation test, the browser that gets launched doesn’t have any bookmarks or plugins attached to it. To resolve this Firefox provides an option to create a Firefox profile. Open up a command prompt and type: “firefox.exe –p” to create desired Firefox profile. Your automation test would now run on the recently selected Firefox Profile.

Coming back to the FirefoxOptions, we can use different properties in this, such as headless, browser size, setBinary.

Example:

DesiredCapabilities capabilities = DesiredCapabilities.firefox();

FirefoxOptions options = new FirefoxOptions();

options.setHeadless(true);

capabilities.merge(options);

WebDriver driver = new FirefoxDriver(capabilities);

Code Walkthrough:

In the above code sample, we are running our test on Firefox browser in a headless mode. Just to brief, headless browser is a browser that doesn’t have any graphical user interface. It increases the test performance by reducing the execution time.

Desired Capabilities for Internet Explorer

Example:

DesiredCapabilities caps = DesiredCapabilities.internetExplorer();

caps.setCapability(CapabilityType.BROWSER_NAME, "IE");

caps.setCapability(InternetExplorerDriver.

INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);

WebDriver driver = new InternetExplorerDriver(caps);

Code Walkthrough

In the above code, we have used the capability “INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS” which states not to ignore the browser protected mode while launching the automated browser. For more IE desired capabilities, you can have a look at Selenium official doc page.

Below are few capabilities that are widely used with IE:

  • ignoreZoomSetting: Sets the browser zoom level to 100%. The browser zoom level must be set to 100% so that the native mouse events can be set to the correct coordinates.
  • enablePersistentHover: Determine whether to enable persistently sending of mouse move messages to the IE window during mouse hover.
  • requireWindowFocus: Allows the driver focus on IE window before executing any mouse or keyboard event. Initially the value of this capability is false but still performs many native events.
  • ignoreProtectedModeSettings: Determine whether to skip the protected mode check or not which helps from malicious software, code, plugins from being installed.

Summarizing it all!!

Desired Capabilities are perfect to be used with Selenium Grid where you can run your test on different configurations. Though the use of Desired Capabilities is very vast, the Selenium 4 has provided an option where you can even avoid the use of Desired Capability and maintain your workflow only with ChromeOptions/FirefoxOptions in some cases. However, it is not actually 100% deprecated; you may face many scenarios while using Selenium 4 where you might need to use Desired Capabilities. If your test scripts are already setup according to Selenium 3 then you would find Desired Capabilities an amazing class to be used with browser options. So, guys give it a try to this interesting topic. Good Luck and Happy Testing :)

You may like to read more on JAVA-Gauge: Open Source Test Automation Framework