Automatically download WebDriver Binaries

Kenil Fadia
3 min readDec 6, 2019

--

If you use Selenium WebDriver, you will know that in order to use some browsers such as Chrome, Firefox, Opera, PhantomJS, Microsoft Edge, or Internet Explorer, first you need to download a binary file which allows WebDriver to handle browsers. In Java, the path to this binary must be set as JVM properties, as follows:

System.setProperty("webdriver.chrome.driver", "/path/to/binary/chromedriver");
System.setProperty("webdriver.gecko.driver", "/path/to/binary/geckodriver");
System.setProperty("webdriver.opera.driver", "/path/to/binary/operadriver");
System.setProperty("phantomjs.binary.path", "/path/to/binary/phantomjs");
System.setProperty("webdriver.edge.driver", "C:/path/to/binary/msedgedriver.exe");
System.setProperty("webdriver.ie.driver", "C:/path/to/binary/IEDriverServer.exe");

And this is how you would traditionally set driver binaries in your code base:

@Test
public void WebDriverManagerTest() {

String os = System.getProperty("os.name");
String driverPath = "";
if (os.contains("Windows")) {
driverPath = file.createPath(prop.getProperty("pathToChromeDriverWindows"));
} else if (os.equalsIgnoreCase("linux")) {
driverPath = file.createPath(prop.getProperty("pathToChromeDriverLinux"));
} else {
driverPath = file.createPath(prop.getProperty("pathToChromeDriverMac"));
}
//Set the path to Chrome driver
System.setProperty("webdriver.chrome.driver", driverPath);
//Create driver object for Chrome
WebDriver driver = new ChromeDriver();
//Navigate to a URL
driver.get("http://www.google.com");
//Quit the Driver
driver.quit();
}

This is quite annoying since it forces you to link directly this binary file into your source code. 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.

What if we could download all the driver executables automatically ?? Have you heard of WebDriverManager?

Let WebDriverManager manage your browser’s Driver Executable
PC: https://www.atablogs.agiletestingalliance.org

WebDriverManager by Boni Garcia helps to download binaries/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 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 the Maven project, we need to add the following dependency in pom.xml:-

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

Now you could just replace your traditional code block to -

@Test
public void WebDriverManagerTest() {

WebDriverManager.chromedriver().setup();
//Create driver object for Chrome
WebDriver driver = new ChromeDriver();
//Navigate to a URL
driver.get("http://www.google.com");
//Quit the Driver
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();

WebDriverManager does magic for you:

  • It checks the version of the browser installed in your machine (e.g. Chrome, Firefox).
  • It checks the version of the driver (e.g. chromedriver, geckodriver). If unknown, it uses the latest version of the driver.
  • It downloads the WebDriver binary if it is not present on the WebDriverManager cache (~/.m2/repository/webdriver by default).
  • It exports the proper WebDriver Java environment variables required by Selenium.

WebDriverManager resolves the driver binaries for the browsers Chrome, Firefox, Opera, PhantomJS, Microsoft Edge, and Internet Explorer. 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.operadriver().setup();
WebDriverManager.phantomjs().setup();
WebDriverManager.edgedriver().setup();
WebDriverManager.iedriver().setup();

Isn’t this cool? If you like this post, share it with more people by recommending it. Keep Learning, Keep Automating!

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Kenil Fadia
Kenil Fadia

Written by Kenil Fadia

Lead Software Test Engineer at Media.net | Ex Senior Development Engineer in Test at upGrad | Ex Module Lead QA at Zeus Learning

No responses yet

Write a response