Desired Capabilities: Introduction and Implementation in Selenium

Every testing starts in some test environments like Local environment, Release environment, production environment, BAU environment, etc. Like these environments, test devices also create a kind of environments and we record the behavior of Application under test in these environments. Test device environment would be like testing in Android device, iOS device, Tablet, and Desktop device. We discussed earlier that test distribution on different devices is done with the help of Selenium Grid. In the same way, we can set properties of the browsers in such environments through Desired Capabilities. Hence, the agenda of this tutorial is about the use of Desired capabilities in Selenium WebDriver.

What are the Desired Capabilities in Selenium?

The desired capability is the collection of some attributes and their values which is used to configure browser properties like browser name, version, platform and much more. It even helps to configure the driver instances, ChromeDriver, FirefoxDriver, etc through Desired capabilities.

Significances of Desired Capabilities

Desired capabilities have a great significance with Selenium Grid configuration. Let’s have a quick look over some of the benefits of it with respect to the Selenium project configuration:

  • It helps in mobility testing to set the properties of the browser and device as well
  • The desired capability has very much importance in setting the browser’s properties during test distribution through Selenium Grid
  • It is the best way to work with Cross browser testing

DesiredCapabilities class is basically used to configure browser properties remotely. We need to import the following file from Selenium remote library before we proceed to the implementation part:

import org.openqa.selenium.remote.DesiredCapabilities;

Desired Capabilities Inviul

Some of the Desired Capabilities method

Here is the list of some of the methods which we use while implementing Desired capabilities in Selenium. These methods are like getters and setters, they simply used to set the properties and their value and to get their values as well.

  • getBrowserName()
  • setBrowserName()
  • getPlatform()
  • setPlatform()
  • getVersion()
  • setVersion()
  • getCapability()
  • setCapability()
  • acceptInsecureCerts()
  • setJavaScriptEnabled()
  • setAcceptInsecureCerts()

You first need to create an instance of the class then you can use the above methods either to set the property or to get the properties. Here is the generic class instance declaration.

DesiredCapabilities capability = new DesiredCapabilities();

DesiredCapabilities with respect to a browser/platform

We can instantiate DesiredCapabilities class directly with respect to specific browser without using setBrowserName() method or setCapability() method.

DesiredCapabilities cap = DesiredCapabilities.chrome();

Here is a list of browsers and platform supported by DesiredCapabilities class.

Desired capabilities browserCapability Type Arguments

There are some capability types which is used to pass as arguments inside setCapability() method to perform some quick actions. Here is the sample implementation then we will see the list of some capability types.

DesiredCapabilities cap = DesiredCapabilities.chrome();

cap.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);

List of Capability Types

  • ACCEPT_INSECURE_CERTS
  • BROWSER_VERSION
  • ACCEPT_SSL_CERTS
  • APPLICATION_NAME
  • BROWSER_NAME
  • TAKES_SCREENSHOT
  • PROXY
  • LOGGING_PREFS
  • ELEMENT_SCROLL_BEHAVIOUR
  • ENABLE_PERSISTENT_HOVERING
  • HAS_NATIVE_EVENTS
  • ENABLE_PROFILING_CAPABILITY
  • HAS_TOUCHSCREEN
  • SUPPORTS_APPLICATION_CACHE
  • PLATFORM
  • VERSION
  • PLATFORM_NAME

These are some of the capability types which we can use whenever required. Click here to know more.

How to implement Desired capabilities in the Selenium project?

After all the theoretical discussion, let’s have a look over the implementation of the Desired capabilities in our Selenium WebDriver project. Here is the sample program:

package Test;

import java.util.concurrent.TimeUnit;

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 DesiredCapabilitiesDemo {

	public static void main(String[] args) {
		
		DesiredCapabilities capability = DesiredCapabilities.chrome();
		
	     capability.setCapability(CapabilityType.BROWSER_NAME, "CHROME");
		
		System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver.exe");	
		 
		WebDriver  driver  = new ChromeDriver(capability);
		
		 driver.get("http://www.inviul.com/");
	  	  
	  	  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		  
	  	  driver.manage().window().maximize();
	  	  
	  	 System.out.println(driver.getTitle());
	  	 
	  	 driver.close();
	  	 
	  	 driver.quit();

	}

}

Note: I would like to tell you that the above technique to implement DesiredCapabilities is not practiced these days. A new alternative technique has come which we will discuss separately in the next tutorial.

Join Inviul fb group

Leave a Reply