Rails E2E Test Environment by Rspec, Turnip, Capybara, Selenium and Poltergeist
Contents
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.
|
|
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.
|
|
Now write features, make features/ directory under spec/ and create a feature file.
|
|
Add steps directory, and write steps.
|
|
Then create turnip_helper.rb and make it like this.
|
|
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.
|
|
Then tell capybara to use PhantomJS. We specify the :poletergeist
which is a PhantomJS driver for Capybara.
|
|
Google Chrome
Add the following gem.
|
|
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.
|
|
Other browsers
I haven’t searched yet, these documents might help.