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!

automation-300x300

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.

Installation

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 = Capybara::Session.new (:selenium)
  end

  def tear_down
    @session.execute_script('window.close();')
  end

  def go_to(site)
    @session.visit site
  end

  def click_on_link(name)
    @session.click_link name
  end

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

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

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

  # 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 = File.read('C:\...\***_Test_Automation' + '\dnd.js')
    dnd_javascript+"$('#column-a').simulateDragDrop({ dropTarget: '#column-b'});"
  end

  def by_id(name)
    @session.find_by_id(name)
  end

  def find_all_drop_down
    @session.find('#dropdown').all('option').collect(&:text)
  end

  def select_dropdown(x)
    @session.select(x, from: 'dropdown')
  end

end

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.

Note:

  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 = The_Heroku_App.new
    @browser = @cls_instance.start_browser
    @cls_instance.go_to 'https://the-internet.herokuapp.com'
  end

  after(:each) do
    @cls_instance.tear_down
  end

  context 'Title and Body' do

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

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

  end

  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
        @cls_instance.click_checkbox
        expect(@cls_instance.checkbox_clicked?).to eq TRUE
      else
        @cls_instance.clear_checkbox
        expect(@cls_instance.checkbox_clicked?).to eq FALSE
      end
    end

  end

  context 'Credentials' do

    it 'Verify Basic Auth with VALID credentials', valid: true do
      @cls_instance.click_on_link 'Basic Auth'
      @browser.visit 'http://admin:admin@the-internet.herokuapp.com/basic_auth'
      expect(@browser).to have_content 'Congratulations! You must have the proper credentials.'
    end

    it 'Verify Basic Auth with INVALID credentials', invalid: true  do
      @cls_instance.click_on_link 'Basic Auth'
      @browser.visit 'http://admin:123@the-internet.herokuapp.com/basic_auth'
      expect(@browser).to have_content 'Not authorized'
    end

  end

  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'
      @browser.execute_script(@cls_instance.drag_and_drop_script)
      expect(@cls_instance.by_id('column-a').text).to eq 'B'
      expect(@cls_instance.by_id('column-b').text).to eq 'A'
    end

  end

  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'}
    end

  end


end

Run the complete Suite of test cases:

complete_suite

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

individual_test_case


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
end
describe 'Verifying the components of the_heroku_app' do

  before(:each) do
    @cls_instance = The_Heroku_App.new
    @browser = @cls_instance.start_browser
    @cls_instance.go_to 'https://the-internet.herokuapp.com'
  end
.
.
.
.

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

rspec_exclude

Test Result:

Test_result

Note: Post will be updated...
Advertisements

One thought on “RSpec, Capybara for Web Automation

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s