Many automated testing suites built on Selenium WebDriver rely on locally stored driver executable files to run tests in a web browser. This works fine, until your web browser updates itself automatically, and suddenly you find that your web browser’s version and your web browser’s driver version are out of sync, resulting in an error message like below (in this example I’m using Chrome version 83, and ChromeDriver version 81):
You know the drill... Now you need to go out and download the latest driver version, update your master branch, send out a message to your team members to update their browsers, update your CI machines' browsers (if you have them), etc.
I say enough! Time spent updating web browsers and driver files is time wasted (if you disagree, I might suggest some new hobbies). Let's solve this problem.
Using the WebDriverManager library to keep driver files updated
Web browser driver files are traditionally stored somewhere in the testing suite project code itself. Below is an example of a basic WebDriver factory class that uses driver files stored in the project's resources directory:
As mentioned above, this works fine until the version of the driver executable is no longer compatible with the version of the web browser being used. So how do we fix this?
The solution I've found utilizes the WebDriverManager library created by GitHub user bonigarcia. WebDriverManager will automatically select and download the correct driver file for the version of the web browser that tests are being executed on. Using the WebDriverManager library is straightforward. Start by adding the library to your project. My project uses Maven and I'll be using WebDriverManager version 4.0.0 (the latest at the time of writing), so I'm going to add the following dependency to my POM file:
Next, I'm going to modify my WebDriverFactory.java class methods to use WebDriverManager to handle my driver files. Below is an example with the "old way" intentionally commented out. Notice that I am no longer referencing any locally stored driver files in my code:
That's all there is to it. WebDriverManager will check the version of your web browser, and then download and use the correct driver file automatically.
If you want to pass option arguments to the browsers, you can do so as you normally would:
Additional note: I recently came across a problem where WebDriverManager was not selecting the correct driver version on some Jenkins VMs (WebDriverManager was working fine on my local machine). I don't know what caused this, but clearing the WebDriverManager resolution cache before launching the browser fixed the issue for me:
Stay tuned for more automation tips and tricks in our Technology Corner!
We love sharing best practices with the test automation community. Check out my previous post on how to view multiple JUnit Assertion Errors in a single test execution.
Jared Hoernemann is a Senior Automation Engineer specializing in designing and coding test automation scripts and frameworks from the ground up, as well as mentoring other developers. Jared designs, implements, and executes automated tests for web browsers, APIs of all kinds, and databases.
Have a QA question?
Our team would love to help!