How to Automate Browser Driver Management with WebDriverManager in Java

From Xshell Ssh, the free encyclopedia of technology

How to Automate Browser Driver Management with WebDriverManager in Java

Automating web interactions in Java with Selenium is straightforward—until you face the browser driver mismatch problem. Every browser version requires a specific driver binary, and a small update can break your tests. Manually downloading, storing, and updating drivers is tedious and error-prone, especially in team or CI/CD environments. WebDriverManager is a Java library that solves this by automatically resolving, downloading, and configuring the correct driver for your installed browser—no manual System.setProperty calls needed. This guide walks you through setting up and using WebDriverManager step by step.

How to Automate Browser Driver Management with WebDriverManager in Java
Source: www.baeldung.com

What You Need

  • Java Development Kit (JDK) (version 8 or later)
  • Maven or Gradle build tool
  • Selenium Java bindings (version 4.x recommended)
  • A modern web browser (Chrome, Firefox, Edge, etc.)
  • An IDE (IntelliJ, Eclipse, or VS Code)

Step-by-Step Guide

Step 1: Understand the Problem WebDriverManager Solves

Before coding, recognize why manual driver management fails. With traditional Selenium, you have to:

  • Download the driver binary (e.g., ChromeDriver) that matches your browser version exactly.
  • Place the binary somewhere on disk and set the system property:
    System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
  • Repeat this for every version update—and for every team member and CI machine.

WebDriverManager eliminates this friction. It detects the installed browser version, finds the compatible driver, downloads it to a cache, and sets all required system properties automatically.

Step 2: Add WebDriverManager as a Dependency

WebDriverManager is available on Maven Central. Add it to your project’s build file.

For Maven (pom.xml):

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

For Gradle (build.gradle):

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

Many projects also use Selenium; ensure you have the selenium-java dependency as well.

Step 3: Configure WebDriverManager in Your Code

After adding the dependency, replace the manual System.setProperty call with a one-liner. For Chrome:

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

public class WebDriverManagerDemo {
    public static void main(String[] args) {
        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();
        driver.get("https://example.com");
        // your test code here
        driver.quit();
    }
}

The setup() method checks your Chrome version, downloads the matching ChromeDriver binary (if not already cached), and sets webdriver.chrome.driver automatically.

Step 4: Use WebDriverManager with Different Browsers

WebDriverManager supports all major browsers. Replace chromedriver() with the appropriate method:

  • Firefox: WebDriverManager.firefoxdriver().setup(); → then new FirefoxDriver()
  • Edge: WebDriverManager.edgedriver().setup(); → then new EdgeDriver()
  • Opera: WebDriverManager.operadriver().setup(); → then new OperaDriver()
  • Internet Explorer: WebDriverManager.iedriver().setup(); → then new InternetExplorerDriver()

For headless testing, you can still use the same setup; just configure browser options as usual.

Step 5: Integrate with Testing Frameworks (JUnit/TestNG)

In a typical test automation project, you’ll use WebDriverManager inside a @BeforeAll or @BeforeEach method. Example with JUnit 5:

How to Automate Browser Driver Management with WebDriverManager in Java
Source: www.baeldung.com
import static io.github.bonigarcia.wdm.WebDriverManager.*;

public class LoginTest {
    WebDriver driver;

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

    @Test
    void testLogin() {
        driver.get("https://my-app.com/login");
        // assertions
    }

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

WebDriverManager also integrates seamlessly with Selenium Grid and Docker. Use WebDriverManager.chromedriver().driverInDocker() to run tests in containers.

Step 6: Take Advantage of Advanced Features

Beyond basic setup, WebDriverManager offers powerful capabilities:

  • Driver caching: Downloaded drivers are cached by default. You can control the cache with .clearCache() or set a custom cache path using .cacheDirectory().
  • Version pinning: Force a specific driver version: WebDriverManager.chromedriver().driverVersion("114.0.5735.90").setup();
  • Remote execution via Docker: .driverInDocker() automatically pulls a Docker image with the browser and driver.
  • Logging control: Change log level with .useLogger(Logger.LOGGER) or silent mode with .quiet().

These features make WebDriverManager suitable for both local development and CI/CD pipelines where environment consistency is critical.

Tips and Best Practices

  • Always match library versions: Use the latest stable WebDriverManager version compatible with your Selenium version. Check Maven Central for updates.
  • Cache management: In CI environments, share a persistent cache (e.g., via Docker volumes) to avoid re-downloading drivers on every run.
  • Enable quiet mode: Use .quiet() to reduce console noise during test execution.
  • Combine with WebDriverManager.chromedriver().forceDownload() if you want to force re-download even if cached.
  • Avoid hardcoding driver paths entirely: Once you switch to WebDriverManager, remove all System.setProperty calls for drivers.
  • Test across browsers: Use parameterized tests with different WebDriverManager methods to verify cross-browser compatibility.
  • For Selenium Grid or cloud services (BrowserStack, Sauce Labs), WebDriverManager is not needed—the remote server handles driver management.

By adopting WebDriverManager, you eliminate a whole category of test flakiness caused by driver mismatches. Your setup becomes portable, maintainable, and ready for modern CI/CD workflows.