Automate Browser Driver Management with WebDriverManager: A Step-by-Step Guide

Introduction

Web automation in Java is straightforward in concept: open a browser and interact with a web page. However, a hidden challenge emerges from the browser itself: binary compatibility. Each browser requires a specific driver binary, and that binary must exactly match the installed browser version. Even a minor mismatch triggers runtime errors. WebDriverManager is a Java library that solves this by automatically resolving, downloading, and configuring browser drivers for Selenium projects. In this guide, you'll learn how to eliminate manual driver management and make your test setups portable and reliable.

Automate Browser Driver Management with WebDriverManager: A Step-by-Step Guide
Source: www.baeldung.com

What You Need

Step-by-Step Guide

Step 1: Understand the Problem with Manual Driver Management

In a traditional Selenium setup, you specify the driver path explicitly using System.setProperty. For example, for Chrome:

System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();

This approach works initially but doesn't scale. Every time your browser updates, you must manually download and replace the driver. In shared environments like CI/CD pipelines or team projects, maintaining consistent driver versions becomes a nightmare. Hardcoded paths also make your code less portable across different machines. WebDriverManager removes these burdens by handling driver resolution dynamically.

Step 2: Add WebDriverManager as a Dependency

Include WebDriverManager in your project using your build tool.

For Maven, add this to your pom.xml:

<dependency>
    <groupId>io.github.bonigarcia</groupId>
    <artifactId>webdrivermanager</artifactId>
    <version>6.3.3</version>
    <scope>test</scope>
</dependency>

For Gradle, add to your build.gradle:

dependencies {
    testImplementation("io.github.bonigarcia:webdrivermanager:6.3.3")
}

Make sure to check for the latest version.

Step 3: Configure WebDriverManager in Your Code

Instead of setting system properties manually, you call a static method from WebDriverManager. For Chrome:

import io.github.bonigarcia.wdm.WebDriverManager;

public class WebDriverSetup {
    public static void main(String[] args) {
        WebDriverManager.chromedriver().setup();
        // Now you can safely create a ChromeDriver instance
        WebDriver driver = new ChromeDriver();
    }
}

WebDriverManager will:

Step 4: Create and Use the WebDriver

After calling setup(), you can directly instantiate the driver. Here’s a complete example that opens Google’s homepage:

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

public class QuickTest {
    public static void main(String[] args) {
        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();
        
        driver.get("https://www.google.com");
        System.out.println("Page title: " + driver.getTitle());
        
        driver.quit();
    }
}

Run it – you’ll see the browser launch automatically with no driver path errors.

Automate Browser Driver Management with WebDriverManager: A Step-by-Step Guide
Source: www.baeldung.com

Step 5: Integrate with Test Frameworks

WebDriverManager works seamlessly with JUnit 5 or TestNG. For example, in a JUnit 5 test:

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;

public class GoogleSearchTest {
    WebDriver driver;

    @BeforeEach
    void setUp() {
        WebDriverManager.chromedriver().setup();
        driver = new ChromeDriver();
    }

    @Test
    void testGoogleSearch() {
        driver.get("https://www.google.com");
        // Your assertions…
    }

    @AfterEach
    void tearDown() {
        driver.quit();
    }
}

Step 6: Explore Advanced Features (Optional)

WebDriverManager goes beyond basic driver setup. It offers:

For Firefox, call WebDriverManager.firefoxdriver().setup(); for Edge, WebDriverManager.edgedriver().setup().

Tips for Success

By following these steps, you’ll eliminate the pain of manual driver management and create robust, portable Selenium automation scripts.

Tags:

Recommended

Discover More

AI-Driven Vulnerability Discovery Triggers Urgent Security Alert for EnterprisesThe 1996 Doctor Who Movie: A Cross-Atlantic Gamble That Failed to ResonateAstronaut Health Monitor: A Step-by-Step Guide to Using the Artemis Wristband for Lunar Mission Risk AssessmentHow to Understand the Strategic Significance of Nebius's $643M Acquisition of Eigen AIHow to Leverage AI for Early Pancreatic Cancer Detection via CT Scans