From f9985cd4881cb88fbabeff73b939908542780821 Mon Sep 17 00:00:00 2001 From: lukemelia Date: Tue, 5 Jan 2010 18:33:45 -0500 Subject: [PATCH] Make selenium matchers handle negative match more consistently with positive match --- History.txt | 4 +++ lib/webrat/selenium/matchers/have_content.rb | 34 +++++++++++++------ lib/webrat/selenium/matchers/have_selector.rb | 8 +++++ lib/webrat/selenium/matchers/have_xpath.rb | 8 +++++ lib/webrat/selenium/selenium_rc_server.rb | 2 +- 5 files changed, 44 insertions(+), 12 deletions(-) diff --git a/History.txt b/History.txt index eb8bef84..0d9900c9 100644 --- a/History.txt +++ b/History.txt @@ -1,3 +1,7 @@ +* Bug fixes + + * Make selenium matchers handle negative match more consistently with positive match (Luke Melia) + == 0.6.0 / 2009-11-28 REMOVED: Support for Hpricot + REXML as an alternative to Nokogiri. diff --git a/lib/webrat/selenium/matchers/have_content.rb b/lib/webrat/selenium/matchers/have_content.rb index c6348a3d..f1f6973d 100644 --- a/lib/webrat/selenium/matchers/have_content.rb +++ b/lib/webrat/selenium/matchers/have_content.rb @@ -7,29 +7,33 @@ def initialize(content) end def matches?(response) - if @content.is_a?(Regexp) - text_finder = "regexp:#{@content.source}" - else - text_finder = @content - end - response.session.wait_for do response.selenium.is_text_present(text_finder) end - rescue Webrat::TimeoutError - false + rescue Webrat::TimeoutError => e + @error_message = e.message + false + end + + def does_not_match?(response) + response.session.wait_for do + !response.selenium.is_text_present(text_finder) + end + rescue Webrat::TimeoutError => e + @error_message = e.message + false end # ==== Returns # String:: The failure message. def failure_message - "expected the following element's content to #{content_message}:\n#{@element}" + "expected the response to #{content_message}:\n#{@error_message}" end # ==== Returns # String:: The failure message to be displayed in negative matches. def negative_failure_message - "expected the following element's content to not #{content_message}:\n#{@element}" + "expected the response to not #{content_message}" end def content_message @@ -40,6 +44,14 @@ def content_message "match #{@content.inspect}" end end + + def text_finder + if @content.is_a?(Regexp) + "regexp:#{@content.source}" + else + @content + end + end end # Matches the contents of an HTML document with @@ -52,7 +64,7 @@ def contain(content) # the supplied string or regexp def assert_contain(content) hc = HasContent.new(content) - assert hc.matches?(response), hc.failure_message + assert hc.matches?(response), hc.failure_message end # Asserts that the body of the response diff --git a/lib/webrat/selenium/matchers/have_selector.rb b/lib/webrat/selenium/matchers/have_selector.rb index ba0bbfa0..dbb0f94c 100644 --- a/lib/webrat/selenium/matchers/have_selector.rb +++ b/lib/webrat/selenium/matchers/have_selector.rb @@ -14,6 +14,14 @@ def matches?(response) false end + def does_not_match?(response) + response.session.wait_for do + !response.selenium.is_element_present("css=#{@expected}") + end + rescue Webrat::TimeoutError + false + end + # ==== Returns # String:: The failure message. def failure_message diff --git a/lib/webrat/selenium/matchers/have_xpath.rb b/lib/webrat/selenium/matchers/have_xpath.rb index 6875a9db..60fcc453 100644 --- a/lib/webrat/selenium/matchers/have_xpath.rb +++ b/lib/webrat/selenium/matchers/have_xpath.rb @@ -14,6 +14,14 @@ def matches?(response) false end + def does_not_match?(response) + response.session.wait_for do + !response.selenium.is_element_present("xpath=#{@expected}") + end + rescue Webrat::TimeoutError + false + end + # ==== Returns # String:: The failure message. def failure_message diff --git a/lib/webrat/selenium/selenium_rc_server.rb b/lib/webrat/selenium/selenium_rc_server.rb index ecc00404..82aae31a 100644 --- a/lib/webrat/selenium/selenium_rc_server.rb +++ b/lib/webrat/selenium/selenium_rc_server.rb @@ -61,7 +61,7 @@ def wait_for_socket TCPSocket.wait_for_service_with_timeout \ :host => (Webrat.configuration.selenium_server_address || "0.0.0.0"), :port => Webrat.configuration.selenium_server_port, - :timeout => 15 # seconds + :timeout => 45 # seconds end end