How To Use WebDriverManager In Selenium

Cagri Ataseven

Posted On: November 2, 2022

view count320776 Views

Read time15 Min Read

Selenium WebDriver is still the most popular and widely used UI testing framework. Recently some test frameworks, such as Cypress, Playwright, etc., have entered the competition with Selenium.But Selenium came out with version 4, renewed and equipped with many new features, and it retook the leadership chair in the testing world.

So, is Selenium WebDriver very practical to use? Unfortunately, I can’t say yes to this question as a Selenium user. Today, you still need to download third-party browser drivers (or WebDrivers) and make various configurations in the framework to use Selenium WebDriver alone. Of course, you should remember to keep the WebDrivers up to date.

Isn’t the manual management (i.e., download, setup, and maintenance) of these drivers in a framework prepared for automated testing a bit annoying? Yes, it is. The good news is that there is an excellent solution to this problem: Boni García’s WebDriverManager.

In this blog on WebDriverManager in Selenium, I will examine how WebDriverManager offers a best practice using Selenium WebDriver.

Selenium WebDriver Architecture

Before you start using Selenium WebDriver, it will be helpful for you to understand how it works to solve the problems you may encounter in the future. In this section, I will summarize the Selenium WebDriver architecture.

Selenium WebDriver is a library that enables interaction between browsers and browser drivers. It is often used to create automated tests for web applications. This architecture of Selenium WebDriver consists of four interfaces: the Selenium Client Library/Language Bindings, JSON Wire Protocol (only Selenium 3), Browser Drivers, and Browsers.

Selenium WebDriver Architecture

Selenium Client Library/Language Bindings

One of the biggest advantages of Selenium WebDriver is that it supports many programming languages, such as Java, C#, Python, Ruby, etc.

Here Selenium Client Library offers flexibility in programming language selection for test automation.

JSON Wire Protocol and W3C WebDriver Protocol

The JSON Wire Protocol is a REST API that allows all communication between the browser and the test code. In other words, JSON Wire Protocol transfers information from the client to the server by sending HTTP Requests and receiving HTTP Responses.

W3C WebDriver Protocol

However, with Selenium 4, the JSON Wire protocol is no longer supported. The WebDriver W3C protocol, which has received the endorsement of W3C Protocol (World Wide Web Consortium), started to be used with Selenium 4 instead of the JSON Wire Protocol. It means there is direct communication between the Browser Drivers and Selenium Client/Language Bindings. It also offers the following advantages:

  • W3C WebDriver Protocol provides international W3C standards. Therefore, it is expected that the UI tests written with Selenium are expected to be much more stable (or less flaky).
  • It provides an updated Actions API that offers new actions like zoom, multi-touch actions, pressing two keys simultaneously, etc. Relative locators in Selenium 4 can be leveraged to locate WebElements in the vicinity of a particular element.

Browser Drivers

The browsers supported by Selenium contain a separate browser driver. Browser driver binaries (ChromeDriver for Chrome, GeckoDriver for Firefox, or MsedgeDriver for Edge) communicate with the respective browser by hiding the implementation logic of the browser’s functionality.

When a browser driver has received any command with the help of WebDriver W3C protocol, it will trigger the respective browser, and the response will go back in the form of an HTTP response.

Browsers

Selenium supports multiple browsers such as Chrome, Firefox, IE, Safari, etc. But remember! You need to make the browser and corresponding browser driver installation. Because Selenium is only able to run tests on the browsers if they are installed.

However, this is only applicable if you run tests on a local Selenium Grid. You would not face similar issues with a cloud Grid like LambdaTest since the onus of browser and browser driver update is not with you!

How to use third-party WebDrivers?

Since we have basic knowledge about how Selenium WebDriver works, we can talk about how to use third-party WebDrivers. As understood from the architecture I tried to explain above, to create web browser automation with Selenium, we need to place a binary file called driver between Selenium Client and the web browser on which the Selenium automation tests will be run. So how is it done? Let’s examine the use of 3rd party drivers in 4 steps:

3rd party drivers in 4 steps

Before downloading the third-party driver, you need to ensure that the browser driver is in sync with the browser version on which the tests would be performed. For instance, You can find the Chrome version on the computer where you ran your tests at chrome://settings/help.

Chrome version

Then you have to download ChromeDriver, which is in sync with the version of the Chrome browser.

ChromeDriver

If the versions are not in sync, you will encounter a failure message, as shown below. On the other hand, you would not get such issues when running tests on an online Selenium Grid like LambdaTest.

Cloud testing platforms like LambdaTest allow you to run Selenium automation tests over an online browser farm of 3000+ browsers and operating systems. You can run tests in parallel, accelerate the release cycle, and cut down test execution by multiple folds.

Also, subscribe to LambdaTest YouTube Channel and get detailed tutorials around Selenium automation, Cypress automation, Appium automation, and more.

For this reason, we need to find the correct driver version for a specific browser release.

Drivers are platform-specific binary files. It means we must identify the driver type according to the operating system. For example, if we want to use Chrome, we need to download ChromeDriver by considering the operating system (Windows, Linux, or macOS) and the architecture (typically 32 or 64 bits).

Index of chrome

After finding and downloading the proper driver binary file, we can proceed to the setup phase. Here we should ask a question: How will our Selenium tests find and use this binary driver file? In Java, we use Java system properties to export the driver path as follows:

Let’s show the ChromeDriver implementation as an example. Firstly we need to set the path of the ChromeDriver as follows:

After path setting, we create an instance of the ChromeDriver.

And we are ready to open the website with the “get()” method:

And so the binary driver file that we downloaded and completed the configuration will open the web page we want by interacting with the Chrome browser on the machine on which we run our tests.

Ecommerce playground

Here, I would like to remind you that the problem you will most likely encounter in the version control (Git, SVN, etc.) is that the driver is not executable if you use the binary driver file for Linux:

To solve that problem, you need to import the class java.io.File and use the following two lines of code:

The last step of the third-party driver usage process is maintenance. You’re probably questioning why we need maintenance. The answer is simple but annoying: Modern browsers automatically upgrade themselves. It means the compatibility between driver and browser is not warranted in the long term. That’s why you should do maintenance periodically.

As I said above, the manual maintenance effort we give for automated tests is laborious. Also, this is an obstacle to universality (Independence from the environment, browser version, operating system, etc.), which is one of the most important principles of test automation.

WebDriverManager from Bonigarcia is the solution to the problems mentioned above. Let’s dive deep into the integral aspects of WebDriverManager in Selenium.

What is Bonigarcia WebDriverManager?

Dr. Boni Garcia brought a great solution to the problem I mentioned above by creating the first version of the WebDriverManager library in 2015. So what is WebDriverManager in Selenium?

WebDriverManager is an open-source Java library that automatically performs the four steps (find, download, setup, and maintenance) mentioned above for the drivers required for Selenium tests.

Here are some salient benefits of WebDriverManager in Selenium:

  • WebDriverManager automates the management of WebDriver binaries, thereby avoiding installing any device binaries manually.
  • WebDriverManager checks the version of the browser installed on your machine and downloads the proper driver binaries into the local cache (~/.cache/selenium by default) if not already present.
  • WebDriverManager matches the version of the drivers. If unknown, it uses the latest version of the driver
  • WebDriverManager offers cross browser testing without the hassle of installing and maintaining different browser driver binaries.
  • WebDriverManager can create browsers in Docker containers

The Resolution Algorithm of WebDriverManager

Before we start using WebDriverManager in Selenium, let’s look at how it works. How does it find and download the proper versions of browsers? Let’s dive deep into it.

 Algorithm of WebDriverManager

WebDriverManager executes a resolution algorithm to automate the process given above for the appropriate driver binary.

Here are the brief steps of the resolution algorithm:

Find the proper driver version

WebDriverManager tries to find the browser version using the knowledge database called commands database. This database consists of shell commands used to discover a browser’s version in the different operating systems.

Here are the commands for the Linux environment:

Also, with the help of another knowledge database called versions database, WebDriverManager in Selenium maps the browser releases with the known proper driver versions.

Download and set up the driver

After the respective driver version is found, WebDriverManager downloads this driver to a local cache (~/.cache/selenium by default). Then it sets the downloaded driver path using Java system properties corresponding to browsers:

Maintenance

As mentioned earlier, the most problematic aspect of using third-party driver binaries is maintenance, i.e., finding the appropriate driver version for the browser we use for certain periods and downloading them repeatedly. So how does WebDriverManager in Selenium handle this process automatically?

WebDriverManager has a resolution cache called resolution.properties. This cache stores the match between the downloaded driver and browser versions. Also, this match is valid for 1 hour for browsers and considered correct for 1 day for drivers.

Long story short, WebDriverManager does not try to find a new browser and driver version in every test run, so using the current driver and browser in subsequent test runs improves performance.

How to set up WebDriverManager in Selenium?

Installing WebDriverManager in Selenium is also very simple. If you are using a Maven project, then you need to go to mvnrepository and find the WebDriverManager dependency:

WebDriverManager in Selenium

Simply add the following dependency to your pom.xml file:

Don’t forget to include the test scope, because WebDriverManager in Selenium is usually used in the testing part of your project.

If you are using a Gradle project, you can add WebDriverManager to your project as shown below:

Driver Management with WebDriverManager

After the simple setup above, WebDriverManager is ready to use to manage your browsers. First, let’s examine the components WebDriverManager uses for browser management, then see browser management in practice by making an example.

WebDriverManager library provides a class called WebDriverManager:

This class consists of some static methods as below to create managers for driver management. To manage the driver, select a given manager in the WebDriverManager class to manage the driver and invoke the method setup(). Let’s say you want to work with Chrome driver; then you can implement it as shown below:

WebDriverManager in Selenium offers a set of managers not only for Chrome but also for various browsers:

After version 5, WebDriverManager also started to provide a manager for Safari called safaridriver().

Now let’s see the practical usage of WebDriverManager in Selenium. The following example shows a test case using TestNG, Selenium WebDriver, and WebDriverManager for the Chrome driver.

Code Walkthrough

If you are going to work on the Chrome browser with Selenium WebDriver, you need to import the WebDriver interface and the ChromeDriver class from Selenium as follows:

To use WebDriverManager in Selenium, the WebDriverManager class must be imported.

Since we want to run WebDriverManager on the Chrome browser, we use the static method chromedriver() by invoking the setup() method, and we also initialize the WebDriver object for the Chrome driver (driver = new ChromeDriver();)

By using @BeforeTest annotation in TestNG, we ensure that this setup process happens automatically before each test.

Using driver.get(), the browser is opened to go to any website. Selenium interacts with the browser using the Chrome driver determined by the WebDriverManager resolution algorithm.

Using driver.quit(), we reset the driver object that we initialized, and we do this after each test with the help of @AfterTest annotation.

How to use WebDriverManager on Remote Grid?

The remoteAddress() method specifies the remote URL in the RemoteWebDriver instance, typically when using a Selenium Server or a cloud provider such as LambdaTest. In the example below, let’s examine how the WebDriverManager in Selenium works on the Remote Grid offered by LambdaTest.

Before running the tests, please set the environment variables LT_USERNAME & LT_ACCESS_KEY from the terminal. The account details are available on your LambdaTest Profile page.

For macOS
export LT_USERNAME=LT_USERNAME
export LT_ACCESS_KEY=LT_ACCESS_KEY

For Linux
export LT_USERNAME=LT_USERNAME
export LT_ACCESS_KEY=LT_ACCESS_KEY

For Windows
set LT_USERNAME=LT_USERNAME
set LT_ACCESS_KEY=LT_ACCESS_KEY

Execution

Navigate to the LambdaTest Automation Dashboard to look at the execution results.

LambdaTest Automation Dashboard

You can also leverage the New Analytics Dashboard, which allows you to create custom dashboards and widget variants for test analytics. You can build custom views with various widgets and get the desired insights quickly by creating dashboards.further You can find elusive bugs with a visual testing tool Selenium.

Conclusion

In this blog on WebDriverManager in Selenium, we entered the world of WebDriverManager by examining how it solves an important problem in using Selenium WebDriver, how it works with an algorithm in the background, and how it is implemented in practice.

Of course, this is only the beginning of our journey. After version 5, WebDriverManager in Selenium has been enriched with many new great features, such as Browser in Docker and Browser Monitoring. Let’s discuss these topics in detail in our next blog.

Frequently Asked Questions (FAQs)

What is WebDriverManager Selenium?

WebDriverManager is a powerful Selenium WebDriver management library, which allows you to automatically manage the drivers required by WebDriver in a fully automated manner.

Why do we use WebDriverManager?

WebDriverManager will enable you to easily automate finding and downloading the correct web driver for your browser, whether Chrome, Firefox, or IE.

Author Profile Author Profile Author Profile

Author’s Profile

Cagri Ataseven

Cagri Ataseven is a Software Test Automation Engineer, highly experienced with UI Testing, Rest API Testing, and Database validation. So far he has taken part in many test automation projects and has undertaken the responsibility of creating the Cucumber BDD test framework from scratch with Selenium and Java. Also, he has a Ph.D. in mathematics.

Blogs: 1



linkedintwitter

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free