Introduction to Drivers & Web Driver Manager in Selenium Web Driver

Monil Joshi
6 min readSep 26, 2021

--

In the previous blog we saw Introduction to Selenium Web Driver and hierarchy of related classes and interfaces. You can read those blogs

Introduction to Selenium Web Driver

WebDriver interface & hierarchy of related interfaces and Classes

Why we write WebDriver driver = new ChromeDriver(); in Selenium?

In this articles we are deep digging in to Drivers.

Browser drivers helps selenium scripts to execute on respective browsers. Each Browser vendors like Firefox, Chrome is having their own WebDriver implementation, so they are tightly coupled to the browsers giving a better testing experience.

Modern Browsers such as Edge, Safari are introduced with the WebDrivers shipped by their vendors.

How does web driver exactly works?

If you check architecture diagram of Selenium Web Driver

Selenium Architechrure

WebDriver could not make direct call to the browser so it uses its native support for automation. It uses browser driver for this communication

Browser driver works as a bridge that interprets the selenium commands to execute it on the browsers.

So When any Automated Script is executed, for every Selenium command, HTTP Request has been created. This http request is sent in JSON wire protocol to the browser driver. and then sent to browser through driver. The browser driver uses a HTTP Server for getting HTTP Requests. This HTTP Server determines the steps needed for implementation of Selenium command.

Driver will translate code written in any supported language in to browser understandable commands to perform the desired operations. Once commands get executed we can see desired actions are getting performed on browser. Browser will then send response to browser drivers. The execution status which is run on browser is sent back to HTTP Server

There are separate browser drivers for each browser, for example, for Chrome we have ChromeDriver like wise we do have EdgeDriver, FirefoxDriver, InternetExplorerDriver, OperaDriver, SafariDriver, etc

While using driver with any browser make sure that driver version should be compatible with browser version

For example I am having Chrome browser version 94 and I have downloaded Chrome Driver version 92. My execution will fail with this error message

So make sure that browser driver should be compatible with browser

Working with Firefox browser:

Till Firefox version 47 it has full support from Selenium. You write and execute your script on Firefox with Selenium web driver version 2.53 with below code

WebDriver driver = new FirefoxDriver();
driver.get("https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/WebDriver.html");

But selenium web driver version 2.53 is not compatible with Mozilla FireFox for the version > 47. After Firefox version 47, FireFox is provided with GeckoDriver. GeckoDriver is a proxy for using W3C WebDriver-compatible clients to interact with gecko-based browsers i.e. Mozilla FireFox.

Also after Selenium 3 you must use gecko driver to initialised driver instance for Mozilla Firefox browser

So your code will be like this

WebDriver driver = new FirefoxDriver();System.setProperty("webdriver.gecko.driver",<GeckoDriver_Path>/geckoDriver);driver.get("https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/WebDriver.html");

Working with IE browser:

To execute your test o IE browser, there are some pre-requisite you must be full-filled

  1. Zoom level should be 100%
    Click on Settings and Select Zoom level.
  2. Enable/ Disable Protected mode
    1. Go to Internet options > Security
    2. Inside security Enable protected mode should be check/uncheck for all four zone.(Disable them is recommended)
  3. Driver Download
    1. It is recommended to use 32 bit driver for IE even for 64 bit system.
    2. You can download driver from here.

Working with Safari browser:

  1. Enable Develop tab
    1. Go to Preferences
    2. Click on Advance Tab
    3. Check the checkbox for “Show develop menu in menu bar”
    It will enable Develop menu in safari menu bar
  2. Allow remote automation
    1. Click on Develop menu in menu bar
    2. Click and make sure “Allow Remote Automation” is ticked to execute your test on Safari

Introduction to web driver manger

What are the problem with the above approach.

With above usage of driver code you can still execute our script. But you might face below problems

Manual Work: To use browser driver you have to perform some manual steps

  1. You have to download browser driver, unzip the downloaded zip file
  2. You have to provide path to the driver in your script. If we failed to give correct path we will get below exception
IlligalStateException

What are pain points with this approach,

  1. If you want to execute same automation script on different computer you have to again download driver file and give the respective local directory path
  2. If you are executing on different machines like Mac, linux and windows then you have to download different files (with respective extensions) and store it in project repository. Also you have to perform manual work mentioned in the point number 1.
  3. These days browsers are updating so fast so our test will fail in next 3–4 days due to incompatibility of driver and browser
  4. We have to keep checking version on each browser on each of our target machines and download that much number of compatible drivers and perform point number 1 and 2.
  5. If you want to execute your automation through CI/CD pipelines then this will be bottleneck every time
  6. SO this approach is hurdle for maintainability and reliability of the framework

What is the solution for this ?

To resolve this maintenance problem we can take help of an open source library called WebDriverManager.

According to git page, WebDriverManager is an open-source Java library that carries out the management (i.e., download, setup, and maintenance) of the drivers required by Selenium WebDriver (e.g., chromedriver, geckodriver, msedgedriver, etc.) in a fully automated manner. In addition, WebDriverManager provides other relevant features, such as the capability to discover browsers installed in the local system, building WebDriver objects (such as ChromeDriver, FirefoxDriver, EdgeDriver, etc.), and running browsers in Docker containers seamlessly.

You can read full ReadMe.md here

The primary use of WebDriverManager is the automation of driver management. For using this feature, you need to select a given manager in the WebDriverMager API (e.g., chromedriver() for Chrome) and invoke the method setup().

As it is library either you have to download jar or set dependency in your pom.xml (in case of maven project) or in build.gradle project (in case of gradle project.

Here is link for Java library. Once Jar gets updated

Now no need to add driver path. So that you can remove

System.setProperty("webdriver.chrome.driver",<chromeDriver_Path>/chromeDriver);

instead add below code in your script

WebDriverManager.chromedriver().setup();

While execution above line first check the version of the chrome browser installed on your machine. If it is unknown, it uses the latest version of the driver. It downloads WebDriver binary and store it in local cache for the first time

For next execution it will first check driver in local cache and use it for execution.

Advantage of doing It is saves time to download binary. In this case if your first execution took 20–22 seconds then consecutive executions will take only 5–6 seconds.

this will solve all of the problems mentioned above like different platform, different machine, different versions. It will check for compatible browser driver and download it in local cache

WebDriverManager is available for all of the browsers Chrome, Firefox, Edge, Opera, PhantomJS, Internet Explorer.

Here is code snippet you can use to manage different browser drivers

WebDriverManager.chromedriver().setup();
WebDriverManager.firefoxdriver().setup();
WebDriverManager.edgedriver().setup();
WebDriverManager.operadriver().setup();
WebDriverManager.phantomjs().setup();
WebDriverManager.iedriver().setup();

You can use a specific version of chrome instead of latest driver. But you should cross check compatibility of browser and driver. For example you want to execute your script on chrome version 67 to 69 you can check comaptible driver version here and update your code for webdriver manager as below

WebDriverManager.chromedriver().version(“2.41”).setup();

It will check your browser version is between 67–69 and download required browser driver and execute the test

After chrome browser version 73 they have stopped giving versions like 2.x and giving complete version number. In this case you can use below code snippet

WebDriverManager.chromedriver().driverVersion("85.0.4183.38").setup();

Here is complete code

WebDriver driver;
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.get("https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/WebDriver.html");
driver.quit();

Cheers!

--

--

Monil Joshi
Monil Joshi

Written by Monil Joshi

I am having an 8+ years of experience in software testing. Working with Web, Mobile and API technologies. Visit my GIT https://github.com/monilj

Responses (1)