In real-time automation using selenium webdriver, it is often required to run our tests on a various combinations of ( OS , Browser and Versions ). So, this is required for the compatibility of the application in different environments. In this tutorial, we see how we can achieve this using the DesiredCapabilities class.
It is a class in WebDriver API. First of all, it provides support for setting the capability to run tests.DesiredCapabilities are a series of key-value pairs (KV ) that allow customization of testing. Also, note that if a session cannot support a capability that is requested in the desired capabilities, no error is thrown. Instead, a read-only capabilities object is returned that indicates the capabilities the session actually supports.
In the below example I would take BrowserStack as an example and for running a sample test customized to run on Mozilla Firefox with version 45 and on Windows 10 OS .
For example some are listed below:
OS you want to test.
OS version you want to test.
Browser you want to test.
Browser version you want to test.
To get the above capability configuration we can use the below sample code snippet:
caps.setCapability("browser", "Firefox"); caps.setCapability("browser_version", "45.0"); caps.setCapability("os", "Windows"); caps.setCapability("os_version", "10"); caps.setCapability("resolution", "1024x768");
Choosing mobile browsers for running tests :
If we want to run your Selenium test scripts on iOS by specifying the version and device in the input capabilities. These capabilities are browserName and device.
The following example code snippet for running on iPhone device:
DesiredCapabilities caps = new DesiredCapabilities(); caps.setCapability("browserName", "iPhone"); caps.setPlatform(Platform.MAC); caps.setCapability("device", "iPhone 5");
Example code snippet to open Twitter on Windows 10 operating system on Mozilla Firefox with version 45 on BrowserStack using desired capabilities is shown below:
DesiredCapabilities caps = new DesiredCapabilities(); caps.setCapability("browser", "Firefox"); caps.setCapability("browser_version", "45"); caps.setCapability("os", "Windows"); caps.setCapability("os_version", "10"); caps.setCapability("browserstack.debug", "true"); WebDriver driver = new RemoteWebDriver(new URL(URL), caps); driver.get("https://www.twitter.com"); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
Using desired capabilities with cloud, we get the freedom to run our selenium webdriver test on countless number of device combinations. Also, check out the Webdriver API desired capabilities class which has a lot of constructors and explore news of instantiating desired capabilities class with ease.
Selenium WebDriver Tutorials on this website can be found at:
For more details on the Selenium, visit the official website at: