Acceptance Testing with Webrat
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Acceptance Testing with Webrat

on

  • 7,069 views

Presented at Madrid-rb on June 25th, 2009

Presented at Madrid-rb on June 25th, 2009

Statistics

Views

Total Views
7,069
Views on SlideShare
7,018
Embed Views
51

Actions

Likes
9
Downloads
71
Comments
0

4 Embeds 51

http://www.slideshare.net 42
https://www.linkedin.com 4
http://www.linkedin.com 3
http://coderwall.com 2

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Acceptance Testing with Webrat Presentation Transcript

  • 1. Acceptance Testing with Webrat Luismi Cavallé http://twitter.com/cavalle http://lmcavalle.com http://spainrb.org/luismi-cavalle
  • 2. Demo
  • 3. Internals
  • 4. Webrat #get #post #head #delete ... #put #request #follow_redirect! ActionController:: Nokogiri Integration:: Session
  • 5. 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
  • 6. Webrat #get #post #head #delete ... #put #request #follow_redirect! Hpricot + REXML Rack::Test
  • 7. click_link def click_link(text_or_title_or_id, options = {}) find_link(text_or_title_or_id).click(options) end
  • 8. click_link def find_link(text_or_title_or_id) #:nodoc: LinkLocator.new(@session, dom, text_or_title_or_id).locate! end
  • 9. click_link def locate Link.load(@session, link_element) end
  • 10. 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
  • 11. click_link def matching_links @matching_links ||= link_elements.select do |link_element| matches_text?(link_element) || matches_id?(link_element) end end
  • 12. click_link def link_elements Webrat::XML.xpath_search(@dom, *Link.xpath_search) end
  • 13. click_link def self.xpath_search ".//a[@href]" end
  • 14. 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
  • 15. click_link def matching_links @matching_links ||= link_elements.select do |link_element| matches_text?(link_element) || matches_id?(link_element) end end
  • 16. 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
  • 17. 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
  • 18. 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
  • 19. click_link def locate Link.load(@session, link_element) end
  • 20. click_link def click_link(text_or_title_or_id, options = {}) find_link(text_or_title_or_id).click(options) end
  • 21. 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
  • 22. API
  • 23. Navigation visit "/hotels/new" visit hotels_path
  • 24. Navigation click_link "Create a new hotel"
  • 25. Navigation click_area 'Madrid'
  • 26. Forms select "Canada", :from => "Country" select "Canada"
  • 27. Forms
  • 28. 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"
  • 29. 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"
  • 30. 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"
  • 31. 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"
  • 32. 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"
  • 33. 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"
  • 34. 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"
  • 35. 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"
  • 36. Forms attach_file "Brochure", "/path/nh_cornella.pdf" attach_file "Photo", "/path/front.jpg", "image/jpg"
  • 37. Forms check 'Remember me' uncheck 'You can send me spam'
  • 38. Forms fill_in "Email", :with => "user@example.com" fill_in "user[email]", :with => "user@example.com"
  • 39. Forms click_button "Login" click_button
  • 40. And more... basic_auth "admin", "password" save_and_open_page
  • 41. And more... within "#project_1" do click_link "Destroy" end
  • 42. Matchers assert_contain "Hotel was successfully created" assert_not_contain(/You have booked for d+ nights/)
  • 43. 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"
  • 44. 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"
  • 45. Matchers response.should contain(/You have booked for d+ nights/) response.should_not have_selector(".bookings", :count => 5)
  • 46. 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
  • 47. Configuration
  • 48. Webrat.configure do |config| config.parse_with_nokogiri = true config.infinite_redirect_limit = 10 config.open_error_files = true end
  • 49. Webrat.configure do |config| config.mode = :rails | :merb | :sinatra | :rack_test | :mechanize | :selenium end
  • 50. TDD Benefits ★ Driving the design and development ★ Building a suite of automated regression tests
  • 51. http://www.flickr.com/photos/icopythat/4375608/
  • 52. 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