RSpec, Capybara for Web Automation

Why Automate?

Becuase manual testing is boring! Hmm, well in some case it is but we have to accept the fact that a lot of errors do happen when we have to test the same scenarios multiple times with different set of data and on different platforms running different browsers. Woah! that’s something to think about. But remember, automation is very useful only for repetitive tasks that rarely change and for those applications which need to be verified with a variety of datasets. No automation can replace manual testers in the case of exploratory testing and it is evident that any product without manual testing is difficult to trust upon and it is the humans who decide on what to automate and what not to!


Let’s learn how to automate web applications. Today we have a lot of frameworks for web automation, to name a few famous ones: Selenium-webdriver, Watir-webdriver. And selenium forms the backbone for most of the frameworks.

We will consider Capybara which runs on selenium, RSpec for BDD and the website under test, the-internet, which has all the features necessary for an interactive web application.


Install Ruby and from the command line run

gem install capybara

Note: By default Capybara will only locate visible elements. This is because a real user would not be able to interact with non-visible elements.

Note: All searches in Capybara are case sensitive. This is because Capybara heavily uses XPath, which doesn’t support case insensitivity.

If you need to know about different frameworks and the methods to learn basics of Capybara then hit the site GitHub. But before we jump in automating the website, it is better to visit the site and explore manually once. Then go ahead, I will be waiting for you…

The Beginning

I believe you got an insight on the website and ready to automate. Wait! do we have any plan for how we are automating, I mean, do we need to follow any rules or just make some script which just automates the task at hand?. Well, that’s a valid question and an important thing to consider before automating any software.

Here, we will use ‘Page Object Model’, a way of grouping together the set of elements so as to keep the code clean and maintainable. If you wish to learn more about POM then I suggest you to Google it, as one can find tons of resource on the internet.

I have written a simple POM for the-internet website as given below. Here, I am imitating the human interaction with the website using Capybara and Ruby and verifying them using RSpec. I believe the below  code is self explanatory.

Create a Class file, 'the_heroku_app.rb':

class The_Heroku_App

  def start_browser
    # This will create a Capybara session and uses the default browser of firefox.
    @session = (:selenium)

  def tear_down

  def go_to(site)
    @session.visit site

  def click_on_link(name)
    @session.click_link name

  def checkbox_clicked?
    @session.find(:css, 'input:nth-child(1)[type="checkbox"]').checked?

  def click_checkbox
    @session.find(:css, 'input:nth-child(1)[type="checkbox"]').click

  def clear_checkbox
    @session.find(:css, 'input:nth-child(1)[type="checkbox"]').clear

  # Save the file which was downloaded from GitHub, the below step will 
  # simulate the drag and drop functionality.
  def drag_and_drop_script
    dnd_javascript ='C:\...\***_Test_Automation' + '\dnd.js')
    dnd_javascript+"$('#column-a').simulateDragDrop({ dropTarget: '#column-b'});"

  def by_id(name)

  def find_all_drop_down

  def select_dropdown(x), from: 'dropdown')


I have used RSpec as the testing framework and all the test scripts are run via RSpec. The scripts can be run as a complete suite or can be run individually using the ‘tags’. I will show how to use both.


  1. The ‘Basic Auth’ for invalid credentials scenario needs you to click, ‘Cancel’ when prompted for user/pass details. I want you guys to figure out on how to overcome this. (It’s easy tough!!)
  2. To simulate the ‘Drag and Drop’ functionality, I have used the javascript written by rcorreia.
Save the below code in a separate file called, 'the_heroku_spec.rb'

require 'rspec'
require 'capybara'
require 'C:\...\***_Test_Automation\the_heroku_app.rb'

describe 'Verifying the components of the_heroku_app' do

  # The before and after is run everytime for each test cases.
  before(:each) do
    @cls_instance =
    @browser = @cls_instance.start_browser
    @cls_instance.go_to ''

  after(:each) do

  context 'Title and Body' do

    it 'Check for title of the page', page_title: true  do
      expect(@browser.title).to eq('The Internet')

    it 'Check for the body of page', page_body: true  do
      expect(@browser).to have_content('Welcome to the Internet')


  context 'Checkbox' do

    it 'Verify Checkbox by clicking', chk_box: true  do
      expect(@browser).to have_content('Welcome')
      @cls_instance.click_on_link 'Checkboxes'
      checkbox_1 = @cls_instance.checkbox_clicked?

      if !checkbox_1
        expect(@cls_instance.checkbox_clicked?).to eq TRUE
        expect(@cls_instance.checkbox_clicked?).to eq FALSE


  context 'Credentials' do

    it 'Verify Basic Auth with VALID credentials', valid: true do
      @cls_instance.click_on_link 'Basic Auth'
      @browser.visit ''
      expect(@browser).to have_content 'Congratulations! You must have the proper credentials.'

    it 'Verify Basic Auth with INVALID credentials', invalid: true  do
      @cls_instance.click_on_link 'Basic Auth'
      @browser.visit ''
      expect(@browser).to have_content 'Not authorized'


  context 'Drag and Drop' do

    it 'Should drag and drop Column A to Column B', dnd: true  do
      @cls_instance.click_on_link 'Drag and Drop'
      expect(@cls_instance.by_id('column-a').text).to eq 'B'
      expect(@cls_instance.by_id('column-b').text).to eq 'A'


  context 'Dropdown box/Select box' do

    it 'Should select all options', drop_down: true  do
      @cls_instance.click_on_link 'Dropdown'
      values = @cls_instance.find_all_drop_down
      nw_vals = values[1..values.length]
      results = []
      nw_vals.each {|x| results << @cls_instance.select_dropdown(x) == 'ok' ? true: false}
      results.each{|x| expect(x).to eq 'ok'}



Run the complete Suite of test cases:


Run particular test case from the suite (use 'tag'):


If you want to exclude few of the test cases from the suite then add the below code in your RSpec,
above the 'describe':

RSpec.configure do |c|
  c.filter_run_excluding valid: true
  c.filter_run_excluding invalid: true
describe 'Verifying the components of the_heroku_app' do

  before(:each) do
    @cls_instance =
    @browser = @cls_instance.start_browser
    @cls_instance.go_to ''

When you run the RSpec it will skip those test cases which you have specified with the tag name.


Test Result:


Note: Post will be updated...

One thought on “RSpec, Capybara for Web Automation

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s