Apr 20, 2016

Automatically download OS-specific WebDriver executable

When using the Selenium browser functional testing library, testing with some browsers requires using an operating system-specific executable in addition to the driver .jar file. For example, when running Selenium tests with Chrome you need to have the ChromeDriver executable for the operating system you are using. Internet Explorer also requires a similar executable to run tests with it.

Having to manually download and manage these drivers for each operating systems you run tests on is a pain. Each developer on your team has their own laptop, potentially running different operating systems. Plus your continuous integration environment may be on a different OS.

In the past I used some custom download code in each project I worked on to detect the OS and download the driver. But carrying that download code from project to project is also inconvenient. I’d planned for years to write a small library to do the same thing, but thankfully someone else recently did it already!

The WebDriverManager project by Boni Garcia automatically handles downloading and configuring the drivers for your operating system. And it’s very easy to use.

First, add the dependency in your project’s build file. In this example I’m using Gradle as my build system, so I add this to build.gradle:

dependencies {
    // For downloading browser-specific drivers that browsers like Chrome and IE require
    testCompile("io.github.bonigarcia:webdrivermanager:1.4.1") {
        exclude group: 'org.seleniumhq.selenium'
    }
}

Next, add code to download and configure the driver before the tests run. In this example I’m using Geb to write my tests, so in my GebConfig.groovy I add:

import io.github.bonigarcia.wdm.ChromeDriverManager
import org.openqa.selenium.chrome.ChromeDriver
 
environments {
    chrome {
        // Download and configure ChromeDriver using https://github.com/bonigarcia/webdrivermanager
        ChromeDriverManager.getInstance().setup()
 
        driver = { new ChromeDriver() }
    }
}

Now when I run my Geb tests in the ‘chrome’ environment, WebDriverManager will automatically download the ChromeDriver executable for my OS and set up the required system property to use it. It’s that easy!

For a full working example of using WebDriverManager with Geb, check out this example project on Github: https://github.com/craigatk/grails3-geb-example

Happy testing!

About the Author

Object Partners profile.

One thought on “Automatically download OS-specific WebDriver executable

  1. Javed says:

    great soloution. thanks Man.

  2. Rahul says:

    I am using the above mention concept, but for me getInstance() throwing an error saying: The method getInstance() is undefined for the type ChromeDriverManager.

  3. Jan says:

    Great work. Briliant solution, pleasure to use. We happily use it for three months. Thanks!

  4. Pirit says:

    Hi, but in my testing environment, sometimes, there is poor network. It often poped up the download failed error with this function.
    How to fix it?

  5. Tester says:

    Hi Boni,

    Thanks for this. Its working fine for chrome but i am facing difficulty in firefox, IE and Edge. Could you tell me where am going wrong?
    I tried these in my GebConfig file

    WebDriverManager.iedriver().setup()
    driver = {
    def driver = new InternetExplorerDriver()
    driver.manage().window().maximize()
    return driver
    }

    WebDriverManager.edgedriver().setup()
    driver = {
    def driver = new EdgeDriver()
    driver.manage().window().maximize()
    return driver
    }

    WebDriverManager.firefoxdriver().setup()
    driver = {
    WebDriver driver = new FirefoxDriver()
    driver.manage().window().maximize()
    return driver
    }

    It is throwing error saying
    org.openqa.selenium.WebDriverException: java.net.SocketException: Connection reset

    Complete stack trace is provided here
    https://github.com/bonigarcia/webdrivermanager/issues/270

Leave a Reply to Anonymous Cancel reply

Your email address will not be published.

Related Blog Posts
Natively Compiled Java on Google App Engine
Google App Engine is a platform-as-a-service product that is marketed as a way to get your applications into the cloud without necessarily knowing all of the infrastructure bits and pieces to do so. Google App […]
Building Better Data Visualization Experiences: Part 2 of 2
If you don't have a Ph.D. in data science, the raw data might be difficult to comprehend. This is where data visualization comes in.
Unleashing Feature Flags onto Kafka Consumers
Feature flags are a tool to strategically enable or disable functionality at runtime. They are often used to drive different user experiences but can also be useful in real-time data systems. In this post, we’ll […]
A security model for developers
Software security is more important than ever, but developing secure applications is more confusing than ever. TLS, mTLS, RBAC, SAML, OAUTH, OWASP, GDPR, SASL, RSA, JWT, cookie, attack vector, DDoS, firewall, VPN, security groups, exploit, […]