Capybara (software)
   HOME

TheInfoList



OR:

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 In software development and product management, a user story is an informal, natural language description of features of a software system. They are written from the perspective of an User (computing)#End user, end user or User (system), user o ...
and automates
web application A web application (or web app) is application software that is created with web technologies and runs via a web browser. Web applications emerged during the late 1990s and allowed for the server to dynamically build a response to the request, ...
testing for behavior-driven software development. It is written in the
Ruby programming language Ruby is a general-purpose programming language. It was designed with an emphasis on programming productivity and simplicity. In Ruby, everything is an object, including primitive data types. It was developed in the mid-1990s by Yukihiro "Mat ...
. Capybara can mimic actions of real users interacting with web-based applications. It can receive pages, parse the
HTML Hypertext Markup Language (HTML) is the standard markup language for documents designed to be displayed in a web browser. It defines the content and structure of web content. It is often assisted by technologies such as Cascading Style Sheets ( ...
and submit forms.


Background and motivation

During the software development process (especially in the Agile and
Test-driven Development Test-driven development (TDD) is a way of writing source code, code that involves writing an test automation, automated unit testing, unit-level test case that fails, then writing just enough code to make the test pass, then refactoring both the ...
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 Behavior-driven development (BDD) involves naming software tests using domain language to describe the behavior of the code. BDD involves use of a domain-specific language (DSL) using natural-language constructs (e.g., English-like sentences) ...
style of frameworks such as
Cucumber The cucumber (''Cucumis sativus'') is a widely-cultivated creeping vine plant in the family Cucurbitaceae that bears cylindrical to spherical 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 product ...
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) 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 di ...
(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 and core technology of the World Wide Web, alongside HTML and CSS. Ninety-nine percent of websites use JavaScript on the client side for webpage behavior. Web browsers have ...
. 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; it has symbol (chemistry), symbol Se and atomic number 34. It has various physical appearances, including a brick-red powder, a vitreous black solid, and a grey metallic-looking form. It seldom occurs in this elem ...
-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 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 A domain-specific language (DSL) is a computer language specialized to a particular application domain. This is in contrast to a general-purpose language (GPL), which is broadly applicable across domains. There are a wide variety of DSLs, ranging ...
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) in 1999, and can be used to compute values (e.g., strings, numbers, or ...
/ 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 => 'newuser@example.com' 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 product ...
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 Watir (Web Application Testing in Ruby, pronounced water), is an open-source family of Ruby libraries for automating web browsers. It drives Internet Explorer, Firefox, Chrome, Opera and Safari, and is available as a RubyGems gem. Watir was pri ...
* 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 ...
* Acceptance test-driven development *
Behavior-driven development Behavior-driven development (BDD) involves naming software tests using domain language to describe the behavior of the code. BDD involves use of a domain-specific language (DSL) using natural-language constructs (e.g., English-like sentences) ...
*
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 *
List of web testing tools This list of web testing tools gives a general overview of features of software used for web testing, and sometimes for web scraping. Main features Web testing tools may be classified based on different prerequisites that a user may require to ...
*
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 '' regr ...
*
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