i

Selenium Step By Step Guide

Desired Capabilities

What is the Desired Capability?

Desired capability is the capability of the automation tester to control the behavior of the web browser at the run time. We can set the browser behavior such as maximize browser window, Handling SSL certification error, Version of the browser, Name of the browser, and many more things that can be done by using DesiredCapabilities class.

DesiredCapabilities class has a range of methods available to set or get the behavioral attributes of a web browser. Let us see all those available methods:

  1. getBrowserName()
  2. getVersion()
  3. setVersion()
  4. setBrowserName()
  5. getPlatform()
  6. setPlatform()
  7. getCapability()
  8. setCapability()

One the most used method of the DesiredCapabilities class is setCapability() method. As this method helps user to set the behavior of the Web Browser at the run time. We will see the use of setCapability() method for Chrome, Firefox and IE browsers.

Chrome Browser:

There are a variety of capabilities there to set up using the setCapability() method of DesiredCapabilities. Let us see how to handle the SSL certification error issue in the Chrome browser by setting up the Desired capability.

package SeleniumTest.SeleniumTest;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.remote.CapabilityType;

import org.openqa.selenium.remote.DesiredCapabilities;

public class SSLCertificationErrorHandling

{

    public static void main( String[] args )

    {

        DesiredCapabilities cap=DesiredCapabilities.chrome();

        cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);// This accepts all the certs without seeing a security risk.

        WebDriver driver=new ChromeDriver(cap);

        driver.get(webUrl);

    }

}

IE web browser:

We can handle SSL certification error and Protected mode setting barrier in IE browser by using DesiredCapabilities class as shown in the below sample code:

package SeleniumTest.SeleniumTest;

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 SSLCertificationErrorHandling

{

    public static void main( String[] args )

    {

      DesiredCapabilities cap=new DesiredCapabilities();

      cap.setCapability("ignoreProtectedModeSettings", true);

      cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);

      WebDriver driver=new InternetExplorerDriver(cap);

      driver.get(webUrl);

    }

}

Firefox browser:

Let us see how we control the behavior of Firefox behavior by using the DesiredCapabilities class, and we will handle the SSL error and setting up the screen resolution of the browser window.

package SeleniumTest.SeleniumTest;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.firefox.FirefoxOptions;

import org.openqa.selenium.remote.CapabilityType;

import org.openqa.selenium.remote.DesiredCapabilities;

public class SSLFirefox

{

    public static void main( String[] args )

    {

        String driverPath="C:\\Users\\AC42610\\geckodriver.exe";

        System.setProperty("webdriver.gecko.driver",driverPath);

        FirefoxOptions options=new FirefoxOptions();

        options.setCapability("acceptInsecureCerts", true);

        DesiredCapabilities cap=new DesiredCapabilities();

        cap.setCapability(FirefoxOptions.FIREFOX_OPTIONS, options);

        cap.setCapability("screenResolution", "1280x1024");

        WebDriver driver=new FirefoxDriver(cap);

        driver.get("https://hyperion-ui-test1.kubeodc-test.corp.intranet/#/home");

    } 

}