Desired Capabilities in Selenium

Desired Capabilities in Selenium

What Are Desired Capabilities?

Desired Capabilities is a class used to set the properties of browser before invoking it such as browser name, operating system, browser version etc. Its a setup of key value pair that is used to configure the setting of browser.

Basically this allows you to have a better control over the browser which you want to automate.

Key Characteristics of Desired Capabilities :

  • Browser Name : Specify which browser to launch (Chrome, Firefox) etc.

  • Version : Define the browser version you wish to launch.

  • Platform : Indicates the operating system on which the the test will run.

  • Screen : Resolution of browser window can be set.

  • JavaScript : Enable or disable the JavaScript in the browser.

Importance of Desired Capabilities

Desired Capabilities allows you to :

  • Control browser behavior : Tailor the browser environment to meet the specific testing requirement.

  • Cross-Browser Testing: It helps testing across different browsers and their versions to ensure application compatibility.

  • Remote Testing: Running tests on remote machines or cloud services (like Selenium Grid or BrowserStack), Desired Capabilities are essential to communicate the testing environment’s needs.

Setting Up Desired Capabilities in Selenium with Java

Configure Desired Capabilities

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.chrome.ChromeOptions;

public class DesiredCapabilitiesExample {
    public static void main(String[] args) {

        // Create an instance of DesiredCapabilities
        DesiredCapabilities capabilities = new DesiredCapabilities();

        // Set desired capabilities
        capabilities.setCapability("browserName", "chrome");
        capabilities.setCapability("version", "92.0");
        capabilities.setCapability("platform", "WINDOWS");
        capabilities.setCapability("screenResolution", "1920x1080");

        // Additional options using ChromeOptions
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--start-maximized"); // Maximize the browser window
        options.addArguments("--disable-infobars"); // Disable the info bars
        capabilities.setCapability(ChromeOptions.CAPABILITY, options);

        // Initialize the WebDriver with the desired capabilities
        WebDriver driver = new ChromeDriver(capabilities);

        // Open a website
        driver.get("https://www.google.com");

        // Testing logic here

        // Close the browser
        driver.quit();
    }
}

Code Explanation

  • DesiredCapabilities Instance: An instance of DesiredCapabilities is created to set the required capabilities.

  • Setting Capabilities: Various capabilities are set, including the browser name, version, platform, and screen resolution.

  • ChromeOptions: Additional options specific to Chrome are configured using ChromeOptions.

  • WebDriver Initialization: The ChromeDriver is initialized with the defined capabilities.

  • Running the Test: The browser is directed to a specified URL, and any required test logic can be executed.

  • Closing the Browser: Finally, the browser session is terminated.

Common Use Cases for Desired Capabilities

  • Running Tests on Remote WebDriver:

    When you want to execute tests on a remote server, Desired Capabilities specify the browser and environment settings.

      // Example for Remote WebDriver
      URL url = new URL("http://localhost:4444/wd/hub");
      RemoteWebDriver driver = new RemoteWebDriver(url, capabilities);
    

Conclusion

Desired Capabilities in Selenium with Java allows you to customize your testing environment to suit your needs. By understanding how to configure these capabilities, you can enhance your test automation strategies, perform cross-browser testing, and streamline remote testing processes.

By enhancing you Desired Capabilities knowledge you can increase the overall quality of your software products.

Did you find this article valuable?

Support Perf Insights by becoming a sponsor. Any amount is appreciated!