What Is Desired Capabilities in Selenium??

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 Names Syntax Description
getBrowserName public java.lang.String getBrowserName()Gets the current Browser Name
setBrowserName public void setBrowserName(java.lang.String browserName)Sets the current Browser Name(java.lang.String browserName)
getVersion public java.lang.string getVersion()Gets the browser version
setVersion public void setVersion(java.lang.String browserVersion)Sets the version to the current browser
getPlatform public Platform getPlatform()Gets the current platform information
setPlatform public Platform setPlatform()Sets the platform
getCapability public java.lang.object getCapability(java.lang.String CapabilityName)Use to get the current capability of the system
setCapability public void setCapability(java.lang.String capabilityName ,boolean value)Use to set the current capability of the system
setCapability public void setCapability(java.lang.String capabilityName ,java.lang.String.value)sets the current capability with a given string
setCapability public void setCapability(java.lang.String capabilityName ,PlatformValue)sets the current capability with a given Platform info
setCapability public void setCapability(java.lang.String capabilityName ,java.lang.Object.value)sets the current capability with a given object

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 Names Syntax Description
acceptSslCerts CapabilityType.Accept_SSL_CERTSThis is used to enable or disable browser session to accept or reject the SSL certificates
applicationCacheEnabled CapabilityType.SUPPORTS_APPLICATION_CACHEEnables browsers to use cache
CssSelectorEnabled CapabilityType.SUPPORTS_FINDING_BY_CSSChecks if the CSS selector is enabled for finding web element
javaScriptEnabled CapabilityType.SUPPORTS_JAVASCRIPTChecks if the javascript execution is enabled for the browser
TakesScreenshot CapabilityType.TAKES_SCREENSHOTChecks if screenshot taking ability is enabled or not
WebStorage Enabled CapabilityType.SUPPORTS_WEB_STORAGEChecks if the browser instance can interact with storage object
HandleAlerts CapabilityType.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);

What Is Desired Capabilities in Selenium?? What Is Desired Capabilities in Selenium?? Reviewed by Animesh Chatterjee on September 10, 2013 Rating: 5

No comments:

Powered by Blogger.