Time to say ‘Good Bye’ to traditional way of instantiating browsers in Selenium?
We know that to execute Selenium automation scripts on browsers like chrome or firefox, we must download the binary files of these drivers like chromedriver and geckodriver, etc. After this, we need to set the path to these binaries in the automation script or add the classpath location.
System.setProperty("webdriver.chrome.driver", "/absolute/path/to/binary/chromedriver");System.setProperty("webdriver.gecko.driver", "/absolute/path/to/binary/geckodriver");
Issues with traditional approach
- If the binary file and browser version mismatch causes error
- Each time we have to upgrade binary according to browser versions
- File path to be provided and it consume space in the project
Bonigarcia WebDriverManager
A library and 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 (not done when using WebDriverManager from the CLI or as a Server).
How to instantiate a browser using WebDriverManager in Selenium?
WebDriverManager resolves the driver binaries for the browsers Chrome, Firefox, Edge, Opera, PhantomJS, Internet Explorer, and Chromium. 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.edgedriver().setup();
WebDriverManager.operadriver().setup();
WebDriverManager.phantomjs().setup();
WebDriverManager.iedriver().setup();
WebDriverManager.chromiumdriver().setup();
When we setup WebDriverManager in Project. By default, it downloads the latest version for a specified driver executable/s. Using WebDriverManager, we can also setup driver executable/s for a specific browser version.
//Code Snippet for Chrome Browser
WebDriverManager.chromedriver().version("2.40")

Maven dependency:
Here is the maven dependency for WebdriverManager.

https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager
All right! Its time to move on with Webdriver Manager.!