What is Desired Capability?
Why do we need Desired Capabilities?
Different types of Desired Capabilities Methods
Example for set capability method

The setCapability method of the DesiredCapabilities Class, which is explained in the later part of the tutorial, can be used in Selenium Grid. It is used to perform a parallel execution on different machine configurations. Ex: Grid

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. 3.x,4.x in Android) can be set.

The above emulator example 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.

Different types of Desired Capabilities Methods

Here we will see a different type of desired capabilities methods and see how to use one of this method “setCapability Method”.

getBrowserName()

setBrowserName()

getVersion()

setVersion()

getPlatform()

setPlatform()

getCapability Method

The getCapability method of the DesiredCapabilities class can be used to get the capability that is in use currently in the system. “setCapability method” in Java has the below declarations:

Example for set capability method

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. 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”)

Code Explanation: In the code above,

The import statements is to import the required packages for the selenium web driver, required packages for the Internet Explorer driver, 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. Web Driver 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. Conclusion The Desired Capabilities class will help to set an environment to define the behaviour 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.