Instruction to set up a Rails E2E test environment, which covers Google Chrome, FireFox and PhantomJS as test browsers.

See Liooo/rails4_turnip_capybara_selenium_poltergeist for the complete app.

Basic Setup

Add the following lines in Gemfile.

Gemfile

1
2
3
4
5
6
7
group :test do
  gem 'turnip'
  gem 'rspec-rails'
  gem "capybara"
  gem 'poltergeist'
  gem 'selenium-webdriver'
end

Then run bundle install.

Execute rails g rspec:install, which generates .rspec file and spec directory. Add the following line in .rspec file to tell Rspec to run turnip tests.

.rspec

1
-r turnip/rspec

Now write features, make features/ directory under spec/ and create a feature file.

spec/features/my_test.feature

1
2
3
4
5
Feature: Test a Feature

  Scenario: Let's test
    When it executes
    Then it passes

Add steps directory, and write steps.

spec/features/steps/my_test_steps.rb

1
2
3
4
5
6
7
step 'it executes' do
  visit '/'
end

step 'it passes' do
  expect(1).to be(1)
end

Then create turnip_helper.rb and make it like this.

spec/turnip_helper.rb

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
require 'turnip'
require 'turnip/capybara' # to use Capybara DSL methods in steps
require 'capybara'
require 'selenium-webdriver'
require 'capybara/poltergeist'

Dir.glob( File.expand_path("../features/steps/**/*steps.rb", __FILE__) ){ |f| load f, true  }

Capybara.configure do |c|
  c.javascript_driver = :selenium
  c.current_driver = :selenium
end

Turnip explains what turnip_helper.rb is.

Before loading your spec_helper, Turnip also tries to load a file called turnip_helper where you can setup anything specific to your turnip examples. You might find it beneficial to load your steps from this file so that they don’t have to be loaded when you run your other tests.

Now your test should be able to run. Spin up server by rails server and run rspec. You’ll see FireFox browser starts up and the test result is shown in the console.

Changing Browser

You can see the available browser drivers by Capybara.drivers in ruby code. By default you’ll only see rack_test. Here’s how to add other drivers.

PhantomJS

If you don’t have PhantomJS executable, get it first. In MacOS you can install with homebrew.

1
brew install phantomjs

Then tell capybara to use PhantomJS. We specify the :poletergeist which is a PhantomJS driver for Capybara.

1
2
3
4
Capybara.configure do |c|
  c.javascript_driver = :poltergeist
  c.current_driver = :poltergeist
end

Google Chrome

Add the following gem.

Gemfile

1
gem "chromedriver-helper"

This gem installs chromedriver in gem path. You can download chromedriver manually as well, but why not be lazy.

Finally, register the driver and tell capybara to use chrome.

spec/turnip_helper.rb

 1
 2
 3
 4
 5
 6
 7
 8
 9
10

# :selenium_chrome is just a name you set, you can make it whatever you want.
Capybara.register_driver :selenium_chrome do |app|
  Capybara::Selenium::Driver.new(app, :browser => :chrome)
end

Capybara.configure do |c|
  c.javascript_driver = :selenium_chrome
  c.current_driver = :selenium_chrome
end

Other browsers

I haven’t searched yet, these documents might help.

InternetExplorer

Opera