Desired Capabilities is a class in Selenium used to set properties of browsers to perform cross browser testing of web applications. It stores the capabilities as key-value pairs and these capabilities are used to set browser properties like browser name, browser version, path of browser driver in the system, etc. to determine the behaviour of browser at run time.

  • Desired capability can also be used to configure the driver instance of Selenium WebDriver.
  • We can configure driver instance like FirefoxDriver, ChromeDriver, InternetExplorerDriver by using desired capabilities.

In this article we will go through following things –

  • What is Desired Capability.
  • Why do we need Desired capabilities?
  • Different types of Desired Capabilities Methods
  • Working example.

Why do we need Desired Capabilities?

Desired Capabilities are needed because every Testing scenario should be executed on some specific testing environment. The testing environment can be a web browser, Mobile device, mobile emulator, mobile simulator, etc. The Desired Capabilities Class helps us to tell the webdriver, which environment we are going to use in our test script.

The setCapability method of the DesiredCapabilities Class, can be used in Selenium Grid. It is used to perform a parallel execution on different machine configurations.

It is used to set the browser properties (Ex. Chrome, IE), Platform Name (Ex. Linux, Windows) that are used while executing the test cases. In the case of mobile automation, as we perform the tests on different varieties of mobile devices, the Mobile Platform (ex. iOS, Android) Platform Version (Ex. 8.x, 9.x 10.x and so on in Android) can be set.

Desired Capabilities are more useful in cases like:

  • In mobile application automation, where the browser properties and the device properties can be set.
  • In Selenium grid when we want to run the test cases on a different browser with different operating systems and versions.

Using Desired Capabilities class, we instruct the WebDriver about the environment like browser name, browser version, platform, etc. over which we perform automated browser testing using our automation scripts. But that is not all we can do using the Desired Capabilities class. We can also configure browser-specific preferences such as enabling chrome options, firefox options, testing with incognito mode or even disabling javascript, etc.

If you are using a cloud-based online Selenium grid, then the Desired Capabilities class can also be used to perform parallel testing with Selenium or Appium, testing locally hosted web applications with Selenium, generating command-by-command screenshots, or even a video recording of the entire test automation execution over different browsers + OS configurations.

Methods in DesiredCapabilities for Selenium

Now let us 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()

Desired Capabilities in Selenium WebDriver for Chrome

When we want to customize and configure the Desired Capabilities in Selenium WebDriver for Chrome, we need to invoke the ChromeDriver session to leverage these capabilities. So let us discuss capabilities that are supported by ChromeDriver and how we can use them to set those desired capabilities in Selenium Webdriver for Chrome.

There are two ways to set the capabilities for ChromeDriver.

1. ChromeOptions

2. Desired Capabilities

ChromeOptions is another class that can be used in conjunction with Desired Capabilities in Selenium WebDriver for Chrome to customize or manipulate the various properties of Chrome browser.
Following are the commonly used arguments from ChromeOptions class

Disable-infobars: It is used to prevent chrome browser from displaying notifications like “Chrome is being controlled by automated software”.

Make-default-browser: It is used to make the chrome browser as default browser.

Disable-popup-blocking: It is used to disable the pop-ups which are displayed on chrome browser.

Incognito: It opens chrome browser in incognito mode

start -maximized: It opens chrome browser in maximized mode

Headless: It is used to open the chrome browser in headless mode.

Now, let us discuss an example of an ad blocker where Chrome Option is used in conjunction with Desired Capabilities class. We will use an adblocker extension for this purpose.
Prerequisite for that a crx file of the extension should be downloaded.

//Setting up capabilities to run our test script

ChromeOptions opt = new ChromeOptions();

opt.addExtensions(new File(“path for crx file of the extension”));

DesiredCapabilities capabilities = new DesiredCapabilities();

capabilities.setCapability(ChromeOptions.CAPABILITY, options);

ChromeDriver driver = new ChromeDriver(capabilities);

Desired Capabilities in Selenium WebDriver for Firefox

Like we have seen in the case of the Chrome browser, there are also ways to customize the Desired Capabilities in Selenium WebDriver for Firefox browsers.

moz:firefoxOptions capability is available specifically for Firefox browser. We can use moz:firefoxOptions to customize or manipulate different properties of the Firefox browser, which helps us to control the behavior of the Firefox browser. It is usually invoked as a member in either of the two, one is from alwaysMatch and the other is from firstMatch entries.

moz:firefoxOptions can contain the following things that can control how Firefox browser starts or runs.

a) binary(String): We provide absolute path for the custom Firefox binary to use. Normally, GeckoDriver which is driver that we use for Firefox tries to find the location of Firefox on the current system if it is not defined.

b) args(array of strings): This argument is used to pass the command line arguments to the Firefox binary. While using this syntax should contain, hyphen/dash like [“-profile”].
So to pick up the existing profile by GeckoDriver, we should use syntax like
[“-profile”, “path to the profile”]

c) profile(string): We can provide the path for the directory so that Firefox instance can be used. For installing some firefox extensions or some custom certificates, we can use this. While it is recommended to use prefs while we want to set the custom preferences, instead of directly passing the profile. You can find the example of prefs below along with others.

Here is an example of declaring Desired Capabilities in Selenium WebDriver for Firefox.

{
"capabilities": {
"alwaysMatch": {
"moz:firefoxOptions": {
"binary": "/usr/local/firefox/bin/firefox",
"args": ["-headless", "-profile", "/path/to/my/profile"],
"prefs": {
"dom.ipc.processCount": 9,
"javascript.options.showInConsole": true
},
"log": {"level": "trace"}
}
}
}
}
if(driverParameter == null || driverParameter.equalsIgnoreCase(FIREFOX))
{
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
FirefoxOptions options = new FirefoxOptions();
options.setHeadless(headless);
capabilities.merge(options);
}

Desired Capabilities in Selenium WebDriver for IE(Internet Explorer)

Now, let us proceed with Desired Capabilities in Selenium WebDriver for IE(Internet Explorer). For IE, we make use of InternetExplorerOptions with Desired Capabilities in Selenium WebDriver.

Here is an example of declaring Desired Capabilities in Selenium WebDriver for IE.


if(driverParameter == null || driverParameter.equalsIgnoreCase(IE))
{
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability("requireWindowFocus", true);
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
}

There are few Internet Explorer specific capabilities which we can use, let us discuss some of them one by one.

a) ignoreZoomSetting(boolean): This capability can be used to ignore checking of the browser’s zoom level to 100%. By default, it is set as false. It takes a boolean value as input.

b) initialBrowserUrl(string): Using this capability we can decide initial URL, website to which it should redirect while internet explorer browser starts. It takes a string as input.

c) enableElementCacheCleanup(boolean): This capability provides a check over the obsolete elements from element cache. If they are found then the capability instructs the WebDriver to clean up. It takes a boolean value as input.

d) requireWindowFocus(boolean): This capability is used for instructing the driver to check that the internet explorer window has the focus before performing any operations like a mouse or keyboard events etc. It takes a boolean value as input and by default, it is set as false.

e) ignoreProtectedModeSettings(boolean): If you wish to skip past the protected mode check while performing Selenium automation testing then you can leverage this capability. It takes a boolean value as input.

So these are some of the Internet Explorer specific capabilities which we can use.

Desired Capabilities In Appium For Mobile Automation

Till now we have seen desired capabilities with different languages, browsers, and different methods available for capabilities. And I hope by far, you have had a good idea on how to use Desired Capabilities in Selenium automation testing. However, Selenium automation testing can only ensure your web application for desktop devices but your customers may be using their mobiles for accessing your web application or website, especially your blog page.

We are living in a mobile-first era. The automated browser testing over desktops only cannot ensure you that your website is cross browser compatible. Almost every business is going digital, so it requires a website or a web application to be both desktop and mobile-ready. This means that you may have to perform automated browser testing over mobile browsers too.

How can we perform automated browser testing for mobile browsers then? The good news is that there is another spectacular open-source framework to help you out with that purpose and it is called Appium.

Now, we will discuss the Appium testing, how we can use desired capabilities in Appium.

So, there can be different requirements for platform or operating system (like Android, iOS), platform version, device, etc. which are similar to what we do in case of web application automation. This is where the desired capabilities in Appium come into play for automated browser testing for mobile web-applications.

Example For Desired Capabilities In Appium Testing

When a new automation session is requested, Appium client sends request to server with JSON object which includes Desired Capabilities in the form of key and value pairs.

Using these key and values from JSON object, we provide decide and instruct appium drivers about how we want our mobile automation test to work.

Desired Capabilities can then be set within a server of Appium or we can also add it in our Webdriver test script.

Following is an example for desired capabilities in Appium testing using a JSON object:

{
“platformName”: “Android”,
“platformVersion”: “10.0”,
“deviceName”: “Samsung Galaxy s10”,
“automationName”: “Appium”,
“app”: “path for the app under test”
}

So using above desired capabilities in Appium testing through a JSON object, we are instructing drivers to start our mobile automation session on device Samsung Galaxy s10 with Android version 10.0 using Appium and for the app with the given file location.

There are different desired capabilities Appium supports, out of which some capabilities are common for all the platform or drivers, while some are driver or platform-specific. Let us discuss some of them as we proceed further.

General or Standard Desired Capabilities in Appium Testing

  1. automationName: Using this capability we can instruct driver about which automation engine it should use. By default, it is set as Appium. Other available engines like Appium, Espresso (both are for android), XCUITest (for iOS)
  2. platformName: We can use this to instruct about the mobile operating system platform like iOS, Android, etc.
  3. platformVersion: We can use this for mentioning the mobile operating system version on which test expect to execute.
  4. deviceName: We can use this to specify the particular mobile device over which we wish to perform automated browser testing. Like we have used Samsung Galaxy s10 in the above example.
  5. app: This capability is used to provide the path of our application which needs to be tested with script. Here we provide an absolute local path or remote path with HTTP URL.
  6. browserName: This can be used when we want to execute a test on mobile web browser. We can provide the name of the browser like Safari for iOS, Chrome for Android, etc. It can be left empty when we want to automate app and not using browser.
  7. language: This can be used to set the language of Android or iOS.
  8. newCommandTimeout: This capability helps to instruct driver for the time it has to wait for a new command from client before ending the idle session.

There are few more general capabilities like noReset, fullReset,eventTimings, etc.

Desired Capabilities in Appium For Android

  1. appActivity: Use this capability to declare a name for an Android activity which you wish to launch from the package. E.g. .MainActivity, .Settings
  2. appPackage: Using this capability, we decide the JAva package of the Android app which we want to execute. By default package manifest provides this capability.
    E.g. com.example.android.testApp
  3. appWaitActity: This capability provides name of android activity we want to wait.
  4. appWaitPackage: This provides java package for android app which we want to wait for.
  5. appWaitDuration: This capability used to instruct the wait in milliseconds for the appWaitActivity to launch.
  6. deviceReadyTimeout: This capability instructs about timeout in seconds while driver is waiting for device under test to become ready.

Apart from the above android capabilities, there are few more like adbPort, systemPort, remoteAdbHost, androidInstallPath, etc.

Desired Capabilities in Appium For iOS

  1. calendarFormat: This capability can be used to set the calendar format for the iOS simulator. E.g. Gregorian
  2. launchTimeout: This capability instructs about the time in milliseconds upto which driver has to wait and after that it considers session as failed or hung.
  3. autoAcceptAlerts; This capability is used to accept all the iOS alerts, if they popup while performing automated browser testing. Examples: privacy access permission alerts like location, contacts, etc.
  4. autoDismissAlerts: This capability is used to decline/dismiss all the iOS alerts when they popup while performing automated browser testing. Examples: privacy access permission alerts like location, contacts, etc.
  5. safariInitialUrl: Using this capability we can set the initial URL for safari browser, so it works as welcome page when browser launches.
  6. appName: This capability used to display the name of application which is under test.

Apart from the above iOS capabilities, there are few more like safariAllowPopups, customSSLCert, etc.

So, we have seen desired capabilities that we can use while creating a test with examples for different browsers, with different languages and we also discussed it for Appium with different platforms/operating systems.

Conclusion

The Desired Capabilities class will help to set an environment to define the behavior of the browser/environment on which the test can be executed.

It helps to launch our application in the desired environment having the capabilities that we desire to use.


Tags


You may also like

API Basics and Architecture

API Basics and Architecture
Leave a Reply

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

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}