Geb Quickie: Automatically download drivers for Chrome and Internet Explorer

A common error encountered by Geb beginners using Webdriver with Chrome or Internet Explorer is that they forget to download and properly configure chromedriver / iedriver.

In this post, I will show you how to set up your project so that it ensures that your driver file is available for your tests. ( Credit Marcin Erdmann for initial implementation ).

We start with the GebConfig provided in the grails-geb-example project. So the environment for chrome looks like this:

environments {

    // run as “grails -Dgeb.env=chrome test-app”
    // See: http://code.google.com/p/selenium/wiki/ChromeDriver
    chrome {
        driver = { new ChromeDriver() }
    }

... 
}

We’re going to modify the chrome section so that it sets up the latest chromedriver for mac. To do so, we must download the latest chromedriver if it does not exists.

For this project, I’m going to put the latest chromedriver in /test/drivers/chrome/chromedriver, and am going to skip the download if the file already exists.

On the Mac, I also need to change my permissions so that chromedriver can execute.

I will use the ant builder available to groovy in my helper method, but you can use whatever you want. AntBuilder just makes it slightly easier to work with downloading and unzipping files.

The method looks like this:

private void downloadDriver(File file, String path) {
    if (!file.exists()) {
        def ant = new AntBuilder()
        ant.get(src: path, dest: 'driver.zip')
        ant.unzip(src: 'driver.zip', dest: file.parent)
        ant.delete(file: 'driver.zip')
        ant.chmod(file: file, perm: '700')
    }
}

I will add this just above the environment block.

I now just need to call this downloadDriver method and set the environment variable:

environments {

    // run as “grails -Dgeb.env=chrome test-app”
    // See: http://code.google.com/p/selenium/wiki/ChromeDriver
    chrome {
        def chromeDriver = new File('test/drivers/chrome/chromedriver')
        downloadDriver(chromeDriver, "http://chromedriver.googlecode.com/files/chromedriver_mac_23.0.1240.0.zip")
        System.setProperty('webdriver.chrome.driver', chromeDriver.absolutePath)
        driver = { new ChromeDriver() }
    }

    ...
}

Notice that I pass in a mac specific chromedriver. But you can change _mac_ to _win_, _linux32_ or _linux64_. There are convenience methods provided by Apache Commons SystemUtils that come bundled with grails you can use to make this code more system specific.

I also set a system property for the chrome driver after it has been downloaded. (line 8).

Now, when I run the tests, it will automatically download chromedriver into the test/drivers directory if it doesn’t exist.

For your reference, a final GebConfig can be found here. The linked project also updates the example to grails 2.2.0.

Similarly, for internet explorer, you would need to use an IE driver, and the download link would look something like this:

def ieDriver = new File( 'test/drivers/ie/IEDriverServer.exe' )
downloadDriver( ieDriver, 'http://selenium.googlecode.com/files/IEDriverServer_x64_2.28.0.zip' )
System.setProperty('webdriver.ie.driver', ieDriver.absolutePath)

Note the system property is webdriver.ie.driver and the file path is different.

One last thing, you would want to exclude the test/drivers directory recursively from your source control system.

Happy testing.

8 thoughts on “Geb Quickie: Automatically download drivers for Chrome and Internet Explorer

  1. Pingback: Geb Quickie: Automatically download drivers for Chrome and Internet Explorer | Nevada Java User Group

  2. anotherjay

    Thanks for this post. I got caught up on one thing which I think is just a typo:

    def chromeDriver = new File(‘test/drivers/chrome/chromedriver’)

    I had to change this to:

    def chromeDriver = new File(‘test/drivers/chrome/chromedriver.exe’)

    otherwise the “webdriver.chrome.driver” system property gets set to “test\drivers\chrome\chromedriver” instead of “test\drivers\chrome\chromedriver.exe” and you get the following exception:

    Caused by: java.lang.IllegalStateException: The driver executable does not exist: path\to\project\test\drivers\chrome\chromedriver

    Just thought I’d leave that feedback in case someone else tries to use this example and runs into the same issue.

    Thanks again!

    Reply
  3. Alex White

    Would you guys expect this code to work here in 2016? I can’t seem to get the code to run that loads the driver and sets the system property. Running the tests all fail: java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property;

    Reply
  4. Pingback: An Army of Solipsists » Blog Archive » This Week in Grails (2013-01)

Leave a comment