Capybara with Rspec

Capybara with Rspec

Welcome to GKTCS Innovations Pvt. Ltd. Corporate Training, Online Training and Consultancy Surendra Panpaliya Director, GKTCS Innovations Pvt. Ltd, Pune. 18+ Years of Experience ( MCA, PGDCS, BSc. [Electronics] , CCNA) Director , GKTCS Innovations Pvt. Ltd. Pune [ Nov 2009 – Till date ] IT Manager and Consultant at Rolta India Ltd, Mumbai [ April 2007 – Oct 2009 ] Associate Professor at Sinhgad Institute , Pune [Jan 2002 – April 2007] Instructor , ITM Bangalore [ May 2000 – Jan 2002 ] Project Trainee, DSYS Software Group, Bangalore [ Jan 1999 to April 2000] Skills ❑ Ruby, Rail,Python, Jython, Django, Android , PHP, LAMP ❑ Data Communication & Networking, CCNA ❑ UNIX /Linux Shell Scripting, System Programming ❑ CA Siteminder, Autosys, SSO, Service Desk, Service Delivery Author of 4 Books National Paper Presentation Awards at BARC Mumbai 2 Agenda • Introduction to Capybara • Capybara installation • Environment setup Introduction to 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 a part of the Cucumber testing framework written in the Ruby programming language that simulates various aspects of a web browser from the perspective of a real user. Introduction to Capybara ◆ Background and Motivation ◆ 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 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. Introduction to Capybara ◆ 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. ◆ Drivers ◆ By default, Capybara uses the :rack_test driver which does not have any support for executing JavaScript. Driver can be switched in Before and After blocks. Some of the web drivers supported by Capybara are mentioned below. Introduction to Capybara ◆ 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. ◆ Capybara-webkit ◆ Capybara-webkit driver (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. Introduction to Capybara ◆ Matchers ◆ Capybara finds 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. 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 => '[email protected]' ◆ fill_in 'Password', :with => 'userpassword' ◆ fill_in 'Password Confirmation', :with => 'userpassword' ◆ click_button 'Register' ◆ page.should have content 'Welcome' ◆ end ◆ end User-Registration Process ◆ 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 ◆ describe 'go to home page' do ◆ it 'opens the home page' do ◆ visit (get_homepage) ◆ page.should have_content('Welcome') ◆ end ◆ end Capybara Installation ◆ Surendras-MacBook-Pro:capybara SurendraMac$ gem install capybara ◆ Fetching: addressable-2.4.0.gem (100%) ◆ Successfully installed addressable-2.4.0 ◆ Fetching: capybara-2.7.1.gem (100%) ◆ Successfully installed capybara-2.7.1 ◆ Parsing documentation for addressable-2.4.0 ◆ Installing ri documentation for addressable-2.4.0 ◆ Parsing documentation for capybara-2.7.1 ◆ Installing ri documentation for capybara-2.7.1 ◆ Done installing documentation for addressable, capybara after 3 seconds ◆ 2 gems installed Cucumber-rails Installation ◆ Surendras-MacBook-Pro:capybara SurendraMac$ gem install cucumber-rails ◆ Fetching: cucumber-rails-1.4.3.gem (100%) ◆ Successfully installed cucumber-rails-1.4.3 ◆ Parsing documentation for cucumber-rails-1.4.3 ◆ Installing ri documentation for cucumber-rails-1.4.3 ◆ Done installing documentation for cucumber-rails after 0 seconds ◆ 1 gem installed ◆ Surendras-MacBook-Pro:capybara SurendraMac$ pwd ◆ /Users/SurendraMac/capybara Cucumber - - init ◆ Surendras-MacBook-Pro:capybara SurendraMac$ cucumber --init ◆ create features ◆ create features/step_definitions ◆ create features/support ◆ create features/support/env.rb ◆ Surendras-MacBook-Pro:capybara SurendraMac$ ls ◆ features ◆ Surendras-MacBook-Pro:capybara SurendraMac$ cd features/ ◆ Surendras-MacBook-Pro:features SurendraMac$ ls ◆ step_definitions support ◆ Surendras-MacBook-Pro:features SurendraMac$ ls ◆ step_definitions support youtube_search.feature Cucumber YouTube.feature ◆ Feature: Search for Videos on YouTube ◆ Scenario: Search for Videos of Large Rodents ◆ Given I am on the YouTube home page ◆ When I search for "capybara" ◆ Then videos of capybara are returned Cucumber Test ◆ Surendras-MacBook-Pro:capybara SurendraMac$ cucumber ◆ Feature: Search for Videos on YouTube ◆ Scenario: Search for Videos of Large Rodents # features/ youtube_search.feature:2 ◆ Given I am on the YouTube home page # features/ youtube_search.feature:3 ◆ When I search for "capybara" # features/youtube_search.feature:4 ◆ Then videos of Capybara are returned # features/youtube_search.feature: 5 ◆ 1 scenario (1 undefined) ◆ 3 steps (3 undefined) ◆ 0m0.025s Cucumber steps ◆ You can implement step definitions for undefined steps with these snippets: ◆ Given(/^I am on the YouTube home page$/) do ◆ pending # Write code here that turns the phrase above into concrete actions ◆ end ◆ When(/^I search for "([^"]*)"$/) do |arg1| ◆ pending # Write code here that turns the phrase above into concrete actions ◆ end ◆ Then(/^videos of Capybara are returned$/) do ◆ pending # Write code here that turns the phrase above into concrete actions ◆ end Cucumber Test ◆ Surendras-MacBook-Pro:capybara SurendraMac$ cucumber ◆ Feature: Search for Videos on YouTube ◆ Scenario: Search for Videos of Large Rodents # features/youtube_search.feature:2 ◆ Given I am on the YouTube home page # features/step_definitions/steps.rb:2 ◆ TODO (Cucumber::Pending) ◆ ./features/step_definitions/steps.rb:3:in `/^I am on the YouTube home page$/' ◆ features/youtube_search.feature:3:in `Given I am on the YouTube home page' ◆ When I search for "capybara" # features/step_definitions/steps.rb:6 ◆ Then videos of Capybara are returned # features/step_definitions/steps.rb:10 ◆ 1 scenario (1 pending) ◆ 3 steps (2 skipped, 1 pending) ◆ 0m0.024s Support/env.rb ◆ Add following lines ◆ support/env.rb ◆ require 'capybara/cucumber' ◆ Capybara.default_driver = :selenium ◆ Cucumber steps Modification ◆ Given(/^I am on the YouTube home page$/) do ◆ visit 'http://www.youtube.com' ◆ end ◆ When(/^I search for "([^"]*)"$/) do |arg1| ◆ pending # Write code here that turns the phrase above into concrete actions ◆ end ◆ Then(/^videos of Capybara are returned$/) do ◆ pending # Write code here that turns the phrase above into concrete actions ◆ end Cucumber Test ◆ Surendras-MacBook-Pro:capybara SurendraMac$ cucumber ◆ Feature: Search for Videos on YouTube ◆ Scenario: Search for Videos of Large Rodents # features/youtube_search.feature:2 ◆ Given I am on the YouTube home page # features/step_definitions/steps.rb:2 ◆ unable to obtain stable firefox connection in 60 seconds

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    86 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us