Capybara (software)

Last updated
Capybara
Developer(s) Thomas Walpole
Stable release
3.40.0 [1]   OOjs UI icon edit-ltr-progressive.svg / 27 January 2024;4 months ago (27 January 2024)
Repository
Written in Ruby
Operating system Cross-platform
Type Behavior driven development framework / Test tool
License MIT License
Website teamcapybara.github.io/capybara/

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.

Contents

Capybara can mimic actions of real users interacting with web-based applications. It can receive pages, parse the HTML and submit forms. [2]

Background and motivation

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. [2]

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. [3]

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 (Domain Specific Language) which describe actions that are executed by the underlying web driver. [4]

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. 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-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. [4]

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 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: [4]

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. [5]

describe'UserRegistration'doit'allows a user to register'dovisitnew_user_registration_pathfill_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.shouldhave_content'Welcome'endend

Capybara with Cucumber

An example of a Capybara feature used with Cucumber:

When/^I want to add/dofill_in'a',:with=>100fill_in'b',:with=>100click_button'Add'end

Capybara with RSpec

Some minute integration is required in order to use Capybara with RSpec [4] [6]

describe'go to home page'doit'opens the home page'dovisit(get_homepage)expect(page).tohave_content('Welcome')endend

Similar tools

See also

Related Research Articles

<span class="mw-page-title-main">Acceptance testing</span> Test to determine if the requirements of a specification or contract are met

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.

<span class="mw-page-title-main">Software testing</span> Checking software against a standard

Software testing is the act of checking whether software satisfies expectations.

<span class="mw-page-title-main">Windows Forms</span> Graphical user interface software library

Windows Forms (WinForms) is a free and open-source graphical (GUI) class library included as a part of Microsoft .NET, .NET Framework or Mono, providing a platform to write client applications for desktop, laptop, and tablet PCs. While it is seen as a replacement for the earlier and more complex C++ based Microsoft Foundation Class Library, it does not offer a comparable paradigm and only acts as a platform for the user interface tier in a multi-tier solution.

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 but necessary tasks in a formalized testing process already in place, or perform additional testing that would be difficult to do manually. Test automation is critical for continuous delivery and continuous testing.

Microsoft Active Accessibility (MSAA) is an application programming interface (API) for user interface accessibility. MSAA was introduced as a platform add-on to Microsoft Windows 95 in 1997. MSAA is designed to help Assistive Technology (AT) products interact with standard and custom user interface (UI) elements of an application, as well as to access, identify, and manipulate an application's UI elements. AT products work with MSAA enabled applications in order to provide better access for individuals who have physical or cognitive difficulties, impairments, or disabilities. Some examples of AT products are screen readers for users with limited sight, on screen keyboards for users with limited physical access, or narrators for users with limited hearing. MSAA can also be used for automated testing tools, and computer-based training applications.

Behavior-driven development (BDD) involves naming software tests using domain language to describe the behavior of the code.

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 test scripting language. It also provides a test domain-specific language (Selenese) to write tests in a number of popular programming languages, including JavaScript (Node.js), C#, Groovy, Java, Perl, PHP, Python, Ruby and Scala. Selenium runs on Windows, Linux, and macOS. It is open-source software released under the Apache License 2.0.

<span class="mw-page-title-main">Watir</span>

Watir, 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 primarily developed by Bret Pettichord and Paul Rogers.

WET Web Tester is a web testing tool that drives an IE browser directly, so the automated testing done is equivalent to how a user would drive the web pages. The tool allows a user to perform all the operations required for testing web applications – like automatically clicking a link, entering text in a text field, clicking a button, etc. One may also perform various checks as a part of the testing process by using Checkpoints. The latest version of WET is 1.0.0.

QF-Test from Quality First Software is a cross-platform software tool for automated testing of programs via the graphical user interface. The program is specialized on cross-browser test automation of static and dynamic web-based applications. Version 4.1 added support for MacOS and the Apple Safari and Microsoft Edge browsers via the Selenium WebDriver. RESTful web service testing. From version 5.0, Windows applications can also be tested and modern C++ applications. Version 5.3 added support for the Chrome DevTools protocol, which allows browsers to be controlled using CDP drivers.

RSpec is a computer domain-specific language (DSL) 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 production applications. The basic idea behind this concept is that of test-driven development (TDD) where the tests are written first and the development is based on writing just enough code that will fulfill those tests followed by refactoring. It contains its own mocking framework that is fully integrated into the framework based upon JMock. The simplicity in the RSpec syntax makes it one of the popular testing tools for Ruby applications. The RSpec tool can be used by installing the rspec gem which consists of three other gems, namely rspec-core, rspec-expectation and rspec-mock.

<span class="mw-page-title-main">UFT One</span> Software testing automation tool

OpenText UFT One, formerly known as Micro Focus Unified Functional Testing and QuickTest Professional (QTP), is software that provides functional and regression test automation for software applications and environments.

Twist is a test automation and functional testing solution built by Thoughtworks Studios, the software division of ThoughtWorks. It uses Behavior Driven Development (BDD) and Test-driven development (TDD) for functional testing of the application. It is a part of the Adaptive ALM solution consisting of Twist for Agile testing by ThoughtWorks Studios, Mingle for Agile project management and Go for Agile release management.

<span class="mw-page-title-main">Robot Framework</span> Type of test automation framework

Robot Framework is a generic test automation framework for acceptance testing and acceptance test-driven development (ATDD). It is a keyword-driven testing framework that uses tabular test data syntax.

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), behavior-driven development (BDD), example-driven development (EDD), and support-driven development also called story test–driven development (SDD). All these processes aid developers and testers in understanding the customer's needs prior to implementation and allow customers to be able to converse in their own domain language.

A headless browser is a web browser without a graphical user interface.

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 within received web pages. HtmlUnit emulates parts of browser behaviour including the lower-level aspects of TCP/IP and HTTP. A sequence such as getPage(url), getLinkWith("Click here"), click allows a user to navigate through hypertext and obtain web pages that include HTML, JavaScript, Ajax and cookies. This headless browser can deal with HTTPS security, basic HTTP authentication, automatic page redirection and other HTTP headers. It allows Java test code to examine returned pages either as text, an XML DOM, or as collections of forms, tables, and links.

Factory Bot, originally known as Factory Girl, is a software library for the Ruby programming language that provides factory methods to create test fixtures for automated software testing. The fixture objects can be created on the fly; they may be plain Ruby objects with a predefined state, ORM objects with existing database records or mock objects.

Flood IO is a load testing platform that executes globally-distributed performance tests from open source tools, including JMeter, Gatling, and Selenium. It also runs test plans written with Ruby JMeter, an open source RubyGem.

<span class="mw-page-title-main">Katalon Studio</span> Automation testing software tool

Katalon Platform is an automation testing software tool developed by Katalon, Inc. The software is built on top of the open-source automation frameworks Selenium, Appium with a specialized IDE interface for web, API, mobile and desktop application testing. Its initial release for internal use was in January 2015. Its first public release was in September 2016. In 2018, the software acquired 9% of market penetration for UI test automation, according to The State of Testing 2018 Report by SmartBear.

References

  1. "Release 3.40.0". 27 January 2024. Retrieved 19 February 2024.
  2. 1 2 Fox, Armando; Patterson, David A. (2013). Engineering Software as a Service: An Agile Approach Using Cloud Computing. Strawberry Canyon LLC. ISBN   9780984881246.
  3. Application Testing with Capybara. ISBN   9781783281268.
  4. 1 2 3 4 "The Basics of Capybara and Improving Your Tests". SitePoint. 18 August 2014. Retrieved 2016-02-08.
  5. "Integration testing Devise with RSpec and Capybara". garyrafferty.com. Archived from the original on 2016-01-21. Retrieved 2016-02-16.
  6. Liss, Jo. "Capybara (and Selenium) with RSpec & Rails 3: quick tutorial". www.opinionatedprogrammer.com. Retrieved 2016-02-08.