Desired Capabilities in Selenium WebDriver
โก Smart Summary
Desired Capabilities is a Selenium class that stores browser, platform and device settings as key-value pairs, letting a single test suite target many environments across Selenium Grid, cloud grids and mobile automation.
What is Desired Capabilities?
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.
Version note: Selenium 4 supersedes DesiredCapabilities with browser Options classes โ ChromeOptions, FirefoxOptions, EdgeOptions, InternetExplorerOptions. The examples below keep the original syntax.
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, which is explained in the later part of this page, can be used in Selenium Grid. It is used to perform a parallel execution on different machine configurations.
Ex: Grid (shown below).
It is used to set the browser properties (Ex. Chrome, IE), Platform Name (Ex. Linux, Windows) that are used while executing the Test Case.
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. 3.x,4.x in Android) can be set.
The emulator example below shows the platform set which is android and the platform version set which is IceCream Sandwich (4.x).
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.
Types of Desired Capabilities Methods
Here we will see the different Desired Capabilities methods and how to use one of them, the setCapability() method.
#1) getBrowserName()
public java.lang.String getBrowserName()
#2) setBrowserName()
public void setBrowserName(java.lang.String browserName)
#3) getVersion()
public java.lang.String getVersion()
#4) setVersion()
public void setVersion(java.lang.String version)
#5) getPlatform()
public Platform getPlatform()
#6) setPlatform()
public Platform setPlatform()
#7) getCapability() Method
The getCapability() method of the DesiredCapabilities class can be used to get the capability that is in use currently in the system.
public java.lang.Object getCapability(java.lang.String capabilityName)
#8) setCapability() Method
The setCapability() method of the Desired Capabilities class is used to set the property of a test environment like device name, OS name and version, browser name and version, absolute path of the app under test (the .apk file of the Android app under test), app Activity (in Android) and appPackage (in Java).
โsetCapability methodโ in Java has the below declarations:
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)
Set Capability in Selenium Example
Let us consider an example where we want to run our Test Case on Internet Explorer browser to open www.gmail.com website using Selenium WebDriver.
Following is the code.
import org.openqa.selenium.WebDriver; import org.openqa.selenium.ie.InternetExplorerDriver; public class IEtestforDesiredCapabilities { public static void main(String[] args) { WebDriver driver = new InternetExplorerDriver(); driver.manage().window().maximize(); driver.get("http://gmail.com"); driver.quit(); } }
Now run this code from Eclipse and check out the console.
Output:
It will throw the following error when above code is executed. The error occurs because the path to the browser driver (IE in the above case) is not set. The browser could not be located by the Selenium code.
The path to the driver executable must be set by the webdriver.ie.driver system property; formore information, see http://code.google.com/p/selenium/wiki/InternetExplorerDriver. The latest version can be downloaded from http://code.google.com/p/selenium/downloads/list Dec 11, 201212:59:43PM org.openqa.selenium.ie.InternetExplorerDriverServer initializeLib WARNING: This method of starting the IE driver is deprecated and will be removed in selenium 2.26. Please download the IEDriverServer.exe from http://code.google.com/p/selenium/downloads/list and ensure that it is in your PATH.
Solution:
The solution for the above problem is given in the warning section of the error itself.
- Download the Internet ExplorerDriver standalone server for 32bit or 64bit.
- Save the driver in a suitable location in the system.
- Set the path for the driver using the System.setProperty method. It is used to set the IE driver with the webdriver property. It helps to locate the driver executable file that is stored in the system location. (Ex:โC:\IEDriverLocation\IEDriver.exeโ)
Version note: Selenium 4.6 and later resolve the driver automatically through Selenium Manager, so System.setProperty is optional. The historical code below is unchanged.
import org.openqa.selenium.WebDriver; import org.openqa.selenium.ie.InternetExplorerDriver; import org.openqa.selenium.remote.DesiredCapabilities; public class IEtestforDesiredCapabilities { public static void main(String[] args) { //it is used to define IE capability DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer(); capabilities.setCapability(CapabilityType.BROWSER_NAME, "IE"); capabilities.setCapability(InternetExplorerDriver. INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true); System.setProperty("webdriver.ie.driver", "C:\\IEDriverServer.exe"); //it is used to initialize the IE driver WebDriver driver = new InternetExplorerDriver(capabilities); driver.manage().window().maximize(); driver.get("http://gmail.com"); driver.quit(); } }
Code Explanation:
In the code above,
- The import statements import the required packages for the Selenium WebDriver, the required packages for the Internet Explorer driver, and the packages for the desired capabilities.
- setCapability takes the various capabilities as input variables which are then used by the web driver to launch the application in the desired environment.
- setProperty is used to set the path where the driver is located. WebDriver then locates the required driver.
- Gmail website is opened in the Internet Explorer browser by using โgetโ method.
Output:
The test case on Internet Explorer browser will run successfully using Selenium WebDriver.


