Capybara (software)
Developer(s) | Jonas Nicklas |
---|---|
Stable release | 2.6.2 |
Written in | Ruby |
Operating system | Cross-platform |
Type | Behavior driven development framework / Test tool |
License | MIT License |
Website |
jnicklas |
Capybara is a web based test automation software that simulates scenarios for user stories and automates web application testing for behavior driven software development. It is a part of the Cucumber testing framework written in the Ruby programming language that simulates various aspects of a web browser from the perspective of a real user.
Pretending to be a user, it can interact with the application to receive pages, parse the HTML and submit the forms.[1]
Background and Motivation
In the course of the software development process especially in the Agile and Test-driven Development environments, as the size of the tests increase, it becomes difficult to manage tests which are complex and not modular.[1]
By extending the human-readable behavior-driven development style of frameworks such as Cucumber and RSpec into the automation code itself, Capybara aims to develop simple web-based automated tests.[2]
Anatomy of Capybara
Capybara is a library/gem built to be used on top of an underlying web-based driver. It offers a user-friendly DSL (Domain Specific Language) which is used to describe actions that are executed by the underlying web driver.[3]
When the page is loaded using the DSL (and underlying web driver), Capybara will try to locate the relevant element in the DOM (Document Object Model) and execute the action, such as click button, link, etc.
Some of the web drivers supported by Capybara are:
Drivers
By default, Capybara uses the:rack_test
driver, which does not have any support for executing JavaScript. Driver can be switched in Before and After blocks.
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-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 setup for testing in headless mode which is especially useful for CI scenarios.[3]
Capybara-webkit
Capybara-webkit driver (gem) is used for true headless testing with JavaScript support. It uses QtWebKit and it is significantly faster than selenium as it does not load the entire browser.
Matchers
Capybara finds 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:[3]
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 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.
Usages
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[3][4]
describe 'go to home page' do
it 'opens the home page' do
visit (get_homepage)
page.should have_content('Welcome')
end
end
Similar Tools
See also
- Acceptance testing
- Acceptance test-driven development
- Behavior-driven development
- Test automation
- HtmlUnit
- List of web testing tools
- Regression testing
- Given-When-Then
References
- 1 2 Engineering Software as a Service: An Agile Approach Using Cloud Computing. ISBN 9780984881246.
- ↑ Application Testing with Capybara. ISBN 9781783281268.
- 1 2 3 4 "The Basics of Capybara and Improving Your Tests". SitePoint. Retrieved 2016-02-08.
- ↑ Liss, Jo. "Capybara (and Selenium) with RSpec & Rails 3: quick tutorial". www.opinionatedprogrammer.com. Retrieved 2016-02-08.