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
- Ruby Development Kit
- Bundler (Optional)
- Ansicon (Optional) – for colored output
- Text Editor (Atom Editor preferred)
- 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.
It is a gem of Ruby. You can install it by running this command.
gem install bundler
it is used to give colored output on the terminal/command prompt. Detailed instruction of installation from here.
You can use any text editor. But I personally prefer Atom editor which comes pre-defined with Gherkin, the language that cucumber tool parses.
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:
This will generate folders such as features, inside features folder, there will be step_definitions and support as shown in the screenshot below.
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)
Capybara.default_driver = :selenium
Given (/^I have access to website$/) do
Then (/^I should see title$/) do
- 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)
page.find are the capybara methods.
Running Capyabra Tests
From the project folder, type
To run all the test cases in project,
To run selected feature file in project,
To run selected scenario in the feature file,
Provide the line number of scenario.
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)
The source code of the above example is attached below.
Source Code: first-program.zip
For any suggestion/feedback, please drop your comment below.