What Is Desired Capabilities in Selenium?

Share and Enjoy !

Desired Capabilities got introduced in Selenium to work with lot of browser capabilities. Mostly like-profile data,handling browser cookies , SSL security popup file upload/save etc. In an easy line-It is used to specify the desired capability mostly related to session before the session gets created. In a layman’s language, it is few set of properties/flags/options that needs to be set before we start our test run.

While designing the test it is specified in Webdriver so the client receive the desired capabilities where the run needs to be initiated. If the server sends no data then no capabilities have been supported.

More on webdriver capabilities can be found here. Google’s doc is also very helpful in this respect. More can be found here as well.

The most interesting aspect of desired capabilities is , it gives birth the concept of exception handling.The food for thought can be found here.

If we go little details , this class helps us to communicate with webdriver like which environment we will be running our test scripts. Basically helps us to configure webDriver.It is not only applicable for browser, it is also applicable for mobile testing,where we can set the device properties along with browser version.If we want to run test cases on a different browser on different O.S.

Below are the different methods supported by Desired Capability Class:

Method NamesSyntaxDescription
getBrowserNamepublic java.lang.String getBrowserName()Gets the current Browser Name
setBrowserNamepublic void setBrowserName(java.lang.String browserName)Sets the current Browser Name(java.lang.String browserName)
getVersionpublic java.lang.string getVersion()Gets the browser version
setVersionpublic void setVersion(java.lang.String browserVersion)Sets the version to the current browser
getPlatformpublic Platform getPlatform()Gets the current platform information
setPlatformpublic Platform setPlatform()Sets the platform
getCapabilitypublic java.lang.object getCapability(java.lang.String CapabilityName)Use to get the current capability of the system
setCapabilitypublic void setCapability(java.lang.String capabilityName ,boolean value)Use to set the current capability of the system
setCapabilitypublic void setCapability(java.lang.String capabilityName ,java.lang.String.value)sets the current capability with a given string
setCapabilitypublic void setCapability(java.lang.String capabilityName ,PlatformValue)sets the current capability with a given Platform info
setCapabilitypublic void setCapability(java.lang.String capabilityName ,java.lang.Object.value)sets the current capability with a given object
READ  Alternative Approach to Solve Thread.Sleep in Java or Selenium or in OpenScript

Sample Implementations:

import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

public class DesiredCapability {

public static void main(String[] args) {

DesiredCapabilities cap=DesiredCapabilities.internetExplorer();
cap.setCapability(CapabilityType.BROWSER_NAME, "IE");
WebDriver driver=new InternetExplorerDriver(cap);


Few more capabilities:

Method NamesSyntaxDescription
acceptSslCertsCapabilityType.Accept_SSL_CERTSThis is used to enable or disable browser session to accept or reject the SSL certificates
applicationCacheEnabledCapabilityType.SUPPORTS_APPLICATION_CACHEEnables browsers to use cache
CssSelectorEnabledCapabilityType.SUPPORTS_FINDING_BY_CSSChecks if the CSS selector is enabled for finding web element
javaScriptEnabledCapabilityType.SUPPORTS_JAVASCRIPTChecks if the javascript execution is enabled for the browser
TakesScreenshotCapabilityType.TAKES_SCREENSHOTChecks if screenshot taking ability is enabled or not
WebStorage EnabledCapabilityType.SUPPORTS_WEB_STORAGEChecks if the browser instance can interact with storage object
HandleAlertsCapabilityType.SUPPORTS_ALERTSChecks if the browser instance can handle window popup.

One good example of Desired capability is to disable the save password popup in Chrome. How that is done?

ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("credentials_enable_service", false);
prefs.put("profile.password_manager_enabled", false);
options.setExperimentalOption("prefs", prefs);
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
ChromeDriver driver = new ChromeDriver(capabilities);

Share and Enjoy !

Leave a Reply

Your email address will not be published. Required fields are marked *