capybara with rspec

17
Capybara by:- Omnia Gamal El-Deen email :- [email protected] twitter @Omnia_G

Upload: omnia-helmi

Post on 10-May-2015

7.003 views

Category:

Technology


1 download

DESCRIPTION

Learning how to use capybara and capybara-webkit with rspec. Example :- https://github.com/OmniaGM/learn-capybara

TRANSCRIPT

Page 1: Capybara with Rspec

Capybara

by:- Omnia Gamal El-Deen email :- [email protected] twitter @Omnia_G

Page 2: Capybara with Rspec

Capybara

Go away I'm busy NOW

Page 3: Capybara with Rspec
Page 4: Capybara with Rspec

Why Capybara

Page 5: Capybara with Rspec

Setup

Install :sudo gem install capybara

Or Gemfilegem "capybara", :group => :test

Page 6: Capybara with Rspec

Using Capybara with RSpec

● spec/spec_helper.rbrequire 'capybara/rails'require 'capybara/rspec'

● create test directory /spec/requests● add capybara dsl to RSpec.configure in

spec_helper.rbRSpec.configure do |config| config.include Capybara::DSL, :type => :request

Page 7: Capybara with Rspec

Test

test_file.rb require 'spec_helper'describe "home page" do

before :each do # ...... end

it "displays greeting" do # ......... end

end

Page 8: Capybara with Rspec

The DSL

● navigating (visit)● matcher (page.should)● clicks (click_link - click_button)● action (fill_in) ● finders (find)● scope (within)● Scripting (page.execute_script)● debugger (page.htm - save_and_open_page)

○ NOTE : You'll need to install launchy (gem 'launchy', :

group => :test)and make sure it's available to open pages with save_and_open_page

Page 9: Capybara with Rspec

Example

Page 10: Capybara with Rspec

● Driver agnostic● Support :

○ RackTest○ Selenium○ Capybara-webkit

● Default: rack_test driver

Drivers

Page 11: Capybara with Rspec

RackTest

● It's pure Ruby, So it interacts directly with Rack interfaces

● It does not require a server to be startedBut● Not used to test a remote application, or to access

remote URLs (e.g., redirects to external sites, external APIs, or OAuth services)

● Dumb● It's not have any support for executing JavaScript

Page 12: Capybara with Rspec

Selenium

● Runs tests in a real browser● Supports any Javascript your browser supports, just like

real users● Easy to set up with capybaraBut● Slow● GUI browser adds a lot of cruft you don’t want ● Unfriendly

○ No console.log output○ invisible javascript errors

Page 13: Capybara with Rspec

● Fast● No browser UI● Using webkit engine● console.log output● Errors in standard output

CAPYBARA-WEBKIT

Page 14: Capybara with Rspec

Setup capybara-webkit:

Gemfilegem "capybara-webkit", :group => :test

○ You didn't need capybara itself anymore○ You will need database_cleaner because database transactions aren’

t compatible with rspec drivers besides Rack::Testgem 'database_cleaner', :group => :test

Page 15: Capybara with Rspec

Spec_helper.rbENV["RAILS_ENV"] ||= 'test'require File.expand_path("../../config/environment", __FILE__)require 'rspec/rails'require 'capybara/rspec'require 'capybara-webkit'Capybara.javascript_driver = :webkit #...

Page 16: Capybara with Rspec

DatabaseCleaner configuration

RSpec.configure do |config|#...

config.use_transactional_fixtures = false config.before(:suite) do

DatabaseCleaner.strategy = :truncation end

config.before(:each) do DatabaseCleaner.start end

config.after(:each) do DatabaseCleaner.clean endend