DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

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.

Arunkumar Velusamy user avatar by
Arunkumar Velusamy
·
Oct. 23, 18 · Tutorial
Like (4)
Save
Tweet
Share
32.44K Views

Join the DZone community and get the full member experience.

Join For Free

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

Driver (software)

Published at DZone with permission of Arunkumar Velusamy. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • NEXT.JS 13: Be Dynamic Without Limits
  • When AI Strengthens Good Old Chatbots: A Brief History of Conversational AI
  • Playwright vs. Cypress: The King Is Dead, Long Live the King?
  • Easy Smart Contract Debugging With Truffle’s Console.log

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: