How To Run Your Tests Locally Against Chrome

The Problem

It's straightforward to get your tests running locally against Firefox. But when you want to run them against a different browser like Chrome, you quickly run into configuration overhead that can seem overly complex and lacking in code examples for getting started.

A Solution

With the introduction of WebDriver (circa Selenium 2) a lot of benefits were realized (e.g. more effective & faster browser execution, no more single host origin issues, etc). But with it came some architectural & configuration differences that may not be widely known. Namely -- browser drivers.

WebDriver works with each of the major browsers through a browser driver which is (ideally) maintained by the browser manufacturer. It is an executable file (consider it a thin layer or a shim) that acts as a bridge between Selenium and the browser.

Let's step through an example using ChromeDriver.

An Example

Before starting, we'll need to download the latest ChromeDriver binary executable from here. Once we have it we'll need to tell Selenium where it is. And there are three ways to do this:

  • Add it to the System PATH
  • Specify it in the Selenium setup
  • Launch ChromeDriver and connect to it via Selenium Remote

Let's step through the latter two options.

We'll start by pulling in our requisite libraries (e.g., selenium-webdriver to driver the browser and rspec/expectations & RSpec::Matchers to perform an assertion) and wiring up some simple setup, teardown, and run methods.

# filename: chrome.rb

require 'selenium-webdriver'
require 'rspec/expectations'
include RSpec::Matchers

def setup
  Selenium::WebDriver::Chrome::Service.executable_path = File.join(Dir.pwd, 'chromedriver')
  @driver = Selenium::WebDriver.for :chrome

def teardown

def run

Notice that in setup we are telling Selenium where the ChromeDriver exectuable is with executable_path before creating an instance of the browser.

Now we're ready to add a simple test.

# filename: chrome.rb
# ...

run do
  @driver.get 'http://the-internet.herokuapp.com/'
  expect(@driver.title).to eql 'The Internet'

If we save this file and run it (e.g., ruby chrome.rb) it will launch an instance of Chrome, visit the homepage of the-internet, and assert that the page title loaded.

Another Example

Alternatively, we can launch ChromeDriver from the command-line...

> ./chromedriver
Starting ChromeDriver 2.18.343837 (52eb4041461e46a6b73308ebb19e85787ced4281) on port 9515
Only local connections are allowed.

...and connect to it with Selenium Remote.

# filename: chrome.rb
# ...

def setup
  @driver = Selenium::WebDriver.for :remote, url: 'http://localhost:9515', desired_capabilities: :chrome

# ...

NOTE: This approach will perform faster since ChromeDriver is already running and your test just needs to connect to it. Whereas the prior approach needs to start and stop ChromeDriver for each test. The trade-off here is that ChromeDriver will keep running after your tests complete. So you'll either need to ensure it's always up, or come up with a way to shut it down after your tests finish.

Expected Behavior

When we save this file and run it (e.g., ruby chrome.rb from the command-line) here is what will happen.

Example 1

  • ChromeDriver starts
  • Chrome opens
  • Test runs
  • Chrome closes
  • ChromeDriver stops

Example 2

  • ChromeDriver starts (via command-line execution)
  • Chrome opens
  • Test runs
  • Chrome closes
  • ChromeDriver continues to run until stopped


Hopefully this tip has helped you get a better handle on how WebDriver works with various browsers and saved you some time in your configuration setup. But keep in mind that no two browser drivers are alike, so be sure to check out the documentation for the browser you care about to find out it's specific requirements:

Happy Testing!

Back to the archives