Back to Blog

How to use Chromedriver with Travis CI for Selenium tests

How to use Chromedriver with Travis CI for Selenium tests

This guide takes you through how to download and extract Chromedriver to use alongside Travis, a continuous integration tool. Read on for some useful tips on commonly encountered troubleshooting issues.  

The Code for Life initiative has now passed the 100,000 registered user mark. This high visibility and its purpose as a platform primarily used by teachers and young students means we have to be incredibly thorough when it comes to testing and security. To make our lives easier we decided to use a continuous integration tool.

When it came to deciding upon which particular tool fitted our needs we opted for Travis – a continuous integration (CI) tool used to automatically run tests after a pull request is made, or the master branch is updated on GitHub.

We have now used Travis for over a year, and would like to share some knowledge and best practices on how to avoid some of its pitfalls.

For example, in the case of our Selenium tests, Travis used the Firefox browser by default (Webdriver). The browser would not load the elements in time, and then time out, causing our tests to fail. More generally, the whole Travis build using Firefox was quite slow. Because of this, we chose to upgrade Travis and make it run the tests using Google Chrome. Since then, we have noticed a significant increase in speed and have successfully come up with a solution that does not cause any timeout errors.

To make Travis use Google Chrome’s Chromedriver browser instead, you need to:

  1. Find which version of Chromedriver to use
  2. Download and extract Chromedriver
  3. Update your tests settings to set Chrome as the default browser

Find out which version of Chromedriver to use

  1. In your browser, go to http://chromedriver.storage.googleapis.com/ – This page lists all of the available Chromedriver versions, for different operating systems and machines.
  2. Make a note of which version you require.

Download and extract Chromedriver

  1. Open the .travis.yml file, in your project folder. This file is your Travis build configuration.
  2. Find the section named before_script:This is where you want to download and extract the executable file for Chromedriver.
  3. At the top of before_script, add these lines:
- wget http://chromedriver.storage.googleapis.com/2.24/chromedriver_linux64.zip
- unzip chromedriver_linux64.zip -d 
/home/travis/virtualenv/python2.7.12/bin/
- export CHROME_BIN=chromium-browser

Take care to replace the words in bold with your chosen version of Chromedriver.

Update your test settings, setting Chrome as the default browser

  1. Open the test_settings.py file in your project folder.
  2. Find the dictionary variable called SELENIUM_WEBDRIVERS
  3. Switch the two entries around so that the dictionary looks like this:

    SELENIUM_WEBDRIVERS = {
         ‘default’: {
              ‘callable’: webdriver.Chrome,
              ‘args’: (),
              ‘kwargs’: {},
         },
         ‘firefox’: {
              ‘callable’: webdriver.Firefox,
              ‘args’: (),
              ‘kwargs’: {},
         },
    }

Troubleshooting

  1. Cannot create extraction directory
  2. No output has been received in the last 10 minutes
  3. Import error: failed to import test module

Cannot create extraction directory

If you see the following error message, then the Chromedriver .zip file is being extracted to the wrong directory, or the directory doesn’t exist yet:

checkdir: cannot create extraction directory: 

/home/travis/virtualenv/python2.7.12/bin
 
 No such file or directory
 The command "unzip chromedriver_linux64.zip -d /home/travis/virtualenv/python2.7.12/bin/" failed and exited with 2.

The Chromedriver .zip file needs to be extracted to the project’s PATH variable. If you are unsure what this is, add echo $PATH at the top of the before_script block. This will output a list of valid project PATH directories which can contain the executable file.

If the directory hasn’t been created yet, you can create it by adding the command:

mkdir /home/travis/virtualenv/python2.7.12/bin

No output has been received in the last 10 minutes

If you see the following error, you are probably out of memory.

No output has been received in the last 10m0s, this potentially indicates a stalled build or something wrong with the build itself.

To fix this, add this line to the very top of your .travis.yml file:

sudo: required

This configuration runs the build in a sudo-enabled infrastructure, which allocates 7.5GB of RAM, instead of the default 4GB.

Import error: failed to import test module

If you see this error in the build, add this line at the very top of your travis.yml file:

build: trusty

We hope this guide has got you on the way to using chromedriver for all your testing needs. For more information about our ongoing projects at Code for Life, visit our GitHub page.

Here are some links related to Travis and how we’ve used it for our Code for Life projects:

Let us know in the comments section below if you have any other useful tips and tricks for Travis.

Florian Aucomte, Technology Intern – Code for Life

Scroll Up