As an automation engineer we know that we need to use setProperty() method to set and let our WebDriver know the key and the path of our browser binary (executable file) due to lunch or handle some browsers such as Chrome, Firefox, Opera, PhantomJS, Microsoft Edge, or Internet Explorer. First we need to download the binary file and must be set as JVM properties as follows:
But it has number of disadvantages to link directly the binary file into our source code as we have to check manually when the new versions of the binaries are released. In addition, our browser version can be updated somehow then we may get compatible issue with the binary file (executable file). To resolved this type of issue, WebDriverManager comes to the picture. It’s an API for Selenium what comes in a JAR format. Means it’s a class library. It allows to automate the management of the binary drivers (e.g. chromedriver, geckodriver, etc.). In order to use WebDriverManager in a Maven project, all we need to add the following dependency in our pom.xml (Java 8 or upper required):
WebDriverManager actually gets our browser version and downloads the compatible browser binary by itself due to run our code without interruption.
Now, rather than setting the browser binaries, we just need to add a single line below and forget about the binary file issues:
driver = new ChromeDriver();
Simply adding a single line as
WebDriverManager.chromedriver().setup(); WebDriverManager does magic for us:
- It checks the version of the browser installed in our 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 (
More benefits of the WebDriverManagers:
- For each and every browser we can add setup the particular WebDriverManager instance as below:
Watch the video as an example to use WebDriverManager in our code:
2. We can also search for the particular binary version as well in the WebDriverManager as following way to code:
||Force to use version 2.26 of chromedriver|
||Force to use the 32-bit version of geckodriver|
||Force to use the cache version of operadriver|
Additional methods are exposed by WebDriverManager, namely:
getVersions(): This method allows to find out the list of of available binary versions for a given browser.
getBinaryPath(): This method allows to find out the path of the latest resolved binary.
getDownloadedVersion(): This method allows to find out the version of the latest resolved binary.
clearPreferences(): This methods allows to remove all Java preferences stored previously by WebDriverManager.
clearCache(): This methods allows to remove all binaries previously downloaded by WebDriverManager.