Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

WebDriverManager: How to Manage Browser Drivers Easily and Efficiently

DZone 's Guide to

WebDriverManager: How to Manage Browser Drivers Easily and Efficiently

In this post, we check out a cool open source web dev project from the world of GitHub that makes managing browser drivers easier.

· Web Dev Zone ·
Free Resource

We all know that we need to have browser drivers, .exe files like chromedriver.exe and geckodriver.exe in case of windows environment or binary files like chromedriver and geckodriver in case of Linux distributions, which allows Selenium WebDriver to handle browsers in order to run our selenium webdriver automation scripts on Chrome and Firefox browsers (applicable for other browsers as well).

And, also, we need to set the path of these files in our script, like below, or we need to add the location to the classpath.

System.setProperty(“webdriver.chrome.driver”, “/path/to/binary/chromedriver”);
System.setProperty(“webdriver.gecko.driver”, “/path/to/binary/geckodriver”);

If the path is not defined or if the path provided is wrong, we will get an exception like below when running our tests.

Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases
at com.google.common.base.Preconditions.checkState(Preconditions.java:847)
at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:125)
at org.openqa.selenium.firefox.GeckoDriverService.access$100(GeckoDriverService.java:43)
at org.openqa.selenium.firefox.GeckoDriverService$Builder.findDefaultExecutable(GeckoDriverService.java:168)
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:346)
at org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:168)
at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:125)

WebDriverManager by Boni Garcia helps us to manage driver related settings with ease. WebDriverManager gets the browser version and downloads relevant binaries/executables in an automated way; This helps us to avoid all the manual steps that we previously had to do, related to browser driver setup, in order to run our tests.

It supports browsers such as Chrome, Firefox, Opera, PhantomJS, Microsoft Edge, or Internet Explorer. You can these specs and more on the project page.

All we have to do is to add its dependency through Maven or Gradle to download all the necessary drivers. Note that drivers will be downloaded only if the same version driver is not present on the WebDriverManager cache (~/.m2/repository/webdriver by default) by eliminating frequent downloads. You can update this default location using the properties file.

In the case of a 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>
</dependency>

Check the Maven page for more details about supported builder projects.

Sample Code:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import io.github.bonigarcia.wdm.WebDriverManager;

public class WebDriverManagerTest {
 public static void main(String[] args) {
  new WebDriverManagerTest().testDriverManagerChrome();
  new WebDriverManagerTest().testDriverManagerFirefox();
 }

 public void testDriverManagerChrome() {
  WebDriverManager.chromedriver().setup();
  WebDriver driver = new ChromeDriver();
  driver.get("http://www.google.com/");
  System.out.println(driver.getTitle());
  driver.quit();
 }

 public void testDriverManagerFirefox() {
  WebDriverManager.firefoxdriver().setup();
  WebDriver driver = new FirefoxDriver();
  driver.get("http://www.google.com/");
  System.out.println(driver.getTitle());
  driver.quit();
 }
}

Article source - AllSelenium.info

Topics:
selenium automation ,test automation ,web dev ,browser drivers ,open source

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}