Use Chrome with Cucumber & Capybara

Photo by Tambako the Jaguar, used under Creative Commons https://flic.kr/p/dJD2dV

We frequently swap out Firefox for Chrome in our selenium (@javascript) cucumber tests. It is actually very easy, but not widely known.

Update: I got some questions about why we’d use Chrome. Speed hasn’t been the issue (use capybara-webkit for that) but we have found some things that work better in Chrome vs Firefox (and vice-versa). Also, you get different rendering options and different standards support (for example, websockets). If you’re having test problems, switching probably isn’t your solution, but you can always try.

Step 1 - Tell Capybara about Chrome

Inside your features/support/env.rb add these lines:

Capybara.register_driver :chrome do |app|
  Capybara::Selenium::Driver.new(app, :browser => :chrome)
end

Capybara.javascript_driver = :chrome

Step 2 - Install chromedriver

If you’re using homebrew on OS X (and why wouldn’t you?) simply install using:

brew install chromedriver

Otherwise, download it manually and make sure it is in your path (I typically put it in /usr/local/bin).

Step 3 - Run the features

Run your features and anything tagged with @javascript or @selenium is now running in Chrome instead of Firefox. Yay!

Photo of Daniel Morrison

Daniel founded Collective Idea in 2005 to put a name to his growing and already full-time freelance work. He works hard writing code, teaching, and mentoring.

Comments

Add a Comment

Hmm...that didn't work.

Something went wrong while adding your comment. If you don't mind, please try submitting it again.

Comment Added!

Your comment has been added to this post. Please refresh this page to view it.

Optional. If added, we will display a link to the website in your comment.
Optional. Never shared or displayed in your comment.
  1. glennmaritn76@gmail.com
    glenn
    September 27, 2012 at 4:31 AM

    Thanks mate, this works fiine, but do you know how to override the user agent of the chrome browser? i’ve seen alot of tutorials but only with Firefox, but i much rather do it with chrome browser.

  2. December 17, 2012 at 16:31 PM

    @glenn: see https://makandracards.com/makandra/8381-run-chrome-in-a-specific-resolution-or-user-agent-with-selenium.

  3. ms91325@gmail.com
    Maninder
    March 27, 2013 at 0:09 AM

    no able to find inner html of an element
    eg

    Hello World

    not able o get value “Hello World”
    Using cucumber, capybara and crome driver
    All Latest versions

  4. November 16, 2013 at 2:31 AM

    Does not work on mavricks

  5. gpumple@adjuggler.com
    gabe
    October 16, 2014 at 17:57 PM

    My env.rb looks like this:

    Capybara.default_selector = :css
    Capybara.register_driver :selenium do |app|
    Capybara::Selenium::Driver.new(app, :browser => :chrome, :switches => %w[–test-type]) #comment it out!!!
    end
    ActionController::Base.allow_rescue = false
    begin
    DatabaseCleaner.strategy = :transaction
    rescue NameError
    raise “You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it.”
    end
    Cucumber::Rails::Database.javascript_strategy = :truncation

    that’s the parts that are not commented out anyway. It works great. I would like to test in firefox as well, and ideally eventually with other browsers. How do I switch back to firefox? 

    thanks. 

  6. November 10, 2018 at 8:52 AM

    You completed a few nice points there. I did a search on the theme and found nearly all persons will consent with your blog.

  7. November 12, 2018 at 5:53 AM

    Thank you!!!! I did not install Mercury Reader because I use Firefox but it has an add on simply titled “Reader” and it is fantastic. Your article motivated me to look it up.