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

WebDriverManager: A Hassle-Free Browser Binary Manager for Selenium

DZone's Guide to

WebDriverManager: A Hassle-Free Browser Binary Manager for Selenium

Learn how to use WebDriverManager to manage binary paths in Selenium.

· DevOps Zone ·
Free Resource

Do you need to strengthen the security of the mobile apps you build? Discover more than 50 secure mobile development coding practices to make your apps more secure.

As an automation engineer, I believe you always have to set the path for the browser binary. You 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 addition, the absolute path to this binary must be set as JVM properties, as follows:

System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
System.setProperty("webdriver.gecko.driver", "path/to/geckodriver");
System.setProperty("webdriver.ie.driver", "path/to/IEDriverServer.exe");

It's annoying every time you need to set the binary path. It's not just limited to this one; you also need to change the binaries frequently as the browser version or Selenium version changes. In one major long-term project, the client asked me why he needed to change the library. What if he didn't need to change the library and the script worked as-is even if he changed the browser versions? I was speechless, but later, doing R&D, I found a lifesaver solution. I implemented it, and bingo! I didn't need exe libraries anymore for the browsers! A small maven repository saved the day.

When working on a Maven project, you just need to add a dependency called WebDriverManager. This library will take care of everything your browser version and driver.

In order to use WebDriverManager in a Maven project, you need to add the following dependency in your pom.xml (Java 7 or upper required):

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

WebDriverManager actually gets your browser version and downloads the compatible browser binary by itself to make you run your code without interruption.

Now, rather than setting the browser binaries, you just need to add below line of code and you're done.

WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();

The line WebDriverManager.chromedriver().setup(); WebDriverManager does magic for you:

  • It checks for the latest version of the WebDriver binary.

  • It downloads the WebDriver binary if it's not present on your system.

  • It exports the required WebDriver Java environment variables needed by Selenium.

For each and every browser you can add setup the particular WebDriverManager instance.

  1. More benefits of the WebDriver: You can also search for the particular binary version as well in the WebDriverManager. By default, it'll download the latest binary version.
  2. You can also set the path for the downloaded binary files if in case future use.
  3. By default, WebDriverManager connects to the specific driver repository URL to find out what is the latest version of the binary. This can be avoided forcing to use the latest version from the local repository.
  4. By default, WebDriverManager finds out the latest version of the binary, and then it uses the cached version if exists. This option forces to download again the binary even if it has been previously cached.
  5. By default, WebDriverManager skips beta versions. With this method, WebDriverManager will download also beta versions.
  6. By default, WebDriverManager would try to use the proper binary for the platform running the test case (i.e. 32-bit or 64-bit). This behavior can be changed by forcing a given architecture: 32-bits (Architecture.x32) or 64-bits (Architecture.x64);

Now the struggle of checking compatible binaries ends. You can certainly enjoy  hassle-free automation and make the client happy.

Check out tips for blazing the way from agile to DevSecOps with security built into your mobile app toolchain.

Topics:
test automation ,selenium ,webdriver ,devops ,tutorial ,maven

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}