Capabilities in Selenium Java

Capabilities in Selenium Java

The desired capabilities in selenium Java testing is a class that is used to declare the some necessary basic requirements of the project, for example, declaring the combination of browsers, browser versions, operating systems, etc., to automate cross browser testing of a web application.

The capabilities are useful for automating the test scripts using Selenium testing. We need to configure the capabilities environment to declare a specific test environment over which our web application will be rendered seamlessly. These environments can be   web browsers, mobile devices, desktop devices, screen resolutions, etc. In order to automate these environments scripts, we need to use the desired capabilities in Selenium Testing or Appium Testing.

In this section, we will discuss what the desired capabilities in Selenium Java testing is and how they will be used for Selenium automation testing. Furthermore, we will some examples of the desired capabilities in Selenium testing.

In this section, we have included:

What Are Desired Capabilities in Selenium Testing

The Desired Capabilities is a class in selenium testing that is used to decalre and set the basic properties of a web browser to conduct the cross browser testing of a web application. It is a component of the org.openqa.selenium.remote.DesiredCapabilities package. It stores the capabilities in the form of key-value pair combination. These value are useful for setting the some basic nroser properties such as broser name, broser version, etc. Thus, it demonstrate browser’s behaviour at runtime.

In Selenium Testing, we need to specify some basic things such as which browser to be exectuted, operating system, and version. Here, the capabilities become handy for us.

The setCapabilities methodof the DesiredCapabilities class allows us to set the different properties of the of the web browsers such as Edge, Firefox, Chrome, Safari, etc., and operating system Windows, Linux, macOS, etc.

Why Do We Need Desired Capabilities?

Every testing scenerio will be executed on a specific environment, so, to define these testing environment we are required to define the Capabilities. These environments can be a mobile device, desktop device, web browser, mobile emulator, simulator, etc. The Desired Capabilities class allows us to define the environment for the webdriver.

The setCapabilities method can be used in Selenium Grid to perform a parrallel execution on different environments. Below image demonstrate a Selenium Grid:

Capabilities in selenium Java

We can specify the different types of browsers such as chrome, edge, firefox, etc and platforms such as Windows, Linux, etc by defining the test cases using the capabilities.

In case of mobile devices, the Mobile platform such as Android, iOS and their vversion such as 10.x, 3.x can be specified.

The following are some cases for which the Desired Capabilities will be very useful:

  • To set the properties of the web browser.
  • To automate the mobile application.
  • To run the test cases on a specific browser on different operationg systems and versions.
  • It is handy in a Selenium Grid to run parallel exection.

Desired Capabilities Methods

The following are some useful methods provided by DesiredCapabilities calss:

setCapability()

The setCapability() is a component of DesiredCapabilities class. It is used to set the basic properties such as device name, platform name, platform version, absolute path of the web application, web broser name and version,  activity in mobile application, package, etc.

The declaration for the setCapability() method are as follows:

1.	public void setCapability(java.lang.String capabilityName, boolean value)
2.	public void setCapability(java.lang.String capabilityName, java.lang.String value)
3.	public void setCapability(java.lang.String capabilityName, Platform value)
4.	public void setCapability(java.lang.String key, java.lang.Object value)

getCapability()

The getCapability() meyhod is also a component of the DesiredCapabilities class. It is used to access the values of the current system which are being used in the sytem. It return the value, or non if not set.

The declaration for the getCapability() method is as follows:

public java.lang.Object getCapability(java.lang.String capabilityName)

getBrowserName()

The getBroserName() method is used to get the name of current broser in the system. The declaration for the getBroserName() method is as follows:

default java.lang.String getBrowserName()

setBrowserName()

The setBroserName() method is used to set the browser name value. It is a component of the DesiredCapabilities class. The declaration for the setBrowserName() is as follows:

public void setBrowserName(java.lang.String browserName)

setVersion()

The setVersion() method is used to set the version of the broser or platform. It is decalred as follows:

public void setVersion​(java.lang.String version)

getVersion()

The getVersion() is used to get the browser and platform version. It is declared as follows:

public java.lang.String getVersion()

setPlatform()

The setPlatform() is used to set the platform name of the system. It is declared as follows:

public void setPlatform​(Platform platform)

getPlatform()

The getPlatform() is used to get the platform name of the system. It is declared as follows:

public Platform getPlatform()

Desired Capabilities for Selenium Testing for Different Browsers

Let’s understand how to set different capabilities for different browsers:

Desired Capabilities in Selenium WebDriver for Chrome

To customize and configure the the capabilities in Selenium WebDriver for chrome, we must invoke the ChromeDriver session. Before understanding how to customize and configure the capabilities in chrome, let’s understand the capabilities supported by the ChromeDriver and how to set them in Selenium WebDriver for chrome.

The following are two capabilities which are supported by the ChromeDriver:

1. ChromeOptions.

2. Desired Capabilities.

The ChromeOption is another class used with the DesiredCapabilities to customize and manipulate the different properties of the chrome.

There are different arguments from the ChromeOptions class. Some useful arguments are as follows:

Disable-infobars

  • Make-default-browser
  • Disable-popup-blocking
  • Incognito
  • Start -maximized
  • Headless

For example, to enable an adblocker using Capabilities, we have to use the ChromeOptions with DesiredCapabilities class. An adblocker extension will be used for this.

Below is the configuration code for the prerequisite for a crx file of the extension that should be downloaded:

Setting up capabilities to run our test script
ChromeOptions opt = new ChromeOptions();
opt.addExtensions(new File(“path for crx file of the extension”));
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
ChromeDriver driver = new ChromeDriver(capabilities);

Desired Capabilities in Selenium WebDriver for Firefox

We can also configure and customize the capabilities in Selenium webDriver for firefox browser: there is a  moz:firefoxOptions capability is available for the firefox web browser; we can use it to customize or manipulate different properties of the Firefox browser. It provide a control over the behavior of the browser. The firefox provides the following functionalities to control the execution of the firefox browser:

Binary (String): We can customize the firefox binaries by providing an absolute path.

Args(array of strings): The args is used to pass the command line arguments to the firefox binary.

Profile(string): We canspecify the path for the directory to use the firefox instance. It is useful for installing the extensions or custome certification in firefox.

Below is an example of of declaring Desired Capabilities in Selenium WebDriver for Firefox:

{
"capabilities": {
"alwaysMatch": {
"moz:firefoxOptions": {
"binary": "/usr/local/firefox/bin/firefox",
"args": ["-headless", "-profile", "/path/to/my/profile"],
"prefs": {
"dom.ipc.processCount": 9,
"javascript.options.showInConsole": true
},
"log": {"level": "trace"}
}
}
}
}
if(driverParameter == null || driverParameter.equalsIgnoreCase(FIREFOX))
{
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
FirefoxOptions options = new FirefoxOptions();
options.setHeadless(headless);
capabilities.merge(options);
}

Desired Capabilities in Selenium WebDriver for IE(Internet Explorer)

To use the DesiredCapabilities in Selenium WebDriver for IE, we will use InternetExplorerOptions with Capabilities class:

Below is an example of declaring Desired Capabilities in Selenium WebDriver for IE:

if(driverParameter == null || driverParameter.equalsIgnoreCase(IE))
{
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
InternetExplorerOptions option = new InternetExplorerOptions();
option.setHeadless(headless);
capabilities.merge(option);
}

Desired Capabilities in Selenium WebDriver for Java

We have discussed the Desired Capabilities for the different web browsers. Now, we will understand how to implement the capabilities in Selenium Testing using Java. However, Selenium automation supports multiple programming languages;

Below code illustrates the binding of Desired Capabilities in Selenium WebDriver using Java:

//Setting up capabilities to run our test script
@BeforeClass
public void setUp() throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("browserName", "chrome");
capabilities.setCapability("version", "77.0");
capabilities.setCapability("platform", "win10"); // If this cap isn't specified, it will just get any available one
capabilities.setCapability("build", "LambdaTestSampleApp");
capabilities.setCapability("name", "LambdaTestJavaSample");
capabilities.setCapability("network", true); // To enable network logs
capabilities.setCapability("visual", true); // To enable step by step screenshot
capabilities.setCapability("video", true); // To enable video recording
capabilities.setCapability("console", true); // To capture console logs

capabilities.setCapability("selenium_version","4.0.0-alpha-2");
capabilities.setCapability("timezone","UTC+05:30");
capabilities.setCapability("geoLocation","IN");
capabilities.setCapability("chrome.driver","78.0");
try {
driver = new RemoteWebDriver(new URL("https://" + username + ":" + accesskey + gridURL), capabilities);
} catch (MalformedURLException e) {
System.out.println("Invalid grid URL");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}

Summary:

The DesiredCapabilities class provides us different methods to configure the behaviour of the broser or environment on which we want to execute the test case.