Capybara (Software)
Capybara (Software)
Capybara can mimic actions of real users interacting with web- Stable release 3.39.2[1] / 10
based applications. It can receive pages, parse the HTML and June 2023
submit forms.[2] Repository github.com
/teamcapybara
Background and motivation /capybara (http
s://github.com/t
During the software development process (especially in the Agile eamcapybara/
and Test-driven Development environments), as the size of the capybara)
tests increase, it becomes difficult to manage tests which are
Written in Ruby
complex and not modular.[2]
Operating system Cross-platform
By extending the human-readable behavior-driven development Type Behavior
style of frameworks such as Cucumber and RSpec into the driven
automation code itself, Capybara aims to develop simple web-
development
based automated tests.[3]
framework /
Test tool
Anatomy of Capybara License MIT License
Capybara is a Ruby library (also referred to as a gem) that is used Website teamcapybara
with an underlying web-based driver. It consists of a user-friendly .github.io
DSL (Domain Specific Language) which describe actions that are /capybara/ (htt
executed by the underlying web driver.[4] ps://teamcapyb
ara.github.io/c
When the page is loaded using the DSL (and underlying web apybara/)
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. 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
Capybara-webkit
Capybara-webkit driver (a gem) is used for true headless browser 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/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:[4]
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.[5]
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
Similar tools
Watir
Selenium (software)
See also
Free and open-
source software
portal
Acceptance testing
Acceptance test-driven development
Behavior-driven development
Test automation
HtmlUnit
List of web testing tools
Regression testing
Given-When-Then
References
1. "Release 3.39.2" (https://github.com/teamcapybara/capybara/releases/tag/3.39.2). 10 June
2023. Retrieved 29 June 2023.
2. Engineering Software as a Service: An Agile Approach Using Cloud Computing.
ISBN 9780984881246.
3. Application Testing with Capybara. ISBN 9781783281268.
4. "The Basics of Capybara and Improving Your Tests" (http://www.sitepoint.com/basics-capyb
ara-improving-tests/). SitePoint. Retrieved 2016-02-08.
5. "Integration testing Devise with RSpec and Capybara" (https://web.archive.org/web/201601
21204404/http://garyrafferty.com/2011/09/29/Testing-devise-with-rspec-and-capybara.html).
garyrafferty.com. Archived from the original (http://garyrafferty.com/2011/09/29/Testing-devise
-with-rspec-and-capybara.html) on 2016-01-21. Retrieved 2016-02-16.
6. Liss, Jo. "Capybara (and Selenium) with RSpec & Rails 3: quick tutorial" (http://www.opinion
atedprogrammer.com/2011/02/capybara-and-selenium-with-rspec-and-rails-3/).
www.opinionatedprogrammer.com. Retrieved 2016-02-08.