DesiredCapabilities in Selenium with Example

In the Previous post, we have seen how to create Firefox Profile in Selenium. In this post, we will learn DesiredCapabilities in Selenium with Example.

Not all server implementations will support every WebDriver feature. DesiredCapabilities help to set the properties for WebDriver. A typical example can be to set the path of FirefoxDriver if local installation doesn’t point to the default settings. capabilities are nothing but a series of key/value pairs that express the essential features of a browser. If not able to understand much, don’t worry. It will be clear more with the below examples.

1. Using the preferred browser: The usual way to start a FirefoxDriver is as given below.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

WebDriver driver = new FirefoxDriver();

Now, let’s see how use desiredcapabilities to set the browser preferences. Use the below code for setting browser preference.

import org.openqa.selenium.remote.DesiredCapabilities; 

DesiredCapabilities dc=DesiredCapabilities.firefox();
Webdriver driver =  new FirefoxDriver(dc);

2. Changing the Proxy of the system: The preferred way to set the proxy manually at the system. But because of some reason , if you are not able to do so. Then you can use desiredcapabilities to set the proxy at your system to run the tests. But remember , this proxy changes in only for run time. Once the execution is over ,System will come to its original state.

Use the below code for setting proxy.

import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

public class ChangeProxyExample {

	public static void main(String[] args) {
		String PROXY = "localhost:8080";

		Proxy proxy = new Proxy();
		DesiredCapabilities cap = new DesiredCapabilities();
		cap.setCapability(CapabilityType.PROXY, proxy);
		WebDriver driver = new FirefoxDriver(cap);
                //can set for other browser like internet explorer
               //WebDriver driver = new InternetExplorerDriver(cap);


3. FirefoxProfile at run time with desired capabilities: In the previous post, we have seen how to use custom Firfox profile created by Firefox Profile Manager. We can also create desired firefox profile at run time and set the preferences. 

We can create this Firefox profile for activities like to ignore untrusted certificates errors, downloading file without popping up the download box( We will learn in details about file downloading in coming posts) etc.

Create Firefox Profile:

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("", 2);
profile.setPreference("browser.helperApps.alwaysAsk.force", false);

Example to use Firefox profile with desired capabilities:

DesiredCapabilities caps=DesiredCapabilities.firefox();
FirefoxProfile profile = new FirefoxProfile();
caps.setCapability(FirefoxDriver.PROFILE, profile);
Webdriver driver =  new FirefoxDriver(caps);

4. Can be helpful in selenium Grid: Selenium Grid is helpful to provide parallel testing of the test cases. It can have multiple browser instances , different browser types and platforms. (You can learn about selenium Grid in next upcoming posts.)

DesiredCapability can help to select the browser and platform preferences as given below in the table.

Key Type Description
browserName string We can set the preferred browser name like (android|chrome|firefox|htmlunit|internet explorer|iPhone|iPad|opera|safari).
version string The browser version, or the empty string if unknown.
platform string It is used to specify the particular operating system or platform where test cases will be executing.Its values could be (WINDOWS|XP|VISTA|MAC|LINUX|UNIX|ANDROID). If platform is unknown , keyword ANY could be specified.

Example of using DesiredCapabilities in selenium Grid:


import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public class DesiredCapabilityExample {

	public static void main(String[] args) throws MalformedURLException {
		DesiredCapabilities caps = new DesiredCapabilities().firefox();
		WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), caps);

capability.setPlatform(Platform.MAC);   //Set platform to OSX
capability.setPlatform(Platform.LINUX); // Set platform to Linux based systems
capability.setPlatform(Platform.WINDOWS); //Set platform to Windows
capability.setPlatform(Platform.VISTA); //Set platform to VISTA
capability.setPlatform(Platform.WIN8); //Set platform to Windows
capability.setPlatform(Platform.WIN8_1); //Set platform to Windows 8.1

If you want to read more about Desired Capabilities, You can go to this Desired Capabilities Documentation.

Ask Question
Have any question or suggestion for us?Please feel free to post in Q&A Forum


Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...