WebDriverManager Documentation

repository·master·Indexed 25 days ago

https://github.com/bonigarcia/webdrivermanager

An open-source Java library that automates the management of Selenium WebDriver executables, including downloading, setup, and maintenance. It supports direct WebDriver instantiation via create(), running browsers within Docker containers with VNC and recording capabilities, and providing a standalone CLI and Server for driver resolution. Features include a Java Agent for automatic driver resolution, BrowserWatcher for monitoring, and support for various browsers including Chrome, Firefox, Edge, Opera, and Internet Explorer.

Tokens
3.4K
Snippets
8
Records
22
Agent score
33%

What's inside WebDriverManager

  1. Install WebDriverManager via Maven or Gradle

    master

    WebDriverManager is primarily used as a Java dependency. It is recommended to use a build tool like Maven or Gradle to manage the dependency, typically within the test scope.

    ### Maven
    ```xml
    <dependency>
        <groupId>io.github.bonigarcia</groupId>
        <artifactId>webdrivermanager</artifactId>
        <version>{project-version}</version>
        <scope>test</scope>
    </dependency>

    Gradle

    dependencies {
        testImplementation("io.github.bonigarcia:webdrivermanager:{project-version}")
    }
  2. Inspect Dockerized Browsers via VNC and Video Recording

    master

    To visualize tests running in Docker, you can enable VNC or Video Recording:

    • VNC: Use enableVnc() to start a VNC server in the container. Access the session via the URL provided by getDockerNoVncUrl().
    • Video Recording: Use enableRecording() to record the session using FFmpeg. The MP4 file is saved in the project root by default. Use getDockerRecordingPath() to find the file.
  3. Automate driver management with setup()

    master

    Use WebDriverManager to automatically download, setup, and maintain Selenium drivers (like chromedriver, geckodriver, or msedgedriver). To use this, select a specific manager via the API (e.g., chromedriver()) and call the setup() method. This prepares the driver executable on your system so you can instantiate the standard Selenium WebDriver object manually.

    import org.junit.jupiter.api.BeforeAll;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import io.github.bonigarcia.wdm.WebDriverManager;
    
    class ChromeTest {
    
        WebDriver driver;
    
        @BeforeAll
        static void setupAll() {
            WebDriverManager.chromedriver().setup();
        }
    
        @BeforeEach
        void setup() {
            driver = new ChromeDriver();
        }
    
        @AfterEach
        void teardown() {
            driver.quit();
        }
    
        @Test
        void test() {
            // Your test logic here
        }
    }
  4. Run browsers in Docker containers

    master

    WebDriverManager 5+ allows you to run browsers inside Docker containers. This requires a running Docker Engine on your machine. By combining browserInDocker() with create(), WebDriverManager will pull the necessary image from Docker Hub, start the container, and return the WebDriver object. You can also enable remote access via noVNC using enableVnc() and session recording using enableRecording().

    import org.junit.jupiter.api.AfterEach;
    import org.junit.jupiter.api.BeforeEach;
    import org.junit.jupiter.api.Test;
    import org.openqa.selenium.WebDriver;
    import io.github.bonigarcia.wdm.WebDriverManager;
    
    class DockerChromeVncTest {
    
        WebDriver driver;
    
        WebDriverManager wdm = WebDriverManager.chromedriver().browserInDocker()
                .enableVnc().enableRecording();
    
        @BeforeEach
        void setup() {
            driver = wdm.create();
        }
    
        @AfterEach
        void teardown() {
            wdm.quit();
        }
    
        @Test
        void test() {
            // Your test logic here
        }
    }
  5. Use WebDriverManager as a Java Agent

    master

    The WebDriverManager Agent uses the JVM instrumentation API to intercept calls and resolve drivers automatically before Selenium WebDriver objects (like ChromeDriver) are instantiated. This removes the need for explicit WebDriverManager.chromedriver().setup() calls in your code.

    To configure the agent, specify the path to the WebDriverManager fat-JAR using the JVM flag -javaagent.

    -javaagent:/path/to/webdrivermanager-{project-version}-fat.jar
  6. Configure WebDriverManager via Java API, System Properties, or Environment Variables

    master

    WebDriverManager supports three configuration methods. The preference order (if overlapping) is:

    1. Environment Variables (e.g., WDM_CACHEPATH)
    2. Java System Properties (e.g., -Dwdm.cachePath)
    3. Java API (e.g., .cachePath("..."))

    To convert a configuration key to an environment variable: convert to uppercase and replace . with _.

  7. Troubleshoot: HTTP 403 Forbidden (GitHub Rate Limits)

    master

    If you encounter an HTTP 403 error when requesting drivers hosted on GitHub (like geckodriver or operadriver), it is likely due to GitHub's traffic rate limits.

    Solutions:

    1. Use a GitHub Personal Access Token: Configure a token to make authenticated requests. Use the gitHubToken(String) API method or the GITHUB_TOKEN environment variable.
    2. WebDriverManager 5.3.0+: As of version 5.3.0, this issue is largely mitigated because WebDriverManager uses a GitHub Actions mirror for API responses instead of querying GitHub directly.
  8. Troubleshoot: Chromedriver 115+ Support

    master

    Starting with chromedriver 115, releases are no longer published via traditional repositories but via Chrome for Testing (CfT) JSON endpoints. Older versions of WebDriverManager will fail with a 404 error.

    Solution:

    1. Upgrade: Bump WebDriverManager to the latest version (5.4+ implements CfT support).
    2. Clear Cache: To ensure old, incorrect versions are not used from the resolution cache, run:
    WebDriverManager.chromedriver().clearDriverCache().setup();