Acceptance Testing
   with Webrat
         Luismi Cavallé

       http://twitter.com/cavalle
          http://lmcavalle.co...
Demo
Internals
Webrat

#get   #post      #head       #delete    ...

#put   #request           #follow_redirect!



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

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

 get...
Webrat

#get   #post      #head       #delete    ...

#put   #request           #follow_redirect!



                     ...
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...
click_link


def matching_links
  @matching_links ||= link_elements.select do |link_element|
    matches_text?(link_elemen...
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...
click_link


def matching_links
  @matching_links ||= link_elements.select do |link_element|
    matches_text?(link_elemen...
click_link

def matches_text?(link)
  if @value.is_a?(Regexp)
    matcher = @value
  else
    matcher = /#{Regexp.escape(@...
click_link


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


def link_element
  matching_links.min { |a, b|
Webrat::XML.all_inner_text(a).length <=>
Webrat::XML.all_inner...
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
...
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_datetim...
Forms
select_date "April 26, 1982", :from => "Birthday"
select_date Date.today


select_datetime 2.days.ago
select_datetim...
Forms
select_date "April 26, 1982", :from => "Birthday"
select_date Date.today


select_datetime 2.days.ago
select_datetim...
Forms
select_date "April 26, 1982", :from => "Birthday"
select_date Date.today


select_datetime 2.days.ago
select_datetim...
Forms
select_date "April 26, 1982", :from => "Birthday"
select_date Date.today


select_datetime 2.days.ago
select_datetim...
Forms
select_date "April 26, 1982", :from => "Birthday"
select_date Date.today


select_datetime 2.days.ago
select_datetim...
Forms
select_date "April 26, 1982", :from => "Birthday"
select_date Date.today


select_datetime 2.days.ago
select_datetim...
Forms
select_date "April 26, 1982", :from => "Birthday"
select_date Date.today


select_datetime 2.days.ago
select_datetim...
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 "....
Matchers

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

assert_have_xpath ...
Matchers

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



response.should_not have_selector(".bookings", :coun...
Matchers

response.should have_selector(".hotels") do |hotels|
  hotels.should have_selector("h2", :content => "Hotels")
 ...
Configuration
Webrat.configure do |config|
  config.parse_with_nokogiri = true
  config.infinite_redirect_limit = 10
  config.open_error...
Webrat.configure do |config|
  config.mode = :rails | :merb | :sinatra |
                :rack_test |
                :mec...
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://twit...
Acceptance Testing with Webrat
Acceptance Testing with Webrat
Acceptance Testing with Webrat
Upcoming SlideShare
Loading in...5
×

Acceptance Testing with Webrat

3,992

Published on

Presented at Madrid-rb on June 25th, 2009

Published in: Technology, Business
0 Comments
9 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,992
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
72
Comments
0
Likes
9
Embeds 0
No embeds

No notes for slide

Acceptance Testing with Webrat

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

    Clipping is a handy way to collect important slides you want to go back to later.

×