Capybara | |
Developer: | Thomas Walpole |
Programming Language: | Ruby |
Genre: | Behavior driven development framework / Test tool |
License: | MIT License |
Operating System: | Cross-platform |
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 written in the Ruby programming language.
Capybara can mimic actions of real users interacting with web-based applications. It can receive pages, parse the HTML and submit forms.[1]
During 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.
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]
Capybara is a Ruby library (also referred to as a gem) that is used with an underlying web-based driver. It consists of a user-friendly DSL (Domain Specific Language) which 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 attempt to locate the relevant element in the DOM (Document Object Model) and execute an action such as click button, link, etc.
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.
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-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 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.
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:
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.
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.[4]
An example of a Capybara feature used with Cucumber:
Some minute integration is required in order to use Capybara with RSpec[5]