What is the use of DesiredCapabilities in Selenium WebDriver?
When we want to use this and how?
Answer with example would be appreciated.
I know I am very late to answer this question.
But would like to add for further references to the give answers.
DesiredCapabilities
are used like setting your config with key-value pair.
Below is an example related to Appium
used for Automating Mobile platforms like Android
and IOS
.
So we generally set DesiredCapabilities
for conveying our WebDriver
for specific things we will be needing to run our test to narrow down the performance and to increase the accuracy.
So we set our DesiredCapabilities as:
// Created object of DesiredCapabilities class.
DesiredCapabilities capabilities = new DesiredCapabilities();
// Set android deviceName desired capability. Set your device name.
capabilities.setCapability("deviceName", "your Device Name");
// Set BROWSER_NAME desired capability.
capabilities.setCapability(CapabilityType.BROWSER_NAME, "Chrome");
// Set android VERSION desired capability. Set your mobile device's OS version.
capabilities.setCapability(CapabilityType.VERSION, "5.1");
// Set android platformName desired capability. It's Android in our case here.
capabilities.setCapability("platformName", "Android");
// Set android appPackage desired capability.
//You need to check for your appPackage Name for your app, you can use this app for that APK INFO
// Set your application's appPackage if you are using any other app.
capabilities.setCapability("appPackage", "com.android.appPackageName");
// Set android appActivity desired capability. You can use the same app for finding appActivity of your app
capabilities.setCapability("appActivity", "com.android.calculator2.Calculator");
This DesiredCapabilities
are very specific to Appium
on Android
Platform.
For more you can refer to the official site of Selenium desiredCapabilities class
When you run selenium WebDriver, the WebDriver opens a remote server in your computer's local host. Now, this server, called the Selenium Server, is used to interpret your code into actions to run or "drive" the instance of a real browser known as either chromebrowser, ie broser, ff browser, etc.
So, the Selenium Server can interact with different browser properties and hence it has many "capabilities".
Now what capabilities do you desire? Consider a scenario where you are validating if files have been downloaded properly in your app but, however, you do not have a desktop automation tool. In the case where you click the download link and a desktop pop up shows up to ask where to save and/or if you want to download. Your next route to bypass that would be to suppress that pop up. How? Desired Capabilities.
There are other such examples. In summary, Selenium Server can do a lot, use Desired Capabilities to tailor it to your need.
Desired capabilities comes in handy while doing remote or parallel execution using selenium grid. We will be parametrizing the browser details and passing in to selenium server using desired capabilities class.
Another usage is, test automation using Appium as shown below
// Created object of DesiredCapabilities class.
DesiredCapabilities capabilities = new DesiredCapabilities();
// Set android deviceName desired capability. Set your device name.
capabilities.setCapability("deviceName", "your Device Name");
// Set BROWSER_NAME desired capability.
capabilities.setCapability(CapabilityType.BROWSER_NAME, "Chrome");
// Set android VERSION desired capability. Set your mobile device's OS version.
capabilities.setCapability(CapabilityType.VERSION, "5.1");
// Set android platformName desired capability. It's Android in our case here.
capabilities.setCapability("platformName", "Android");
DesiredCapabilities
are options that you can use to customize and configure a browser session.
You can read more about them here!
org.openqa.selenium.remote.DesiredCapabilities
package.Example:
WebDriver driver;
String baseUrl , nodeUrl;
baseUrl = "https://www.facebook.com";
nodeUrl = "http://192.168.10.21:5568/wd/hub";
DesiredCapabilities capability = DesiredCapabilities.firefox();
capability.setBrowserName("firefox");
capability.setPlatform(Platform.WIN8_1);
driver = new RemoteWebDriver(new URL(nodeUrl),capability);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(2, TimeUnit.MINUTES);
You should read the documentation about DesiredCapabilities. There is also a different page for the ChromeDriver. Javadoc from Capabilities
:
Capabilities: Describes a series of key/value pairs that encapsulate aspects of a browser.
Basically, the DesiredCapabilities
help to set properties for the WebDriver. A typical usecase would be to set the path for the FirefoxDriver
if your local installation doesn't correspond to the default settings.