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 end user or user of a system, and may be recorded on index ...
and automates
web application A web application (or web app) is application software that is accessed using a web browser. Web applications are delivered on the World Wide Web to users with an active network connection. History In earlier computing models like client-serve ...
testing for behavior-driven software development. It is written in the
Ruby programming language Ruby is an interpreted, high-level, general-purpose programming language which supports multiple programming paradigms. It was designed with an emphasis on programming productivity and simplicity. In Ruby, everything is an object, including pr ...
. Capybara can mimic actions of real users interacting with web-based applications. It can receive pages, parse the
HTML The HyperText Markup Language or HTML is the standard markup language for documents designed to be displayed in a web browser. It can be assisted by technologies such as Cascading Style Sheets (CSS) and scripting languages such as JavaScri ...
and submit
forms Form is the shape, visual appearance, or configuration of an object. In a wider sense, the form is the way something happens. Form also refers to: *Form (document), a document (printed or electronic) with spaces in which to write or enter data * ...
.


Background and motivation

During the software development process (especially in the Agile and
Test-driven Development Test-driven development (TDD) is a software development process relying on software requirements being converted to test cases before software is fully developed, and tracking all software development by repeatedly testing the software against al ...
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 In software engineering, behavior-driven development (BDD) is an agile software development process that encourages collaboration among developers, quality assurance experts, and customer representatives in a software project. It encourages teams ...
style of frameworks such as
Cucumber Cucumber (''Cucumis sativus'') is a widely-cultivated Vine#Horticultural climbing plants, creeping vine plant in the Cucurbitaceae family that bears usually cylindrical Fruit, fruits, which are used as culinary vegetables.
and
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 A gemstone (also called a fine gem, jewel, precious stone, or semiprecious stone) is a piece of mineral crystal which, in cut and polished form, is used to make jewelry or other adornments. However, certain rocks (such as lapis lazuli, opal, a ...
) 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 dig ...
(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 Dom or DOM may refer to: People and fictional characters * Dom (given name), including fictional characters * Dom (surname) * Dom La Nena (born 1989), stage name of Brazilian-born cellist, singer and songwriter Dominique Pinto * Dom people, an et ...
(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 that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of Website, websites use JavaScript on the Client (computing), client side ...
. 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 Rack or racks may refer to: Storage and installation * Amp rack, short for amplifier rack, a piece of furniture in which amplifiers are mounted * Bicycle rack, a frame for storing bicycles when not in use * Bustle rack, a type of storage bin ...
interfaces. Consequently, it can only be used for Rack applications.


Selenium

Selenium Selenium is a chemical element with the symbol Se and atomic number 34. It is a nonmetal (more rarely considered a metalloid) with properties that are intermediate between the elements above and below in the periodic table, sulfur and tellurium, ...
-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 A headless browser is a web browser without a graphical user interface. Headless browsers provide automated control of a web page in an environment similar to popular web browsers, but they are executed via a command-line interface or using netwo ...
testing with JavaScript support. It uses
QtWebKit WebKit is a browser engine developed by Apple Inc., Apple and primarily used in its Safari (web browser), Safari web browser, as well as on the iOS and iPadOS version of any web browser. WebKit is also used by the BlackBerry Browser, PlayStation ...
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 f ...
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) and can be used to compute values (e.g., strings, numbers, or Boolean v ...
/ 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 pr ...
*
Selenium (software) Selenium is an open source umbrella project for a range of tools and libraries aimed at supporting browser automation. It provides a playback tool for authoring functional tests across most modern web browsers, without the need to learn a te ...


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 en ...
*
Acceptance test-driven development Acceptance test–driven development (ATDD) is a development methodology based on communication between the business customers, the developers, and the testers. ATDD encompasses many of the same practices as specification by example (SBE), behavio ...
*
Behavior-driven development In software engineering, behavior-driven development (BDD) is an agile software development process that encourages collaboration among developers, quality assurance experts, and customer representatives in a software project. It encourages teams ...
*
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 HtmlUnit is a headless web browser written in Java. It allows high-level manipulation of websites from other Java code, including filling and submitting forms and clicking hyperlinks. It also provides access to the structure and the details with ...
* List of web testing tools *
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 '' regre ...
*
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