Site icon Automation Dojos

Desired Capabilities in Selenium Web Driver

1. Desired Capabilities in Selenium

The performance of a Web application may vary according to different browsers and operating systems. Hence to ship out a near-consistent functional and performant application, it calls for testing your applications against different browsers or browser configurations. That is where Selenium’s ‘Desired Capabilities’ class comes into the picture.

‘DesiredCapabilities’ is a class in Selenium that can be used to define and set some generic capabilities or settings for browsers. The ‘Desired Capabilities’ applies to WebDriver and is used to modify the properties of the web driver.

This class resides in the org.openqa.selenium.remote.DesiredCapabilities package. We use these desired capabilities as key or value pairs to set them for browsers. In this tutorial, we will discuss the ‘Desired Capabilities’ feature in Selenium in detail.

2. What about ChromeOptions Class

Chrome Options class provides methods for setting ChromeDriver-specific capabilities and hence can be used for manipulating various properties of the ChromeDriver. Similar to Chrome there are browser options classes available for Firefox, Safari, Edge and Internet Explorer, etc as well.

Given below is a simple example of ChromeOptions usage to give you an idea. The example below shows how to open the chrome browser in Maximize mode.

ChromeOptions chromeOptions = new ChromeOptions()
chromeOptions.addArgument("start-maximized");
ChromeDriver driver = new ChromeDriver(chromeOptions);

As you can see in the above example the scope of ‘ChromeOptions’ applies to Chrome or Chrome Driver while the ‘Desired Capability’ applies to WebDriver. This will get more clear with the below examples.

3. When and Where to Use

Generally, the ChromeOptions class and all such browser-specific classes (FirefoxOptions, Safari, Internet Explorer) are used in conjunction with ‘Desired Capabilities’ for customizing the behavior of your Selenium WebDriver.

So you can set your browser-specific options using browser-specific classes such as ChromeOptions, FireFoxOptions, SafariOptions, etc. You can then pass on these objects to the DesiredCapabilities class object. So now your desired capability knows what you want for different types of browsers.

To further spice-up things you can use your prepared and customized ‘DesiredCapabilities’ object in Selenium Grid targeting different machines or browsers. This may give you what we call a multi-browser test environment where the same test case needs to be executed on different browsers.

4. Available Capabilities for Browser Types

Chrome offers a long list of options to control the browser settings. You can find this whole list of switches here. Besides here are some most commonly used arguments for ChromeOptions class :

4.1) ChromeOptions for Chrome

  • start-maximized: Used to open Chrome in maximize mode
  • incognito: Used to open Chrome in incognito mode
  • headless: Used to open Chrome in headless mode
  • disable-extensions: Used to disable existing extensions on Chrome browser
  • disable-popup-blocking: Used to disable pop-ups displayed on Chrome browser

4.2) Firefox, Safari, Edge and IE Options

Similarly, you can checkout browser options for other browsers like Safari, IE, Edge, Firefox, etc. on the following links:

FirefoxSafari
EdgeInternet Explorer

4.3) DesiredCapability Class Options

The Selenium WebDriver’s DesiredCapability class as well offers a long list of capabilities and options. Some of the most used common ones are:

  • ACCEPT_SSL_CERTS: This property tells the browser to accept SSL Certificates by default
  • PLATFORM_NAME: This property is used to set the operating system platform used to access the web site
  • BROWSER_NAME: This property is used to set the browser name for a web driver instance
  • VERSION: This property to used to set the browser version

Also, you can see the whole list of DesiredCapabilities options on this page on GitHub

5. Desired Capabilities Methods and Types

Let’s look at the methods available with ‘Desired Capabilities’ class. As mentioned ‘org.openqa.selenium.remote.DesiredCapabilities’ is the package that has the class named DesiredCapabilities as a component. Selenium web drivers can change and set up properties for web browsers from their methods. Let’s discuss some commonly used methods in the DesiredCapabilities package.

  • getBrowserName() This method is used to get the name of the browsers.
  • setBrowserName() This method is used to set the name of the browsers.
  • getCapability() This method is used to obtain the capabilities of the using system.
  • setCapability() This method can be used to give a name for the platform and device, version of the platform used, the absolute path, or the “.apk” of the user application under testing, application package, and application activity.
  • getVersion() This method returns the version of the platform or browser.
  • setVersion() The browser version and platform version can be set by this method.
  • getplatform() This method returns information about the platform.
  • setPlatform() Platform information can be set by using this platform.

6. Usage Scenarios for DesiredCapabilities

Now, that you know the methods in DesiredCapabilities class, let’s see some of their common uses. First, let’s do an example using just the ‘DesiredCapabilities’ class.

6.1) DesiredCapabilities Example

The below example shows how Desired Capabilities enables chrome browsers to accept SSL certificates on websites. With Desired Capabilities, it is just three lines of code. You just have to use the “CapabilityType.ACCEPT_SSL_CERTS” with setCapabilities method. See the code below.

(a) Accept SSL Certs

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
WebDriver driver = new ChromeDriver(capabilities);

6.2) DesiredCapabilities with ChromeOptions

ChromeOptions class is used to control the properties of the Chrome browser. The example below shows how to open the chrome browser in Maximize mode.

ChromeOptions chromeOptions = new ChromeOptions()
chromeOptions.addArgument("start-maximized");
ChromeDriver driver = new ChromeDriver(chromeOptions);

(a) Start Chrome Maximized

Now we can create a ‘Desire Capabilities’ class object and pass the ‘options’ we had set for our chrome object (which in this case is the (‘start maximized). Once the ‘Desired Capability’ object has received the options set for ‘chrome browser’ in this case, then we can further create our ‘Web Driver’ object using this ‘capability model’ as parameter.

ChromeOptions chromeOptions = new ChromeOptions()
chromeOptions.addArgument("start-maximized");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
WebDriver driver=new ChromeDriver(capabilities);

(b) Disable Chrome Extensions

The following code disables extensions of Chrome which is particularly useful when you want to disable developer mode while testing.

ChromeOptions chromeOptions = new ChromeOptions()
chromeOptions.addArgument("--disable-extensions");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
WebDriver driver=new ChromeDriver(capabilities);

(c) Start Chrome Incognito Mode

Likewise, If you want to open the incognito window, use “–incognito” argument in the addArdument() method.

ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--incognito");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
WebDriver driver=new ChromeDriver(capabilities);

(d) Enable Adblocker in Chrome

Finally, let’s go for a more complicated one. You definitely need to enable an adblocker while testing through a browser. Enabling the adblocker has few steps. You have to download and extract the CRX file related to the Adblocker extension from here. Then you have to pass the path of the extracted file in the code. The following code will do the task.

ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addExtensions(new File("Path to CRX File"));
DesiredCapabilities caps = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
ChromeDriver chromeDriver = new ChromeDriver(caps);

6.3) DesiredCapabilities with FirefoxOptions

Using Desired Capabilities with Firefox and IE is quite similar to how we do it with Chrome. So let’s do just one example from each of them. The following code lets you open Firefox in headless mode.

(a) Start Firefox in Headless Mode

DesiredCapabilities desiredCapabilities = DesiredCapabilities.firefox();
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setHeadless(headless);
desiredCapabilities.merge(firefoxOptions);

6.4) DesiredCapabilities with InternetExplorer

(a) Enable IE for SSL and Check Focus

Similarly, You can check that if the Internet Explorer has the focus before you do start any interactions with it. The below code also force IE to accept SSL certificates.

DesiredCapabilities desiredCapabilities = DesiredCapabilities.internetExplorer();
desiredCapabilities.setCapability("requireWindowFocus", true);
desiredCapabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);

Conclusion

Desired Capabilities are the beginning of advanced testing. It’s something really important to know if you are going to do anything more than basic testing. They provide so much functionality with so little code. Using them is pretty simple and quite similar across different uses. I hope you enjoyed our tutorial. Let’s meet again with another exciting tutorial.

Exit mobile version