What Is Desired Capabilities in Selenium?


Introduction to Desired Capabilities in Selenium

Desired Capabilities got introduced in Selenium to work with a 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 the 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 receives 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 to 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 in. 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 the browser version. If we want to run test cases on a different browser on different O.S.

READ  How to resolve Cannot Find Firefox Binary in PATH Error In Selenium - 2 Important Points

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

Handling Security Issue using the Desired Capability

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.

Handle Save password Popup using Desired Capability

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);

Handling SSL Related Error Using Desired Capability

In case you are facing SSL related Error during your test automation, you need to resolve them using selenium and WebDriver using Desired Capabilities. Desired Capabilities are used to configure WebDriver instance via selenium script. The same can be used to configure another browser (driver) instances like ChromeDriver, InternetExplorerDriver etc to handle SSL certificate errors.

Handle SSL Certificate Error in Firefox

In Firefox, you need to perform a two-step process to handle the SSL certificate in Firefox. They are as follows:

  • Creating a profile in Firefox
  • Handling expired SSL certificate by code.
Creating a profile in Firefox

This is a one-time activity. Later you will use this profile from your script.

Steps: (Ref- Mozilla Firefox Profile )

  1. If there are already opened Firefox browsers, close them by File->Exit or from task manager Firefox.exe–>Select->Kill
  2. Open windows start menu and select Run(Windows key+r)
  3. Type Firefox.exe-p and hit the ok button.
  4. Firefox “choose User Profile” dialog box opens.
  5. Click on Create Profile to work with Selenium
  6. It will open create profile wizard. Click the Next button to start creating one.
  7. In the second screen of the wizard, provide the name of the profile(TestSelenium). You may alter the folder where the user data will be stored. However, I do not prefer to change the default directory. I generally remember the default directory. The path is something like- C:\Users\UserName\AppData\Roaming\Mozilla\Firefox\Profiles\TestSelenium
  8. Click on the Finish button to close the wizard.
  9. Now choose User Profile windows opens, select the newly created profile.
  10. Click on the start Firefox button.
Handling expired SSL certificate by code.

//access the profile
ProfileIni prof=new ProfilesIni();
FirefoxProfile ffProf= prof.getProfile("TestSelenium");
//setting up the profile for accepting the untrusted certificates. Below two are the capabilities to handle the certificate issues
//Now use this firefox profile to invoke the firefox driver
WebDriver driver=new FirefoxDriver(ffProf);

Expired SSL certificate handling in Chrome

You need to use DesiredCapabilities using Selenium WebDriver to handle SSL certificate error in chrome. The below code snippet will help you to accept all the SSL certificates in chrome. As a result, users will not get any SSL related errors during test automation.

DesiredCapabilities sslErrorHandler=DesiredCapabilities.chrome();
WebDriver driver=new ChromeDriver(sslErrorHandler);
//your remaining code

Expired SSL certificate handling in Internet Explorer[Outdated content as IE is now replaced with Edge browser]

Read this section only if you want to know how IE used to handle the SSL certificate errors. Just like Chrome, we need to use Desired Capability here.

DesiredCapabilities sslErrorHandler=new DesiredCapabilities();
WebDriver driver=new InternetExplorerDriver(sslErrorHandler);
//your remaining code

Share and Enjoy !

Leave a Reply

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