Monday, September 21, 2015

Automate test on the web with Ruby and Capybara

Our highest priority is to satisfy the customer through early and continuous delivery of valuable software. In this situation testing is very important to delivery good and bug free products. In Ruby on Rails there are many testing tools. But most of the developer like to use human readable code. For this reason Rspec is very popular for behaviour driven development testing framework. It helps us to figure out what to test, where to start and what to ignore.

Also we need to do the acceptance test of your application as a blackbox. It interact with the interface and observe the results. Capybara is a lightweight browser automation library. It helps you to test web applications by simulation how a real user would interact with your app. In this post I will describe how to use Capybara with Rspec.
First open a new rails application and create tasklist using scaffolding generator.
$ rails new capybaratest & cd capybaratest
$ rails g scaffold tasklist name:string
$ rake db:migrate
Now we will install rspec –
put gem ‘rspec-rails’, ‘~> 3.0′ on the gemfile.
$ bundle install
$ rails generate rspec:install
$ rails g integration_test tasklistrake spec:requests
this automatic test will pass. Now we will write our own test case

on spec/request/tasklists_spec.rb file please write this block of code.
describe "GET /tasklists" do
it "Task will work" do
Tasklist.create!(:name => "New task 1")
get tasklists_path
response.body.should include("New task 1")
Run the command - rake spec:requests to check this block of code is passed or not. Let write another test block via post request –
describe "POST /tasklists" do
it "creates a task" do
post_via_redirect tasklists_path,
:tasklist => { :name => "New task 2" }
response.body.should include("New task 2")

Now two test will be passed.
We can use capybara DSL on these block of code.
describe "GET /tasks" do
it "displays tasks" do
Tasklist.create!(:name => "New task 1")
visit tasklists_path
page.should have_content("New task 1")
describe "POST /tasks" do
it "creates a task" do
visit new_tasklist_path
fill_in "Name", :with => "New task 2"
click_button "Add"
page.should have_content("Tasklist was successfully created.")
page.should have_content("New task 2")

Test will be passed.
Testing Javascript with Capybara:
By default Capybara uses the :rake_test driver. It’s pure Ruby, SO it interacts directly with Rake interfaces. It does not require a server to be started. But It has some limitation. We can not use it for remote application like redirects to external site, external API or OAuth services. Also It does not support Javascript.
Capybara also supports Selenium 2.0 (Webdriver) . It runs tests in a real browser and supports any Javascript your browser supports, just like real user. But it’s slow and GUI browser adds a lot of cruft you don’t want. It has no console.log output.
Best way is to use capybara-webkit. It’s fast and has console.log output. It display in standard output. You can install it with:
$ gem install capybara-webkit

And you can use it by:
Capybara.javascript_driver = :webkit

Navigation (visit)
matcher (page.should)
clicks(click_link / click_button )
action ( fill_in)
scope (within)
Scripting ( page.execute_script )
debugger ( save_and_open_page )

You can mix the DSL into any context by including Capybara::DSL
require 'capybara'
require 'capybara/dsl'

Capybara.default_driver = :webkit

module MyModule
  include Capybara::DSL

  def login!
    within("//form[@id='session']") do
      fill_in 'Email', :with => ''
      fill_in 'Password', :with => 'password'
    click_button 'Sign in'

I hope the blog gives a good introduction of adding capybara tests to your rails apps. You can check on github repo of capybara to get more information.


  1. Interestig post, thanks for sharing. One of my friend is working at and he says thar Selenium is better than Capybara. Can you tell me what's the difference?

  2. It helps us to figure out what to test, where to start and what to ignore.web design tips