Rails3, Cucumber, & Capybara

By Keith Connolly on September 25, 2012

Get Reading

When dealing with rails (or any other language) you want to have a proper testing suite.  Normally, I use simple things like PHPunit (on PHP), and it's like for any other language I'm using at the moment.  However, sometimes when working on a larger project you want to side for something larger... more robust.  That's where cucumber and capybara come in.  The ease of testing you get from capybara and cucumber working in tandem is truely incredible.  In the following example I assume you have a model, controller, etc. setup for your rails app (at minimum).

1) You'll need to install the gems you need via bundler.

In your gemfile:

group :test do
  gem 'cucumber-rails'
  gem 'database_cleaner'
  gem 'rspec'
  gem 'spork'
end

2) Then as usual install from bundler:

$ bundle Install 

 

3) Generate Cucumber stuffs.

$ rails g cucumber:install

4) Create some features by creating a file in the newly generated features folder, call it as appropriate to your model.  Mine was features/page.feature

 

Feature: Gomo user manages pages
  Scenario: User adds a new page to their account
    Given I go to the new page page
    And I fill in "Title" with "My Test Site"
    When I press "Create"
    Then I should be on the page list page
    And I should see "My Test Site"

 

5) Once you have a feature written in "test english" (you can say just about anything -- since you'll be making the steps that the test will use).

6) Run cucumber  it'll display a list of step definitions you'll need to implement.

$ cucumber

7) Mine looked like this:

 

Given /^I go to the new page page$/ do
  pending
end
 
Given /^I fill in "(.*?)" with "(.*?)"$/ do |arg1, arg2|
  pending
end
 
When /^I press "(.*?)"$/ do |arg1|
  pending
end
 
Then /^I should be on the page list page$/ do
  pending
end
 
Then /^I should see "(.*?)"$/ do |arg1|
  pending
end
 
8) Actually define these steps by writing some VERY simple code.  You'll need to create a step definition file like this features/step_definitions/{model}_step.rb
 
9) Now we actually fill in these steps, what you should do is run $ cucumber, it'll run your tests and show you that they are all failing, which is proper for this case.  We then fill in the step definitions with some capybara commands (which tie in with selenium super easily) like this:
 
Given /^I go to the new page page$/ do
  visit new_page_path
end
 
Given /^I fill in "(.*?)" with "(.*?)"$/ do |arg1, arg2|
  fill_in arg1, :with => arg2
end
 
When /^I press "(.*?)"$/ do |arg1|
  click_button arg1
end
 
Then /^I should be on the page list page$/ do
  page.should have_content('Listing pages')
end
 
Then /^I should see "(.*?)"$/ do |arg1|
  page.should have_content(arg1)
end
 
10) These fill_in arg1 and similar commands are all capybara handling the actual testing, we are simply telling it what english steps relate to what interenal actions.  
 
There you have a full integration with capybara and cucumber, maybe someday soon I'll write about selenium and/or make a screen cast.  Feel free to comment!

 

Get in touch

I really like hearing from those who read my content, engauge with me via the comments on each post are by one of the many contact methods below.


Keith Connolly
Queen Creek, Arizona 85142
P: (480) 231-9285