WebDriverManager is an API that allows users to automate the handling of driver executables like chromedriver.exe, geckodriver.exe etc required by Selenium WebDriver API.

Now let us see, how can we set path for driver executables for different browsers like Chrome, Firefox etc. in a traditional way.

In general, to automate web applications we need to download driver executables manually for required browsers. Once driver executables are downloaded into local system then we need place them in a specific path and set path for driver executables that becomes very tedious task to maintain driver executables as and when browser version changes.

In below code, we can see how to set path for driver executables in a traditional way.

//For Chrome Browser
System.setProperty("webdriver.chrome.driver", "./Drivers/chromedriver.exe");

//For Internet Explorer Browser
System.setProperty("webdriver.ie.driver", "./Drivers/IEDriverServer.exe");

//For Firefox Browser
System.setProperty("webdriver.gecko.driver", "./Drivers/geckodriver.exe");

It’s a good practice to maintain driver executables under project directory as shown below.

Now let us see the disadvantages of following traditional approach over WebDriverManager to maintain driver executables and setting path for the same.

1. We need to download driver executables manually and set path for the same.

2. As and when browser version is updated, we need to download driver executable/s that can be matched with latest browser version.

3. We need to set exact path for driver executable/s else we get illegalStateException. [When we try to launch browser without setting correct path for driver executable/s]

Now the question is how can we overcome doing above manual stuff using WebDriverManager.

Pre-Condition:

Project should be Maven. In pom.xml file we need to add below dependency to have WebDriverManager in our Project.

<dependency>
	<groupId>io.github.bonigarcia</groupId>
	<artifactId>webdrivermanager</artifactId>
	<version>3.7.1</version>
</dependency>

Once WebDriverManager is added into pom.xml. Update Maven Project and ensure that WebDriverManager Jar is added into Project Build Path under Project Maven Dependencies folder as shown below.

 

Notice that simply adding WebDriverManager.chromedriver().setup(); 

WebDriverManager does magic for you:

  1. It checks the version of the browser installed in your machine (e.g. Chrome, Firefox).
  2. It checks the version of the driver (e.g. chromedrivergeckodriver). If unknown, it uses the latest version of the driver.
  3. It downloads the WebDriver binary if it is not present on the WebDriverManager cache (~/.m2/repository/webdriver by default).
  4. It exports the proper WebDriver Java environment variables required by Selenium (not done when using WebDriverManager from the CLI or as a Server).

WebDriverManager resolves the driver binaries for the browsers ChromeFirefoxEdgeOperaPhantomJSInternet Explorer, and Chromium. For that, it provides several drivers managers for these browsers. These drivers managers can be used as follows:

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

 

 

Now we are good to use WebDriverManager in Test Scripts.

//For Chrome Browser
WebDriverManager.chromedriver().setup();

//For Internet Explorer Browser
WebDriverManager.iedriver().setup();

//For Firefox Browser
WebDriverManager.firefoxdriver().setup();

Likewise we can setup driver executables for other browsers like edge, opera etc using WebDriverManager which launches a browser.

//Below is the sample code to launch Chrome Browser using WebDriverManager
public class WebDriverManagerChrome 
{
        @Test
	public void webDriverManagerChrome()
	{
		WebDriverManager.chromedriver().setup();
		WebDriver driver = new ChromeDriver();
		driver.manage().window().maximize();
		driver.manage().deleteAllCookies();
			
		driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
		driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
		
		driver.get("https://classic.crmpro.com/index.html");
         }
}

When we setup WebDriverManager in Project. By default, it downloads the latest version for a specified driver executable/s. Using WebDriverManager, we can also setup driver executable/s for a specific browser version.

//Code Snippet for Chrome Browser
WebDriverManager.chromedriver().version("2.40")

Now let us see the difference between WebDriverManager and Traditional way of setting up driver executables to launch browsers.

WebDriverManager.chromedriver().setup(); ==>> It will automatically downloads the Driver Executable/s.

System.setProperty(“webdriver.chrome.driver”, “./Drivers/chromedriver.exe”); This will setup driver executable for a specific browser based on the location that we have provided where we downloaded and placed driver executable/s.

Note: WebDriverManager is not part of Selenium WebDriver API.

Refer below Git Repository for WebDriverManager implementation with Maven Project.

https://github.com/PavanReddy77/SeleniumWebDriver_4

In this blog, we have seen setting up WebDriverManager for a Maven Project to work with Selenium WebDriver API.

 

Cheers!!

Naveen AutomationLabs

Blog Contributors:

Author:  Pavan K

Pavan, having good knowledge in Selenium Web UI and API Automation frameworks. Also, he is helping QA community with his knowledge along with Naveen AutomationLabs!!

LinkedIn

Reviewer: Naveen Khunteta 

https://www.linkedin.com/in/naveenkhunteta