Getting started in Capybara! – A web application automation framework (Sample Program)

About Capybara

Capybara is an automation testing framework based on Ruby & Cucumber tool. Capybara is also an Acceptance Testing Framework for a web application. It supports different web drivers to interact with web browsers such as Rack, Selenium, and WebKit.


Pre-Requisites for Capybara

  1. Ruby
  2. Ruby Development Kit
  3. Bundler (Optional)
  4. Ansicon (Optional) – for colored output
  5. Text Editor (Atom Editor preferred)
  6. Web Browser (Any)

Ruby & Ruby Development Kit

If you are Windows users, you can read this document to install Ruby & Ruby Development Kit.

If you are Mac users, ruby will be already available in your system. Just ensure by typing this command in terminal.

ruby -v

Bundler

It is a gem of Ruby. You can install it by running this command.

gem install bundler

Ansicon

it is used to give colored output on the terminal/command prompt. Detailed instruction of installation from here.

Text Editor

You can use any text editor. But I personally prefer Atom editor which comes pre-defined with Gherkin, the language that cucumber tool parses.


Installing Capybara

Type these commands,

gem install cucumber
gem install capybara

This will install all the supported gem for the capybara. You will see the below message once all the dependency gems are installed.

C:\Users\Aravin> gem install capybara
...
Fetching: capybara-2.7.0.gem (100%)
Successfully installed capybara-2.7.0
Parsing documentation for addressable-2.4.0
Installing ri documentation for addressable-2.4.0
...
...
Parsing documentation for capybara-2.7.0
Installing ri documentation for capybara-2.7.0
Done installing documentation for addressable, mime-types-data, mime-types, mini_portile2, nokogiri, rack, rack-test, xpath, capybara after 20 seconds
9 gems installed

Capybara Project/Folder Structure

You can generate folder structure using the following command:

cucumber --init

This will generate folders such as features, inside features folder, there will be step_definitions and support as shown in the screenshot below.

capybara folder structure


Capybara Sample

Go to the feature folder and create the new file with extension .feature.

Sample feature file (first_feature.feature)

Feature: Fist Capybara Test

Scenario: Open a webpage
Given I have access to website
Then I should see title

You can learn more about Cucumber(Gherkin) syntax from here.

Sample Step definition (websteps.rb)

require 'capybara/cucumber'
Capybara.default_driver = :selenium

Given (/^I have access to website$/) do
 visit "http://aravin.net"
end

Then (/^I should see title$/) do
 page.find('.site-title')
end

Where,

  • require will import the necessary libraries for the ruby.
  • Capybara.default_driver = :selenium is to choose the driver. You can choose between any of these drivers
    • :rack_test (default)
    • :selenium
    • :webkit
  • visit and page.find are the capybara methods.

Running Capyabra Tests

From the project folder, type

To run all the test cases in project,

cucumber

To run selected feature file in project,

cucumber features\first_step.feature

To run selected scenario in the feature file,

Provide the line number of scenario.

cucumber features\first_step.feature:3

Output

F:\PlayGround\capybara\first-program > cucumber
Feature: Fist Capybara Test

Scenario: Open a webpage # features/first_step.feature:3
 Given I have access to website # features/step_definitions/websteps.rb:4
 Then I should see title # features/step_definitions/websteps.rb:8

1 scenario (1 passed)
2 steps (2 passed)
0m16.949s

The source code of the above example is attached below.

Source Codefirst-program.zip


For any suggestion/feedback, please drop your comment below.

One thought on “Getting started in Capybara! – A web application automation framework (Sample Program)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.