Browser Management with WebDriverManager in Selenium

Introduction

In Selenium testing, managing web browsers used to be a manual headache. Enter WebDriverManager – a tool that simplifies browser setup. This blog explores the old way of launching browsers, why WebDriverManager is crucial, and how to use it efficiently in Selenium projects..

The Traditional Way of Instantiating Browsers in Selenium

In the traditional approach, setting up a Selenium project involved downloading the browser driver executable (like ChromeDriver, GeckoDriver, etc.) manually. Developers would then need to specify the path to the driver executable in their code, making the setup process prone to errors and version mismatches.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class TraditionalExample {

    public static void main(String[] args) {
        // Set the path to the ChromeDriver executable
        System.setProperty("webdriver.chrome.driver", "C:\\Path\\To\\chromedriver.exe");
        // Instantiate ChromeDriver
        WebDriver driver = new ChromeDriver();

        // Set the path to the GeckoDriver executable
        System.setProperty("webdriver.gecko.driver", "C:\\Path\\To\\geckodriver.exe");
        // Instantiate FirefoxDriver
        WebDriver driver = new FirefoxDriver();

        // Set the path to the InternetExplorerDriver executable
        System.setProperty("webdriver.ie.driver", "C:\\Path\\To\\IEDriverServer.exe");
        // Instantiate InternetExplorerDriver
        WebDriver driver = new InternetExplorerDriver();

        // Your Selenium code goes here...

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

Why is WebDriverManager Needed?

WebDriverManager addresses the challenges posed by the traditional method. It automates the process of downloading and managing browser drivers, ensuring compatibility and simplifying setup. With WebDriverManager, developers can focus on writing robust Selenium scripts without the hassle of managing browser drivers manually.

How to Instantiate a Browser Using WebDriverManager in Selenium

To use WebDriverManager in Selenium, follow these steps:

1. Add WebDriverManager Dependency to Your Project

If you are using Maven, add the following dependency to your pom.xml:

<dependency>
    <groupId>io.github.bonigarcia</groupId>
    <artifactId>webdrivermanager</artifactId>
    <version>4.4.3</version> <!-- Check for the latest version on Maven Central -->
</dependency>

For Gradle, add this to your build.gradle:

implementation 'io.github.bonigarcia:webdrivermanager:4.4.3' // Check for the latest version

2. Instantiate WebDriver Using WebDriverManager

Here's an example of how to instantiate a ChromeDriver using WebDriverManager:

import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class WebDriverManagerExample {

    public static void main(String[] args) {
        // Setup ChromeDriver using WebDriverManager
        WebDriverManager.chromedriver().setup();
        // Instantiate ChromeDriver
        WebDriver driver = new ChromeDriver();

        // Your Selenium code goes here...

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

Different Capabilities of WebDriverManager in Selenium

1. Instantiate a Specific Browser Version

WebDriverManager.chromedriver().version("90.0.4430.24").setup();

2. Instantiate a Platform Version (x32 or x64)

WebDriverManager.chromedriver().arch32().setup(); // For 32-bit version
WebDriverManager.chromedriver().arch64().setup(); // For 64-bit version

3. Set a Proxy Username and Password

WebDriverManager.chromedriver()
    .proxy("http://username:password@proxy.example.com:8080")
    .setup();

Conclusion

WebDriverManager in Selenium is a game-changer for browser management, providing a seamless and automated way to handle driver setups. By integrating WebDriverManager into your Selenium projects, you simplify the process of instantiating browsers, manage versions effortlessly, and enhance the maintainability of your test scripts. Embrace the power of WebDriverManager to streamline your Selenium testing journey and focus more on writing reliable test cases. Happy testing!