Selenium Manager - how does it work?

What is a driver?

A driver is a component that enables communication between our automation script (written in any programming language) and the browser. It is a two-way communication which includes commands like clicking buttons, entering texts, selecting from dropdown box etc. And the browser responds accordingly.

A driver is specific to each browser and they are manager by their respective browser vendors (chromedriver, geckodriver, msedgedriver, etc). As we know, the browsers sends out new changes almost every other day now. So if we don't download the latest driver executable files accordingly, out code may fail. It is a tedious task to do it manually.

What is WebDriverManager?

In order to manage the driver binaries better, we could use the WebDriverManager API until Selenium 3. It is an external dependency created by Boni Garcia.

<!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
<dependency>
    <groupId>io.github.bonigarcia</groupId>
    <artifactId>webdrivermanager</artifactId>
    <version>5.6.2</version>
</dependency>

Along with the Selenium dependency(language specific binding), we need to add the WebDriverManager dependency to our POM.xml file. Every time we run our script, it will download the latest driver executable files automatically.

Let's try it out:

In the code below, I have added Selenium 3.141.59 version and WebDriverManager dependencies to my POM.xml file.

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>
      <!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
        <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>5.6.2</version>
        </dependency>
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class WebdriverManagerConcepts {
    public static void main(String[] args) {
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.google.com");
    }
}

When we run this, we will get the Java IllegalStateException as below, because the WebDriver doesn't have the path to the chromedriver.exe file.

Exception in thread "main" java.lang.IllegalStateException: 
The path to the driver executable must be set by the webdriver.chrome.driver system property; 
for more information, see https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver. 
The latest version can be downloaded from http://chromedriver.storage.googleapis.com/index.html

Now, we add just one line of code and try running it. It will launch the browser.

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

public class WebdriverManagerConcepts {
    public static void main(String[] args) {
        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();
        driver.get("https://google.com");
    }
}

What is Selenium Manager?

In Selenium 4, the WebDriverManager has been integrated with the Selenium package and is now known as the Selenium Manager. That means we do not need to add a separate dependency for it in our POM.xml file. It will download and manage the browser driver versions automatically. This is how our code will look if we add Selenium 4 dependency instead :

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

public class WebdriverManagerConcepts {
    public static void main(String[] args) {
        WebDriver driver = new ChromeDriver();
        driver.get("https://google.com");
    }
}

The traditional way of adding a driver exe file:

In this case, we need to download a specific version of driver executable file and specify the path using System class as below:

public class WebDriverTest {
    public static void main(String[] args) {        
        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver.exe");
        WebDriver driver = new ChromeDriver(); 
    }
}