Capybara is a web-based test automation software that simulates scenarios for user stories and automata 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.
In the course of the software development process especially in the Agile and Test-driven Development environments, the size of the tests increases, it becomes difficult to manage tests which are complex and not modular. 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.
Capybara is a library / gem built to be used on the 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. When the page is loaded using the DSL (and underlying web driver), Capybara will try to locate the relevant DOM in the Document Object Model (DOM) and execute the action, such as click button, link, etc.
By default, Capybara uses the JavaScript driver. Driver can be switched in Before and After blocks. Some of the web drivers are supported by Capybara are mentioned below.
Written in Ruby, Capybara’s default RackTest driver does not need to get started with Rack interfaces. Therefore, it can only be used for Rack applications.
Selenium-webdriver, which is mostly used in web-based automation frameworks, is supported by Capybara. Unlike Capybara’s default driver, it supports JavaScript, which can be used for CI scenarios.
Capybara-webkit driver (gem) is used for JavaScript. It uses QtWebKit and it is significantly faster than Selenium as it does not load the entire browser.
Capybara finds an element using Domain-specific language or XPath / CSS Selectors. Partial matches can lead to unexpected results. 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. One 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 one more than one match is found. If no element is found, a new search for inaccurate matches is started. Again, an ambiguous exception is raised. prefer_exact: Finds all matching.
Here is an example of how to use test is done using Capybara. There is a test to see if the user can continue with the registration 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.
Some minutes integration is required in order to use Capybara with RSpec