All IT Courses 50% Off
Selenium Tutorials

Desired Capabilities in Selenium WebDriver

What is Desired Capabilities?

Desired Capabilities are a set of properties used to configure the driver instance of Selenium WebDriver. It is the component of the org.openqa.selenium.remote.DesiredCapabilities package. It helps Selenium WebDriver to set the properties for the browser instances. So we can set the properties of browsers by using different capabilities from Desired Capabilities. For example, the browser version, name of the browser, path of the browser driver in the system, etc. We use these desired capabilities as key or value pairs to set them for browsers. By using desired capabilities we can configure driver instances like ChromeDriver, Firefox Driver, InternetExplorerDriver, SafariDriver.

Why do we need Desired Capabilities?

Every scenario that we need to test should be executed on some specific testing environment. The testing environment can be a Mobile device, web browser, mobile simulator and mobile emulator, etc. These desired capabilities class helps to tell webdriver, like which environment is we are going to use in our test script.

The setCapability method is used in Selenium Grid, to perform a parallel execution on different machine configurations. These setCapability method from the DesiredCapabilities class is used to set the different types of capabilities of the browser like Firefox, Chrome, IE, Safari, and Edge etc, and platform name like Windows, Linux, macOS, etc. that are used to while executing the test cases.

Parallel Execution in Different Browsers to Setup Selenium Grid

Desired Capabilities in Appium is useful in mobile application automation, where test automation execution is performed on different browser properties and device properties, which can be set using the Desired Capabilities class. In the case of mobile automation testing, we perform tests on different types of mobile platforms like Android, iOS, etc. Also, there can be different platform versions like for Android 10.x, 11.x and for iOS 12.x, 13.x etc, using these Desired Capabilities in Appium for iOS and Android we can set these properties as per our requirement.

All IT Courses 50% Off

Desired Capabilities are useful in most of the cases like:

  • Browser properties and the device properties can be set in mobile application automation.
  • It can be used in Selenium Grid when we want to run our test cases on different browser with different operating systems and on different versions.

Types of Desired Capabilities Methods

Now, let’s discuss the different types of methods in Desired Capabilities class to set different properties in test automation.

1 getCapability(): 

This getCapability() method of the Desired Capabilities class, can be used to get the capability that is currently use in the test automation.

get.PNG

2. setCapability():

This setCapability() method of the Desired Capabilities class, can be used to set the name of device, platform name, version of platform, absolute path of the application under test, application activity (in Android Mobile automation), application Package (Java) etc.

In Java “setCapability” method has following declarations:

set.PNG

3. getBrowserName(): 

This getBrowserName() method of the Desired Capabilities class, is used to get the name of the Browser.

browser.PNG

4. setBrowserName(): 

This setBrowserName() method of the Desired Capabilities class, is used to set the name of the Browser.

setb.PNG

5. getVersion():

This getVersion() method of the Desired Capabilities class, is used to get the version of the browser.

getver.PNG

6. setVersion():

This setVersion() method of the Desired Capabilities class, is used to set the version of the browser.

setver.PNG

7. getPlatform():

This getPlatform() method of the Desired Capabilities class, is used to get the  details of the platform.

getplat.PNG

8. setPlatform():

This setPlatform() method of the Desired Capabilities class, is used to set the  details of the platform.

setplat.PNG

Desired Capabilities for Different Browsers in Selenium Testing

In the above part, we have discussed different methods of Desired Capabilities class. Now, let us discuss how we can set different capabilities for different browsers in selenium testing.

Desired Capabilities for Chrome in Selenium WebDriver

In Selenium WebDriver when we want to configure and customize the Desired Capabilities for Chrome browser, we need to invoke the ChromeDriver session to use these capabilities. So we will discuss the capabilities that are supported by ChromeDriver and how we can use them to set those desired capabilities for Chrome Browser in Selenium Webdriver.

There are two different ways to set the capabilities for ChromeDriver.

  1. ChromeOptions
  2. Desired Capabilities

ChromeOptions is another type of class which can be used in conjunction with Desired Capabilities for Chrome in Selenium WebDriver to customize or to manipulate the various properties of Chrome browser. Following arguments are commonly used from ChromeOptions class.

Disable-infobars: It is used to prevent in displaying notifications in chrome browser like “Chrome is being controlled by automated software”

Make-default-browser: It is used to make the chrome browser as the default browser.

Disable-popup-blocking: It is used to disable the pop-ups that are displayed on chrome.

Incognito: It is used to open the chrome browser in the incognito mode.

start -maximized: It is used to open the chrome browser in the maximized mode.

Headless: It is used to open the chrome browser in the headless mode.

Now, let us create an example for an ad blocker where Desired Capabilities class is used in Chrome Option. To implement this we will use an adblocker extension.
crx file of the extension should be downloaded as a Prerequisite for that.

//Setting up capabilities to run our test script

ChromeOptions opt = new ChromeOptions();
opt.addExtensions(new File(“path of crx file extension”));
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
ChromeDriver driver = new ChromeDriver(capabilities);

Desired Capabilities in Selenium WebDriver for Internet Explorer

Now, let us discuss with Desired Capabilities for IE(Internet Explorer) in Selenium WebDriver. For Internet Explorer, we use InternetExplorerOptions with Desired Capabilities in Selenium WebDriver.

There are certain specific capabilities for Internet Explorer which we can use, let us see some of them.

a) ignoreZoomSetting(boolean): This capability is used to ignore the checking of the browser’s zoom level to 100%. By default, it is set to false. It takes input value as boolean.

b) initialBrowserUrl(string): This capability will decide initial browser URL, and to the website it should redirect while internet explorer browser starts. It takes input as string.

c) enableElementCacheCleanup(boolean): This capability will check over the elements from the element cache and instructs the WebDriver to clean it up if they cache is found. It takes input as boolean value.

d) requireWindowFocus(boolean): This capability is used to instruct the IE driver to check the internet explorer window has the focus before performing any operations like a mouse or keyboard events etc. It takes input as boolean value and by default, it is set to false.e) ignoreProtectedModeSettings(boolean): If you want to skip the protected mode check while performing automation testing then you can use this capability. It takes input as boolean value.

if(driverParameter == null || driverParameter.equalsIgnoreCase(IE))
{
    DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
    capabilities.setCapability("requireWindowFocus", true);
    capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
}

Desired Capabilities for Java in Selenium WebDriver

//Setting up capabilities to run our test script in Java
    @BeforeClass
     public void setUp() throws Exception {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("browserName", "chrome");
        capabilities.setCapability("version", "79.0");
        capabilities.setCapability("platform", "win10");
        capabilities.setCapability("build", "TestingSampleApp");
        capabilities.setCapability("name", "TestingJavaSample");
        capabilities.setCapability("network", true); // To enable logs
        capabilities.setCapability("visual", true); // To enable screenshots
        capabilities.setCapability("video", true); // To enable video recording
        capabilities.setCapability("console", true); // To get console logs

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

Example for set Capability method
Let us consider an example to open www.yahoo.com website on Internet explorer browser using Selenium Webdriver

importorg.openqa.selenium.WebDriver;
importorg.openqa.selenium.ie.InternetExplorerDriver;

public class IEtestforDesiredCapabilities {
  
 public static void main(String[] args) {
  
WebDriver driver = new InternetExplorerDriver();
 driver.manage().window().maximize();
 driver.get("http://yahoo.com");
  
 driver.quit();
 }
}

When you run the above code it will throw an error The path to the driver executable must be set by the webdriver.ie.driver system property; The following error occurs because the path to the IE browser driver is not set. The browser could not be identified by the written selenium code. To overcome the above problem we need to follow the below steps.

  • Download the 32bit or 64bit for Internet Explorer Driver standalone server.
  • Save the driver in a particular location.
  • Set the driver path using the System.setProperty method.
  • It is used to set the webdriver property using IE driver. It helps the driver executable file to locate that which is stored in the system location. (Ex:”C:\IEDriverLocation\IEDriver.exe”)
importorg.openqa.selenium.WebDriver;
importorg.openqa.selenium.ie.InternetExplorerDriver;
importorg.openqa.selenium.remote.DesiredCapabilities;
 
public class DesiredCapabilitiesIEtest {
  
 public static void main(String[] args) {
 
 DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
  
capabilities.setCapability(CapabilityType.BROWSER_NAME, "IE");
capabilities.setCapability(InternetExplorerDriver.
  INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
 
System.setProperty("webdriver.ie.driver", "C:\\IEDriverServer.exe");
  
 //Initialize the IE driver
 WebDriver driver = new InternetExplorerDriver(capabilities);
 driver.manage().window().maximize();
 driver.get("http://yahoo.com");
  
 driver.quit();
 }
}

When you run the above test case it will run successfully on Internet Explorer browser using Selenium Webdriver.

Conclusion

The Desired Capabilities class will help us to set an environment to know the behaviour of the browser/environment on which the test can be executed.

It helps to launch our application in the desired environment by using the capabilities.

Facebook Comments

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles

Back to top button