From 7d94c16ac8afaa162348d949687ef7667f3d9a06 Mon Sep 17 00:00:00 2001 From: Luke Melia Date: Wed, 18 Jun 2008 23:57:56 -0400 Subject: [PATCH] Added options to selenium session methods to better handle ajax operations. For example: clicks_link('Foo', :wait => :ajax). I'd love to figure out how to infer whether a click results in ajax vs. dom manipulation vs. page load, but we'll start with this. --- lib/webrat/selenium/selenium_session.rb | 38 +++++++++++++++++++++---- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/lib/webrat/selenium/selenium_session.rb b/lib/webrat/selenium/selenium_session.rb index cbd0bae..aaf69f4 100644 --- a/lib/webrat/selenium/selenium_session.rb +++ b/lib/webrat/selenium/selenium_session.rb @@ -18,16 +18,44 @@ module Webrat @selenium.get_html_source end - def clicks_button(button_text = nil) + def clicks_button(button_text = nil, options = {}) + button_text, options = nil, button_text if button_text.is_a?(Hash) && options == {} button_text ||= '*' @selenium.click("button=#{button_text}") - @selenium.wait_for_page_to_load() + wait_for_result(options[:wait]) end - def clicks_link(link_text) - @selenium.click("webratlink=#{Regexp.escape(link_text)}") - @selenium.wait_for_page_to_load() + def clicks_link(link_text, options = {}) + @selenium.click("webratlink=#{link_text}") + wait_for_result(options[:wait]) end + + def wait_for_result(wait_type) + if wait_type == :ajax + wait_for_ajax + elsif wait_type == :effects + wait_for_effects + else + wait_for_page_to_load + end + end + + def wait_for_page_to_load(timeout = 15000) + @selenium.wait_for_page_to_load(timeout) + end + + def wait_for_ajax(timeout = 15000) + @selenium.wait_for_condition "window.Ajax.activeRequestCount == 0", timeout + end + + def wait_for_effects(timeout = 15000) + @selenium.wait_for_condition "window.Effect.Queue.size() == 0", timeout + end + + def wait_for_ajax_and_effects + wait_for_ajax + wait_for_effects + end def selects(option_text, options = {}) id_or_name_or_label = options[:from]