SlideShare une entreprise Scribd logo
1  sur  55
Télécharger pour lire hors ligne
Acceptance Testing
   with Webrat
         Luismi Cavallé

       http://twitter.com/cavalle
          http://lmcavalle.com
    http://spainrb.org/luismi-cavalle
Demo
Internals
Webrat

#get   #post      #head       #delete    ...

#put   #request           #follow_redirect!



   ActionController::                            Nokogiri
     Integration::
        Session
Rails Integration Test
test "Hotel creation" do
  get "/hotels"

 assert_select "body", :text => /Ritz/, :count => 0

 get "/hotels/new"

 post_via_redirect "/hotels",
                   :hotel => { :name => "Ritz" }

  assert_response :success
  assert_select "body", /Hotel was successfully created/
  assert_select "body", /Ritz/
end
Webrat

#get   #post      #head       #delete    ...

#put   #request           #follow_redirect!



                                               Hpricot + REXML
         Rack::Test
click_link


def click_link(text_or_title_or_id, options = {})
  find_link(text_or_title_or_id).click(options)
end
click_link


def find_link(text_or_title_or_id) #:nodoc:
  LinkLocator.new(@session, dom, text_or_title_or_id).locate!
end
click_link


def locate
  Link.load(@session, link_element)
end
click_link


def link_element
  matching_links.min { |a, b|
Webrat::XML.all_inner_text(a).length <=>
Webrat::XML.all_inner_text(b).length }
end
click_link


def matching_links
  @matching_links ||= link_elements.select do |link_element|
    matches_text?(link_element) ||
    matches_id?(link_element)
  end
end
click_link


def link_elements
  Webrat::XML.xpath_search(@dom, *Link.xpath_search)
end
click_link


def self.xpath_search
  ".//a[@href]"
end
click_link

def self.xpath_search(element, *searches)
  searches.flatten.map do |search|
    if Webrat.configuration.parse_with_nokogiri?
      element.xpath(search)
    else
      REXML::XPath.match(element, search)
    end
  end.flatten.compact
end
click_link


def matching_links
  @matching_links ||= link_elements.select do |link_element|
    matches_text?(link_element) ||
    matches_id?(link_element)
  end
end
click_link

def matches_text?(link)
  if @value.is_a?(Regexp)
    matcher = @value
  else
    matcher = /#{Regexp.escape(@value.to_s)}/i
  end

  replace_nbsp(Webrat::XML.all_inner_text(link)) =~ matcher ||
  replace_nbsp_ref(Webrat::XML.inner_html(link)) =~ matcher ||
  Webrat::XML.attribute(link, "title")=~ matcher
end
click_link


def matches_id?(link)
  if @value.is_a?(Regexp)
    (Webrat::XML.attribute(link, "id") =~ @value) ? true : false
  else
    (Webrat::XML.attribute(link, "id") == @value) ? true : false
  end
end
click_link


def link_element
  matching_links.min { |a, b|
Webrat::XML.all_inner_text(a).length <=>
Webrat::XML.all_inner_text(b).length }
end
click_link


def locate
  Link.load(@session, link_element)
end
click_link


def click_link(text_or_title_or_id, options = {})
  find_link(text_or_title_or_id).click(options)
end
click_link

def click(options = {})
  method = options[:method] || http_method
  return if href =~ /^#/ && method == :get

 options[:javascript] = true if options[:javascript].nil?

  if options[:javascript]
    @session.request_page(absolute_href, method, data)
  else
    @session.request_page(absolute_href, :get, {})
  end
end
API
Navigation

visit "/hotels/new"

visit hotels_path
Navigation


click_link "Create a new hotel"
Navigation


click_area 'Madrid'
Forms


select "Canada", :from => "Country"

select "Canada"
Forms
Forms
select_date "April 26, 1982", :from => "Birthday"
select_date Date.today


select_datetime 2.days.ago
select_datetime "April 26, 1982 7:00PM", :from => "Event"


select_time "22:30"
select_time "1:30AM", :from => "Arrival time"
Forms
select_date "April 26, 1982", :from => "Birthday"
select_date Date.today


select_datetime 2.days.ago
select_datetime "April 26, 1982 7:00PM", :from => "Event"


select_time "22:30"
select_time "1:30AM", :from => "Arrival time"
Forms
select_date "April 26, 1982", :from => "Birthday"
select_date Date.today


select_datetime 2.days.ago
select_datetime "April 26, 1982 7:00PM", :from => "Event"


select_time "22:30"
select_time "1:30AM", :from => "Arrival time"
Forms
select_date "April 26, 1982", :from => "Birthday"
select_date Date.today


select_datetime 2.days.ago
select_datetime "April 26, 1982 7:00PM", :from => "Event"


select_time "22:30"
select_time "1:30AM", :from => "Arrival time"
Forms
select_date "April 26, 1982", :from => "Birthday"
select_date Date.today


select_datetime 2.days.ago
select_datetime "April 26, 1982 7:00PM", :from => "Event"


select_time "22:30"
select_time "1:30AM", :from => "Arrival time"
Forms
select_date "April 26, 1982", :from => "Birthday"
select_date Date.today


select_datetime 2.days.ago
select_datetime "April 26, 1982 7:00PM", :from => "Event"


select_time "22:30"
select_time "1:30AM", :from => "Arrival time"
Forms
select_date "April 26, 1982", :from => "Birthday"
select_date Date.today


select_datetime 2.days.ago
select_datetime "April 26, 1982 7:00PM", :from => "Event"


select_time "22:30"
select_time "1:30AM", :from => "Arrival time"
Forms
select_date "April 26, 1982", :from => "Birthday"
select_date Date.today


select_datetime 2.days.ago
select_datetime "April 26, 1982 7:00PM", :from => "Event"


select_time "22:30"
select_time "1:30AM", :from => "Arrival time"
Forms


attach_file "Brochure", "/path/nh_cornella.pdf"

attach_file "Photo", "/path/front.jpg", "image/jpg"
Forms


check 'Remember me'

uncheck 'You can send me spam'
Forms


fill_in "Email", :with => "user@example.com"

fill_in "user[email]", :with => "user@example.com"
Forms


click_button "Login"

click_button
And more...


basic_auth "admin", "password"

save_and_open_page
And more...


within "#project_1" do
  click_link "Destroy"
end
Matchers

assert_contain "Hotel was successfully created"



assert_not_contain(/You have booked for d+ nights/)
Matchers

assert_have_selector "div#hotel_1",
                     :content => "Sol Meliá Berlín"

assert_have_selector ".bookings", :count => 5

assert_have_no_selector "a:enabled",
                        :href => "http://google.com"
Matchers

assert_have_xpath "//div[@id = 'hotel_1']",
                  :content => "Sol Meliá Berlín"

assert_have_xpath "//*[@class = 'bookings']",
                  :count => 5

assert_have_no_xpath "//a[enabled(.)]",
                     :href => "http://google.com"
Matchers

response.should contain(/You have booked for d+ nights/)



response.should_not have_selector(".bookings", :count => 5)
Matchers

response.should have_selector(".hotels") do |hotels|
  hotels.should have_selector("h2", :content => "Hotels")
  hotels.should have_xpath("//div[@id = 'hotel_1']",
                           :content => "Ritz")
end
Configuration
Webrat.configure do |config|
  config.parse_with_nokogiri = true
  config.infinite_redirect_limit = 10
  config.open_error_files = true
end
Webrat.configure do |config|
  config.mode = :rails | :merb | :sinatra |
                :rack_test |
                :mechanize | :selenium
end
TDD Benefits
★ Driving the design and
  development


★ Building a suite of automated
  regression tests
http://www.flickr.com/photos/icopythat/4375608/
Gracias!
     http://wiki.github.com/brynary/webrat
http://gitrdoc.com/brynary/webrat/tree/master/


          http://twitter.com/cavalle
             http://lmcavalle.com
       http://spainrb.org/luismi-cavalle

Contenu connexe

Tendances

Building @Anywhere (for TXJS)
Building @Anywhere (for TXJS)Building @Anywhere (for TXJS)
Building @Anywhere (for TXJS)
danwrong
 
jQuery and Rails, Sitting in a Tree
jQuery and Rails, Sitting in a TreejQuery and Rails, Sitting in a Tree
jQuery and Rails, Sitting in a Tree
adamlogic
 

Tendances (20)

jQuery in 15 minutes
jQuery in 15 minutesjQuery in 15 minutes
jQuery in 15 minutes
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuery
 
Polymer - pleasant client-side programming with web components
Polymer - pleasant client-side programming with web componentsPolymer - pleasant client-side programming with web components
Polymer - pleasant client-side programming with web components
 
Emmet cheat-sheet
Emmet cheat-sheetEmmet cheat-sheet
Emmet cheat-sheet
 
Prototype & jQuery
Prototype & jQueryPrototype & jQuery
Prototype & jQuery
 
jQuery from the very beginning
jQuery from the very beginningjQuery from the very beginning
jQuery from the very beginning
 
DOM Scripting Toolkit - jQuery
DOM Scripting Toolkit - jQueryDOM Scripting Toolkit - jQuery
DOM Scripting Toolkit - jQuery
 
Learning jQuery in 30 minutes
Learning jQuery in 30 minutesLearning jQuery in 30 minutes
Learning jQuery in 30 minutes
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
 
Jquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript BasicsJquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript Basics
 
JQuery introduction
JQuery introductionJQuery introduction
JQuery introduction
 
Convidar para page !!
Convidar para page !!Convidar para page !!
Convidar para page !!
 
jQuery Loves Developers - Oredev 2009
jQuery Loves Developers - Oredev 2009jQuery Loves Developers - Oredev 2009
jQuery Loves Developers - Oredev 2009
 
jQuery Essentials
jQuery EssentialsjQuery Essentials
jQuery Essentials
 
SenchaCon 2016: Want to Use Ext JS Components with Angular 2? Here’s How to I...
SenchaCon 2016: Want to Use Ext JS Components with Angular 2? Here’s How to I...SenchaCon 2016: Want to Use Ext JS Components with Angular 2? Here’s How to I...
SenchaCon 2016: Want to Use Ext JS Components with Angular 2? Here’s How to I...
 
Building @Anywhere (for TXJS)
Building @Anywhere (for TXJS)Building @Anywhere (for TXJS)
Building @Anywhere (for TXJS)
 
jQuery quick tuts
jQuery quick tutsjQuery quick tuts
jQuery quick tuts
 
jQuery
jQueryjQuery
jQuery
 
jQuery and Rails, Sitting in a Tree
jQuery and Rails, Sitting in a TreejQuery and Rails, Sitting in a Tree
jQuery and Rails, Sitting in a Tree
 
Learning jQuery made exciting in an interactive session by one of our team me...
Learning jQuery made exciting in an interactive session by one of our team me...Learning jQuery made exciting in an interactive session by one of our team me...
Learning jQuery made exciting in an interactive session by one of our team me...
 

En vedette

En vedette (6)

BDD de fuera a dentro
BDD de fuera a dentroBDD de fuera a dentro
BDD de fuera a dentro
 
BDD de Aceptación con Ruby
BDD de Aceptación con RubyBDD de Aceptación con Ruby
BDD de Aceptación con Ruby
 
Fighting Bottlencks with CQRS - ResearchGate
Fighting Bottlencks with CQRS - ResearchGateFighting Bottlencks with CQRS - ResearchGate
Fighting Bottlencks with CQRS - ResearchGate
 
The CQRS diet
The CQRS dietThe CQRS diet
The CQRS diet
 
From ActiveRecord to EventSourcing
From ActiveRecord to EventSourcingFrom ActiveRecord to EventSourcing
From ActiveRecord to EventSourcing
 
Towards Modelling Processes
Towards Modelling ProcessesTowards Modelling Processes
Towards Modelling Processes
 

Similaire à Acceptance Testing with Webrat

Action View Form Helpers - 1, Season 2
Action View Form Helpers - 1, Season 2Action View Form Helpers - 1, Season 2
Action View Form Helpers - 1, Season 2
RORLAB
 
Big Data for each one of us
Big Data for each one of usBig Data for each one of us
Big Data for each one of us
OSCON Byrum
 
Rails 3: Dashing to the Finish
Rails 3: Dashing to the FinishRails 3: Dashing to the Finish
Rails 3: Dashing to the Finish
Yehuda Katz
 
Rails 3 overview
Rails 3 overviewRails 3 overview
Rails 3 overview
Yehuda Katz
 
Javascript MVC & Backbone Tips & Tricks
Javascript MVC & Backbone Tips & TricksJavascript MVC & Backbone Tips & Tricks
Javascript MVC & Backbone Tips & Tricks
Hjörtur Hilmarsson
 
#NewMeetup Performance
#NewMeetup Performance#NewMeetup Performance
#NewMeetup Performance
Justin Cataldo
 
前端MVC 豆瓣说
前端MVC 豆瓣说前端MVC 豆瓣说
前端MVC 豆瓣说
Ting Lv
 
Jarv.us Showcase — SenchaCon 2011
Jarv.us Showcase — SenchaCon 2011Jarv.us Showcase — SenchaCon 2011
Jarv.us Showcase — SenchaCon 2011
Chris Alfano
 

Similaire à Acceptance Testing with Webrat (20)

Action View Form Helpers - 1, Season 2
Action View Form Helpers - 1, Season 2Action View Form Helpers - 1, Season 2
Action View Form Helpers - 1, Season 2
 
Big Data for each one of us
Big Data for each one of usBig Data for each one of us
Big Data for each one of us
 
Rails GUI Development with Ext JS
Rails GUI Development with Ext JSRails GUI Development with Ext JS
Rails GUI Development with Ext JS
 
Rails 3: Dashing to the Finish
Rails 3: Dashing to the FinishRails 3: Dashing to the Finish
Rails 3: Dashing to the Finish
 
Rails 3 overview
Rails 3 overviewRails 3 overview
Rails 3 overview
 
jQuery
jQueryjQuery
jQuery
 
Secrets of JavaScript Libraries
Secrets of JavaScript LibrariesSecrets of JavaScript Libraries
Secrets of JavaScript Libraries
 
RubyBarCamp “Полезные gems и plugins”
RubyBarCamp “Полезные gems и plugins”RubyBarCamp “Полезные gems и plugins”
RubyBarCamp “Полезные gems и plugins”
 
Javascript MVC & Backbone Tips & Tricks
Javascript MVC & Backbone Tips & TricksJavascript MVC & Backbone Tips & Tricks
Javascript MVC & Backbone Tips & Tricks
 
Protractor Training in Pune by QuickITDotnet
Protractor Training in Pune by QuickITDotnet Protractor Training in Pune by QuickITDotnet
Protractor Training in Pune by QuickITDotnet
 
Protractor Training - Online training On Skype
Protractor Training - Online training On Skype Protractor Training - Online training On Skype
Protractor Training - Online training On Skype
 
JQuery Flot
JQuery FlotJQuery Flot
JQuery Flot
 
The Ring programming language version 1.8 book - Part 50 of 202
The Ring programming language version 1.8 book - Part 50 of 202The Ring programming language version 1.8 book - Part 50 of 202
The Ring programming language version 1.8 book - Part 50 of 202
 
Cheap frontend tricks
Cheap frontend tricksCheap frontend tricks
Cheap frontend tricks
 
Django - Framework web para perfeccionistas com prazos
Django - Framework web para perfeccionistas com prazosDjango - Framework web para perfeccionistas com prazos
Django - Framework web para perfeccionistas com prazos
 
Aplicacoes dinamicas Rails com Backbone
Aplicacoes dinamicas Rails com BackboneAplicacoes dinamicas Rails com Backbone
Aplicacoes dinamicas Rails com Backbone
 
#NewMeetup Performance
#NewMeetup Performance#NewMeetup Performance
#NewMeetup Performance
 
前端MVC 豆瓣说
前端MVC 豆瓣说前端MVC 豆瓣说
前端MVC 豆瓣说
 
Django quickstart
Django quickstartDjango quickstart
Django quickstart
 
Jarv.us Showcase — SenchaCon 2011
Jarv.us Showcase — SenchaCon 2011Jarv.us Showcase — SenchaCon 2011
Jarv.us Showcase — SenchaCon 2011
 

Dernier

Dernier (20)

Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 

Acceptance Testing with Webrat