Capybara is a web-based
test automation
In software testing, test automation is the use of software separate from the software being tested to control the execution of tests and the comparison of actual outcomes with predicted outcomes. Test automation can automate some repetitive bu ...
software that simulates scenarios for
user stories and automates
web application
A web application (or web app) is application software that is accessed using a web browser. Web applications are delivered on the World Wide Web to users with an active network connection.
History
In earlier computing models like client-serve ...
testing for
behavior-driven software development. It is written in the
Ruby programming language
Ruby is an interpreted, high-level, general-purpose programming language which supports multiple programming paradigms. It was designed with an emphasis on programming productivity and simplicity. In Ruby, everything is an object, including p ...
.
Capybara can mimic actions of real users interacting with web-based applications. It can receive pages, parse the
HTML
The HyperText Markup Language or HTML is the standard markup language for documents designed to be displayed in a web browser. It can be assisted by technologies such as Cascading Style Sheets (CSS) and scripting languages such as JavaScri ...
and submit
forms
Form is the shape, visual appearance, or configuration of an object. In a wider sense, the form is the way something happens.
Form also refers to:
*Form (document), a document (printed or electronic) with spaces in which to write or enter data
* ...
.
Background and motivation
During the software development process (especially in the
Agile
Agile may refer to:
* Agile, an entity that possesses agility
Project management
* Agile software development, a development method
* Agile construction, iterative and incremental construction method
* Agile learning, the application of incremen ...
and
Test-driven Development
Test-driven development (TDD) is a software development process relying on software requirements being converted to test cases before software is fully developed, and tracking all software development by repeatedly testing the software against a ...
environments), as the size of the tests increase, it becomes difficult to manage tests which are complex and not modular.
By extending the human-readable
behavior-driven development
In software engineering, behavior-driven development (BDD) is an agile software development process that encourages collaboration among developers, quality assurance experts, and customer representatives in a software project. It encourages teams ...
style of frameworks such as
Cucumber
Cucumber (''Cucumis sativus'') is a widely-cultivated creeping vine plant in the Cucurbitaceae family that bears usually cylindrical fruits, which are used as culinary vegetables.[RSpec
RSpec is a computer domain-specific language (DSL) (particular application domain) testing tool written in the programming language Ruby to test Ruby code. It is a behavior-driven development (BDD) framework which is extensively used in produ ...]
into the automation code itself, Capybara aims to develop simple web-based automated tests.
Anatomy of Capybara
Capybara is a Ruby library (also referred to as a
gem
A gemstone (also called a fine gem, jewel, precious stone, or semiprecious stone) is a piece of mineral crystal which, in cut and polished form, is used to make jewelry or other adornments. However, certain rocks (such as lapis lazuli, opal, a ...
) that is used with an underlying web-based driver. It consists of a user-friendly
DSL
Digital subscriber line (DSL; originally digital subscriber loop) is a family of technologies that are used to transmit digital data over telephone lines. In telecommunications marketing, the term DSL is widely understood to mean asymmetric dig ...
(Domain Specific Language) which describe actions that are executed by the underlying web driver.
When the page is loaded using the DSL (and underlying web driver), Capybara will attempt to locate the relevant element in the
DOM (Document Object Model) and execute an action such as click button, link, etc.
Drivers
By default, Capybara uses the
:rack_test
driver which does not have any support for executing
JavaScript
JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of Website, websites use JavaScript on the Client (computing), client side ...
. Drivers can be switched in Before and After blocks. Some of the web drivers supported by Capybara are mentioned below.
RackTest
Written in Ruby, Capybara's default driver RackTest does not require a server to be started since it directly interacts with
Rack interfaces. Consequently, it can only be used for Rack applications.
Selenium
Selenium
Selenium is a chemical element with the symbol Se and atomic number 34. It is a nonmetal (more rarely considered a metalloid) with properties that are intermediate between the elements above and below in the periodic table, sulfur and telluriu ...
-webdriver, which is mostly used in web-based automation frameworks, is supported by Capybara. Unlike Capybara's default driver, it supports JavaScript, can access HTTP resources outside of application and can also be set up for testing in headless mode which is especially useful for CI scenarios.
Capybara-webkit
Capybara-webkit driver (a gem) is used for true
headless browser
A headless browser is a web browser without a graphical user interface.
Headless browsers provide automated control of a web page in an environment similar to popular web browsers, but they are executed via a command-line interface or using netw ...
testing with JavaScript support. It uses
QtWebKit and it is significantly faster than Selenium as it does not load the entire browser.
Matchers
Capybara locates an element either using
Domain-specific language or
XPath
XPath (XML Path Language) is an expression language designed to support the query or transformation of XML documents. It was defined by the World Wide Web Consortium (W3C) and can be used to compute values (e.g., strings, numbers, or Boolean v ...
/
CSS Selectors. Partial matches can lead to unexpected results. Two or more matches can even result in a failure with an Ambiguous match error. The following are the matching strategies supported by Capybara:
first: Pick the first element which matches. Not advisable to use.
one: Allow only one element match. Error raised if more than one match.
smart: If Capybara.exact is true, it behaves like the above option (one). If Capybara.exact is false, it will first try to find an exact match. Ambiguous exception is raised if more than one match is found. If no element is found, a new search for inexact matches is commenced. Again, an ambiguous exception is raised if more than one match is found.
prefer_exact: Finds all matching (exact and which are not exact) elements. If multiple matches are found then the first exactly matching element is returned discarding other matches.
Usage
User-registration process
Here is an example of how user registration test is done using Capybara. There is a test to see if the user can continue with the registration process or if there are any holds on him. If he has the requisite credentials, he will be registered and then redirected to the 'Welcome' page.
describe 'UserRegistration' do
it 'allows a user to register' do
visit new_user_registration_path
fill_in 'First name', :with => 'New'
fill_in 'Last name', :with => 'User'
fill_in 'Email', :with => '[email protected]'
fill_in 'Password', :with => 'userpassword'
fill_in 'Password Confirmation', :with => 'userpassword'
click_button 'Register'
page.should have_content 'Welcome'
end
end
Capybara with Cucumber
An example of a Capybara feature used with Cucumber:
When /^I want to add/ do
fill_in 'a', :with => 100
fill_in 'b', :with => 100
click_button 'Add'
end
Capybara with RSpec
Some minute integration is required in order to use Capybara with
RSpec
RSpec is a computer domain-specific language (DSL) (particular application domain) testing tool written in the programming language Ruby to test Ruby code. It is a behavior-driven development (BDD) framework which is extensively used in produ ...
describe 'go to home page' do
it 'opens the home page' do
visit (get_homepage)
expect(page).to have_content('Welcome')
end
end
Similar tools
*
Watir
*
Selenium (software)
See also
{{Portal, Free and open-source software
*
Acceptance testing
In engineering and its various subdisciplines, acceptance testing is a test conducted to determine if the requirements of a specification or contract are met. It may involve chemical tests, physical tests, or performance tests.
In systems e ...
*
Acceptance test-driven development
*
Behavior-driven development
In software engineering, behavior-driven development (BDD) is an agile software development process that encourages collaboration among developers, quality assurance experts, and customer representatives in a software project. It encourages teams ...
*
Test automation
In software testing, test automation is the use of software separate from the software being tested to control the execution of tests and the comparison of actual outcomes with predicted outcomes. Test automation can automate some repetitive bu ...
*
HtmlUnit
HtmlUnit is a headless web browser written in Java. It allows high-level manipulation of websites from other Java code, including filling and submitting forms and clicking hyperlinks. It also provides access to the structure and the details wit ...
*
List of web testing tools
*
Regression testing
Regression testing (rarely, ''non-regression testing'') is re-running functional and non-functional tests to ensure that previously developed and tested software still performs as expected after a change. If not, that would be called a ''regres ...
*
Given-When-Then
Given-When-Then (GWT) is a semi-structured way to write down test cases. They can either be tested manually or automated as browser tests with tools like Selenium and Cucumber.
It derives its name from the three clauses used, which start with the ...
References
Software testing tools
Software using the MIT license
Graphical user interface testing
Load testing tools
Unit testing frameworks
Web development software
Web scraping
Free software programmed in Ruby