Desired Capabilities in Selenium WebDriver

Desired Capabilities is one of the most commonly used concept yet one of the most complex due to many underlying factors which work together with this. When we talk about testing websites, Selenium is our best buddy. It helps us check if everything is working smoothly. But as websites get fancier, testing them becomes trickier. That’s where Desired Capabilities come to our rescue. They help us customize our testing setup to fit our needs perfectly.

Before we dive into DesiredCapabilities, let’s refresh our memory on Selenium basics. To start testing with Selenium using Chrome, we usually write something like this in our code:

WebDriver driver = new ChromeDriver();

This line simply opens up a Chrome browser so we can start testing.

What is Desired Capabilities- The basics

Desired Capabilities are like a magic wand for our testing setup. They let us tweak things exactly the way we want. Instead of sticking to default settings, we can now choose things like which browser version to use or any special settings we need. When your test execution platform requirement gets complex, Desired Capabilities comes to the rescue.

How to use Desired Capabilities

So, We can use DesiredCapabilities in three ways. At least as far as I know. Let’s see how to do this.

Desired Capabilities with configurable browser

See the code below, how a new object is created of DesiredCapabilities and then a driver is created with the help of RemoteWebDriver. Here in this example, the browser is configurable along with other options and parameters for that browser. We use a generic DesiredCapabilities object without tying it to any specific browser.

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("browserName", "firefox"); // Specify the browser
capabilities.setCapability("acceptInsecureCerts", true); // Accept insecure certificates
capabilities.setCapability("marionette", true); // Enable Marionette for Firefox
        
// Additional browser options
Map<String, Object> options = new HashMap<>();
options.put("args", new String[]{"--disable-extensions"}); // Disable browser extensions
capabilities.setCapability("moz:firefoxOptions", options);
        
// Specifying the path to the WebDriver executable
System.setProperty("webdriver.gecko.driver", "path/to/geckodriver");

// Launching the browser with Desired Capabilities
WebDriver driver = new RemoteWebDriver(capabilities);

Desired Capabilities with specific browser

Here there are two examples. One for doing it in simpler way and then a more advanced way. In this example an object of DesiredCapabilities is created but the browser type is not configurable. DesiredCapabilities object is specific to Chrome browser but other option and parameters can be configured here.

Example 1 (Simpler form)
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
// Add more settings if needed
WebDriver driver = new ChromeDriver(capabilities);
Example 2 ( More customised)
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized"); // Maximize the window
options.addArguments("--disable-extensions"); // Disable browser extensions
        
// Setting up Desired Capabilities for Chrome
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
capabilities.setCapability("acceptInsecureCerts", true); // Accept insecure certificates
        
// Specifying the path to the ChromeDriver executable
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        
// Launching Chrome with Desired Capabilities
WebDriver driver = new ChromeDriver(capabilities);

With this code, we’re not only opening Chrome but also telling it to behave in certain ways according to our preferences.

Desired Capabilities with local browsers

Now, you must have noticed that after setting different parameters for the DesiredCapabilities, ultimately we come to the point where we need to launch the browser.

Now there are two ways to launch a browser, one is to launch the local browser and second is to launch the browser on Selenium grid. If you want to run your tests on a grid, you will have to use RemoteWebDriver as above examples. But what if you want to run your tests in your local system with the customisations of DesiredCapabilities? Answer is Yes!

You can use DesiredCapabilities with the convenience of execution in local browsers without using the Grid.

// Specify the browser type (e.g., "chrome" or "firefox")
String browserType = "firefox";

// Setting up Desired Capabilities
DesiredCapabilities capabilities = new DesiredCapabilities();

// Customize capabilities based on the browser type
switch (browserType.toLowerCase()) {
    case "chrome":
        // Setting up Chrome options
        Map < String, Object > chromePrefs = new HashMap < > ();
        chromePrefs.put("profile.default_content_settings.popups", 0);
        capabilities.setCapability("chrome.prefs", chromePrefs);
        capabilities.setCapability("download.default_directory", "path/to/download/directory");
        // Specify other Chrome options or capabilities as needed
        break;
    case "firefox":
        // Setting up Firefox options
        capabilities.setCapability("acceptInsecureCerts", true); // Accept insecure certificates
        // Specify other Firefox specific capabilities as needed
        break;
    default:
        System.out.println("Unsupported browser type: " + browserType);
        return;
}

// Specifying the path to the WebDriver executable
WebDriver driver = null;
switch (browserType.toLowerCase()) {
    case "chrome":
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        driver = new ChromeDriver(capabilities);
        break;
    case "firefox":
        System.setProperty("webdriver.gecko.driver", "path/to/geckodriver");
        driver = new FirefoxDriver(capabilities);
        break;
    default:
        System.out.println("Unsupported browser type: " + browserType);
        break;
}

RemoteWebDriver is like our Selenium buddy who can run tests on other computers or browsers. And yes, Desired Capabilities come into play here too.

Desired Capabilities with RemoteWebDriver

Let’s say we want to run tests on a different computer. We’d do it like this:

DesiredCapabilities capabilities = DesiredCapabilities.chrome(); // Add more settings for remote testing 
RemoteWebDriver driver = new RemoteWebDriver(newURL("http://localhost:4444/wd/hub"), capabilities);

Here, we’re setting up RemoteWebDriver to run our tests on another computer, and we’re still customising Chrome’s behaviour using Desired Capabilities.

You can refer to the official documentation on Desired Capabilities here.

https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/remote/DesiredCapabilities.html#DesiredCapabilities–

Similar Posts

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.