User:Ketanjani21/sandbox

Capybara is a web based test automation software that simulates scenarios for user stories and helps us to automate  web application testing for  behavior driven software development. It is a part of Cucumber testing framework written in 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.

Background and Motivation
Software tests should be modular, easy to write and maintainable. They should provide intelligible and prompt feedback on the quality of code. In the course of 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 developing web-based simple automated tests, Capybara intends to address some of the above challenges.

Setup and Installation
To begin with, Capybara requires Ruby 1.9.3 or later. You add Capybara to your Gemfile and then do a bundle install. For testing a Ruby on Rails application, you can add the following to the test helper file: For testing a Rack (web server interface) application, you can set the following: For testing other frameworks such as JavaScript and related installation instructions, you can refer this

Capybara with Cucumber
For testing a Ruby on Rails application, just run the following instruction in the code block. The support for Capybara is built in, being a part of the Cucumber test framework. For using Capybara with Cucumber for applications outside rails, you have to load the requisite module as follows: An example of a Capybara feature used with Cucumber:

Tags
Tags are used to organise features and scenarios. When we want to run some specific scenarios with a driver that suppers a particular language say HTML, Capybara can be used to set up the tags, which we can use in Cucumber. In our example, We can tag the scenario with @HTML.

(add code block)

Anatomy of Capybara
Capybara is a library/gem built to be used on top of an underlying 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 element in the DOM (Document Object Model) and execute the action, such as click button, link, etc.

Some of the web drivers supported by Capybara are:

Drivers
By default, Capybara uses the   driver, which does not have any support for executing JavaScript. Capybara helps in setting up a default driver for our features. For example, to run Watir, we can write below mentioned line: To change the driver for short time, we can write:

It might not be possible to switch in the middle of a Scenario because every time we switch driver, a new session is created. Driver can be switched in Before and After blocks.

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.

RackTest can be configured with a set of headers like this:

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 setup for testing in headless mode which is especially useful for CI scenarios. Add below mentioned lines to setup usage of this driver:

Capybara-webkit
Capybara-webkit driver (gem) is used for true headless testing with JavaScript support. It uses QtWebKit and it is significantly faster than selenium as it does not load the entire browser. To setup usage of this driver, you need to add: