WebDriverManager – Get Rid Of Compatability Issues Between Browser And Drivers

Introduction

While automating a Web-based application using Selenium WebDriver, we must have faced the below issues:-

  1. Unable to create session due to outdated browser driver executable.
  2. maintaining the same browser version across the team so that your framework can run scripts.
  3. compatibility issues between currently installed browser and available driver executable in the framework
  4. managing browsers on the different operating system

Earlier I remember that we used to disable auto-update of browser to make our scripts stable. Nowadays browsers keep updating and we can not restrict our application to run on a specific version of the browser.

All these issues we can solve using a library called WebDriverManager and focus more on covering functionalities rather than spending much time on these browser’s compatibilities issues.

Did you know that I have started a YouTube channel as well and I need your support to make it successful. Please do watch content then comment, like, share, and obviously subscribe.

What is WebDriverManager?

In Selenium WebDriver, you might aware that you need to explicitly download the compatible browser’s driver executable binary file and set the JVM path or properties so that selenium commands can be executed. These browser driver executables work as a bridge between your selenium commands/codes and browser. Below lines of code will explain more about the above statement.

System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
System.setProperty("webdriver.gecko.driver", "/path/to/geckodriver");

We can auto manage the download of browser executable and setting up of path using WebDriverManager.

How to use it?

WebDriverManager is a Java library. So you can either download it or use it as a dependency in a Maven or Gradle project. I will use the latest maven dependency of WebDriverManager as below:-



    io.github.bonigarcia
    webdrivermanager
    4.4.3

After adding the above dependency, you just need to replace System.setProperty() line as below.

If you want to launch chrome browser then use the below line of code –

WebDriverManager.chromedriver().setup();

A similar line of code for other browsers.

The above code from WebDriverManager does magic for you:-

  1. It checks the version of the browser installed in your machine (e.g. Chrome, Firefox).
  2. It matches the version of the driver (e.g. chromedrivergeckodriver). If unknown, it uses the latest version of the driver.
  3. It downloads the driver if it is not present on the WebDriverManager cache (~/.cache/selenium by default).
  4. It exports the proper WebDriver Java environment variables required by Selenium.

In short, it downloads the compatible browser driver executable as per the currently installed browser on your system and stores it on your local itself and an environment path is set up.

Example Code

package mse_webdrivermanager.autoManageExecutables;

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

import io.github.bonigarcia.wdm.WebDriverManager;

public class LaunchChromeUsingWDM {

	public static void main(String[] args) {
		// It will download compatible chrome executable for you
		WebDriverManager.chromedriver().setup();
		System.out.println("Browser executable path is set as :- "+System.getProperty("webdriver.chrome.driver"));
		WebDriver driver = new ChromeDriver();
		driver.get("https://www.google.com/");
		System.out.println("Title is "+driver.getTitle());
		driver.quit();		
	}
}

Output

Browser executable path is set as :- C:\Users\amomahaj\.cache\selenium\chromedriver\win32\91.0.4472.101\chromedriver.exe
Starting ChromeDriver 91.0.4472.101 (af52a90bf87030dd1523486a1cd3ae25c5d76c9b-refs/branch-heads/4472@{#1462}) on port 18868
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Jun 28, 2021 5:40:57 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Jun 28, 2021 5:40:58 PM org.openqa.selenium.devtools.CdpVersionFinder findNearestMatch
INFO: Found exact CDP implementation for version 91
Title is Google

Observer above output. It has download chromedriver 91.0.4472.101 after checking the installed version of chrome on my system. The below images will change in the future based on newer versions. This is the magic done by WebDriverManager.

It will download only if the compatible browser executable is not available in the cache. You can see in the above output how well organized it keeps the chromedrivers.

You can clone the git repo consisting above examples from here.

You can subscribe to my YouTube channel RetargetCommon to learn from video tutorials.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning

Find all Selenium related posts here, all API manual and automation related posts here, and find frequently asked Java Programs here.

1 thought on “WebDriverManager – Get Rid Of Compatability Issues Between Browser And Drivers

  1. Does webdriver manager support chrome updates with relaunch? Some version need relaunch of chrome with latest chrome driver updated.

Leave a Reply

Your email address will not be published. Required fields are marked *