Desired Capabilities in Selenium Webdriver
Ramit Dhamija
Posted On: November 19, 2019
158253 Views
18 Min Read
Desired Capabilities is a class used to declare a set of basic requirements such as combinations of browsers, operating systems, browser versions, etc. to perform automated cross browser testing of a web application.
When we try to automate our test scripts through Selenium automation testing, we need to consider these combinations to declare a specific test environment over which our website or web application should render seamlessly.
These environments can be mobile devices, mobile browsers, desktop devices, desktop browsers, screen resolutions, etc. To declare these environments in our Selenium automation testing script, we make use of the Desired Capabilities in Selenium testing or Appium testing and this is exactly what we will be discussing in this article.
I will help you become familiar with the Desired Capabilities in Selenium testing and Desired Capabilities for Appium too. I will show you what they are, how they are used for Selenium automation testing and Appium testing with examples. Without further ado, let’s get started.
What are DesiredCapabilities in Selenium?
Desired Capabilities in Selenium are key-value pairs used to customize and configure a testing environment, including browser type, version, and operating system, for Selenium test scripts. Desired Capabilities class is a component of the org.openqa.selenium.remote.DesiredCapabilities
package. It helps Selenium WebDriver set the properties for the browsers. So using different capabilities from Desired Capabilities class we can set the properties of browsers. For example, the name of the browser, the version of the browser, etc. We use these capabilities as key-value pairs to set them for browsers.
You can refer to the github library for the Desired Capabilities for all the detailed methods and etc.
While working with Selenium automation testing, we always have different requirements while execution specifically about which browser to execute, which operating system and version to use for test execution, this where Desired Capabilities helps us a lot on Selenium Grid.
Selenium has a collection of configuration settings called desired capabilities that specify the particular browser and its behavior during automated testing. They provide information such as the name, version, and platform of the browser in addition to other options like proxy, timeouts, logging, mobile emulation, and SSL certificates. Testers can alter and regulate the browser’s properties to satisfy their testing needs by declaring these required capabilities.
To declare Desired Capabilities in Selenium automation testing using Grid, we can use the setCapability method from the DesiredCapabilities class to set the different types of capabilities of the browser (Ex. Chrome, IE, Firefox, Edge) platform name (Ex. Windows, macOS, etc.).
Setup Selenium Grid For Parallel Execution In Different Browsers
Desired Capabilities in Appium is useful in the case of mobile application automation, where we have to execute test automation on different browser properties and device properties, which we can set using Desired Capabilities class. To leverage automation testing for different types of mobile devices, the different kinds of mobile platforms like iOS, Android, etc. Also, there can be different platform (operating system) versions like for Android 9.x, 10.x and for iOS 12.x, 13.x, etc, we can set these properties using Desired Capabilities in Appium for Android or iOS as per our requirement.
This certification is ideal for testing professionals who want to acquire advanced, hands-on knowledge in Selenium automation testing.
Here’s a short glimpse of the Selenium Advanced certification from LambdaTest:
There Is More To Desired Capabilities In Selenium And Appium
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.
Selenium Automation Testing with Disabled JavaScript Settings
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.
Types Of Desired Capabilities Methods
Now, let us discuss the different methods available in Desired Capabilities class to set the different properties in test automation.
getCapability()
This method getCapability() from the class Desired Capabilities, which can be used to get the capabilities of the current system which we are using.
setCapability()
This method setCapability() from the class Desired Capabilities, can be used to set the name of device, name of platform, version of platform, absolute path of the application which is under test, application activity (in Mobile automation), application Package (in Java) and etc.
getBrowserName():
This method getBrowserName() from the class Desired Capabilities, can be used to get the name of the Browser.
setBrowserName()
This method setBrowserName() from the class Desired Capabilities, can be used to set the name of the Browser.
getVersion()
This method getVersion() from the class Desired Capabilities, can be used to get the version of the browser or platform.
setVersion()
This method setVersion() from the class Desired Capabilities, can be used to set the version of the browser or platform.
getPlatform()
This method getPlatform() from the class Desired Capabilities, can be used to get the details of the platform.
setPlatform()
This method setPlatform() from the class Desired Capabilities, can be used to set the details of the platform.
For more information, you can refer to Selenium documentation.
Desired Capabilities In Selenium Testing For Different Browsers
In the above section, we have seen different methods of Desired Capabilities class. Now, let us discuss how we can set different capabilities for different browsers.
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.
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.
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.
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.
Examples Of Desired Capabilities In Selenium Testing With Different Languages
Now, I will demonstrate to you how to leverage Desired Capabilities in Selenium testing with different languages. As you may already know, Selenium automation testing is supported by multiple programming languages. It offers bindings for every major programming language i.e. JavaScript, C#, Python, Java, Ruby, PHP. To give you a gist of how you can leverage the desired capabilities in Selenium testing, I will be giving examples for Java, C#, and Python.
Desired Capabilities in Selenium WebDriver for Java
Desired Capabilities in Selenium WebDriver for C#
Desired Capabilities in Selenium WebDriver for Python
Setting Selenium Capabilities with LambdaTest Capabilities Generator
Lambdatest is a cloud-based cross browser testing tool, which allows you to execute Selenium automation testing on an online Selenium Grid of 2000+ real browsers. All the languages and frameworks supported by Selenium are compatible with LambdaTest.
Being an online Selenium Grid as a Service provider we understand the importance of DesiredCapabilities in your Selenium automation testing experience. We also understand that it can take a considerable amount of time to write down your Desired Capabilities every time to run your tests on hundreds of browsers + OS combinations. This is why we have come up with an online Desired Capabilities Generator which provides you the DesiredCapabilities class for multiple programming languages based upon your point-and-click selection.
So, when we navigate to the capabilities generator from LambdaTest using the above URL you can see a screen as below
As we can see in the above screenshot, we can select the properties which we want to use in our test script and after selecting the required information it generates respective desired capabilities.
The following screen shows the generated capabilities which we can directly use in our test script.
Check Out the Desired Capabilities For Selenium On LambdaTest
Did you notice that we used the Desired Capabilities in Selenium 4? If you look at the above image you will find that the point-and-click selections from drop-downs are made under the tab for Selenium 4.
Selenium 4 Desired Capabilities On LambdaTest Capabilities Generator
That is another great benefit of using LambdaTest as an online Selenium Grid. You get the latest browsers, browser versions, operating systems pre-installed on machines hosted on our cloud servers. That is not all! You also get the latest Selenium 4 desired capabilities all ready to fire up in your automated browser testing scripts.
Example Script Incorporating Desired Capabilities In Selenium Testing
Now, let’s take a quick example to demonstrate how to fit in these desired capabilities in Selenium automation testing scripts. So as we are ready with generating desired capabilities that we want, we have used that in the following test script which we can execute on multiple browsers to perform cross browser testing on 2000+ real browsers using Selenium Grid offered by LambdaTest.
Example automation script:
Output On The LambdaTest Automation Dashboard
After executing the above script on the online Selenium Grid of LambdaTest, we get the below automation logs over the automation dashboard at LambdaTest. You get different types of logs under the automation dashboard i.e. Network logs, Command logs, Metadata, Video recording, Screenshots, etc. In the below screenshot I want you to focus on the raw Selenium Logs offered by LambdaTest.
You can notice that the capabilities declared in the automation scripts are displayed there.
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.
Looking to automate mobile apps on real devices, check out our video below –
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:
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
- 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)
- platformName: We can use this to instruct about the mobile operating system platform like iOS, Android, etc.
- platformVersion: We can use this for mentioning the mobile operating system version on which test expect to execute.
- 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.
- 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.
- 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.
- language: This can be used to set the language of Android or iOS.
- 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
- appActivity: Use this capability to declare a name for an Android activity which you wish to launch from the package. E.g. .MainActivity, .Settings
- 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 - appWaitActity: This capability provides name of android activity we want to wait.
- appWaitPackage: This provides java package for android app which we want to wait for.
- appWaitDuration: This capability used to instruct the wait in milliseconds for the appWaitActivity to launch.
- 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
- calendarFormat: This capability can be used to set the calendar format for the iOS simulator. E.g. Gregorian
- 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.
- 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.
- 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.
- safariInitialUrl: Using this capability we can set the initial URL for safari browser, so it works as welcome page when browser launches.
- 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.
LambdaTest Capabilities Generator Offers Desired Capabilities In Appium
LambdaTest Desired Capabilities Generator now provides the Appium capabilities as well. They can be found under the tab which says Appium. You can test over both Android and iOS browsers to ensure that your web application is working as perfectly as it should.
Configure your automation frameworks tests on the LambdaTest Cloud grid using our real device capabilities generator.
What Have We Learned
As we perform Selenium automation testing we need to declare a set of basic requirements regarding the test environment such as specific browsers, operating systems, and more. This is achieved using the Desired Capabilities class for Selenium testing and Appium testing.
We can also specify the conditions over which we need to perform the test such as locally hosted testing, parallel testing, testing with javascript disabled and more.
This Desired Capabilities class is generated with ease if you are already using LambdaTest through our Desired Capabilities Generator which offers you the class on the basis of your testing requirements from the drop-down menus.
If you are preparing for an interview you can learn more through Selenium interview questions.
If you wish to perform Selenium automation testing for ensuring a wider browser compatibility testing then LambdaTest is your go-to store with all the latest browsers, operating systems, Selenium versions. Happy testing! 🙂
Got Questions? Drop them on LambdaTest Community. Visit now