Manage driver executables using webdrivermanager

In order to run our selenium webdriver automation scripts on chrome/firefox browsers, we have to download the binary/.exe files like Chromedriver.exe and geckodriver.exe.

And also we need to set the path of these files in our script like below or its location should be added to the classpath. Earlier we have seen examples to launch browsers like Chrome, Firefox and Edge browser

For chrome browser :-

System.setProperty("webdriver.chrome.driver", "/absolute/path/to/binary/chromedriver");

For firefox browser :-

System.setProperty("webdriver.gecko.driver", "/absolute/path/to/binary/geckodriver");

If the path is not defined or if we provide the wrong path, you will see an exception like below :-

Error: The path to the driver executable must be set by the webdriver.chrome.driver system property

Manually downloading and managing these drivers for each operating systems is very painful. We also have to check when new versions of the binaries are released / new browsers versions are released. We should check the compatibility for all the executables and add it.

Have you ever thought of downloading all the driver executables automatically ???

Yes. WebDriverManager by Boni Garcia does this. Webdrivermanager helps to download binanries/executables in an automated way. It supports browsers such as Chrome, Firefox, Opera, PhantomJS, Microsoft Edge, or Internet Explorer.

We just need to add its dependency through Maven or Gradle to download all the necessary drivers. It will download ONLY if they are not present on the WebDriverManager cache (~/.m2/repository/webdriver by default).

In Maven project, we need to add the following dependency in pom.xml :-

<dependency>
    <groupId>io.github.bonigarcia</groupId>
    <artifactId>webdrivermanager</artifactId>
    <version>3.0.0</version>
    <scope>test</scope>
</dependency>

For Gradle project:

dependencies {
    testCompile("io.github.bonigarcia:webdrivermanager:3.0.0")
}

How to use Webdrivermanager to launch chrome/Firefox browser in Selenium WebDriver ?

let us look at below example to launch chrome browser :-

package com.tests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import io.github.bonigarcia.wdm.WebDriverManager;

public class ExampleTest {

	private WebDriver driver;
	
	@BeforeClass
	public void setUp() {
		WebDriverManager.chromedriver().setup();
		driver = new ChromeDriver();
	}
	
	@Test
	public void Seleniumtest1() {
		System.out.println("In test 1");
		driver.get("http://google.com");
		String expectedPageTitle = "Google";
		Assert.assertTrue(driver.getTitle().contains(expectedPageTitle), "Test Failed");
	}
	
	@Test
	public void Seleniumtest2() {
		System.out.println("In test 2");
	}
	
	@Test
	public void Seleniumtest3() {
		System.out.println("In test 3");
	}

	@AfterClass
	public void tearDown() {
		if(driver!=null)
			driver.quit();
	}
}

When we use webdrivermanager, By default, it tries to download the latest version of a given driver binary. if you want to use a specific version of driver, we can do that by using WebDriverManager.chromedriver().version("2.40").setup();

You can force WebDriverManager to download specific versions by changing the value of the variables in webdrivermanager.properties for wdm.chromeDriverVersion, wdm.operaDriverVersion, wdm.internetExplorerDriverVersion, or wdm.edgeDriverVersion to a concrete version.

For instance:

-Dwdm.chromeDriverVersion=2.32
-Dwdm.geckoDriverVersion=0.21.0
-Dwdm.internetExplorerVersion=3.6

If you require any further information, feel free to add your comment.

Thank You.

Selenium Tutorials: 

Comments

Have you tried on Linux? Will it work?
I did but it was not working.

what is the error message your are seeing ?

Was able to get the same example work on Mac.

Add new comment