From ca9d04422c3755f2a5ccc809b3f0bcc9fef8d52a Mon Sep 17 00:00:00 2001 From: Luke Melia Date: Wed, 29 Oct 2008 02:49:08 -0400 Subject: [PATCH] Add support in Selenium implementation for clicking a link or button using a regular expression. --- History.txt | 1 + .../location_strategy_javascript/button.js | 2 +- lib/webrat/selenium/selenium_extensions.js | 6 ++++ lib/webrat/selenium/selenium_session.rb | 32 +++++++++++++++---- 4 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 lib/webrat/selenium/selenium_extensions.js diff --git a/History.txt b/History.txt index 16530b13..b786d0c7 100644 --- a/History.txt +++ b/History.txt @@ -11,6 +11,7 @@ * Minor enhancements + * Allow clicking links and buttons by a regular expression in Selenium (Luke Melia) * Allow clicking links by a regular expression * Add #http_accept for including MIME type HTTP "Accept" headers (Ryan Briones) * Add #header to support inclusion of custom HTTP headers (Ryan Briones) diff --git a/lib/webrat/selenium/location_strategy_javascript/button.js b/lib/webrat/selenium/location_strategy_javascript/button.js index 42449c7a..add1679e 100644 --- a/lib/webrat/selenium/location_strategy_javascript/button.js +++ b/lib/webrat/selenium/location_strategy_javascript/button.js @@ -6,7 +6,7 @@ return $A(inputs).find(function(candidate){ inputType = candidate.getAttribute('type'); if (inputType == 'submit' || inputType == 'image') { var buttonText = $F(candidate); - return (PatternMatcher.matches(locator + '*', buttonText)); + return (PatternMatcher.matches(locator, buttonText)); } return false; }); diff --git a/lib/webrat/selenium/selenium_extensions.js b/lib/webrat/selenium/selenium_extensions.js new file mode 100644 index 00000000..28363dde --- /dev/null +++ b/lib/webrat/selenium/selenium_extensions.js @@ -0,0 +1,6 @@ +PatternMatcher.strategies['evalregex'] = function(regexpString) { + this.regexp = eval(regexpString); + this.matches = function(actual) { + return this.regexp.test(actual); + }; +}; \ No newline at end of file diff --git a/lib/webrat/selenium/selenium_session.rb b/lib/webrat/selenium/selenium_session.rb index f1c74116..2f6219d0 100644 --- a/lib/webrat/selenium/selenium_session.rb +++ b/lib/webrat/selenium/selenium_session.rb @@ -4,6 +4,7 @@ class SeleniumSession < Session def initialize(selenium_driver) super() @selenium = selenium_driver + extend_selenium define_location_strategies end @@ -22,16 +23,21 @@ def response_body @selenium.get_html_source end - 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}") + def clicks_button(button_text_or_regexp = nil, options = {}) + if button_text_or_regexp.is_a?(Hash) && options == {} + pattern, options = nil, button_text_or_regexp + else + pattern = adjust_if_regexp(button_text_or_regexp) + end + pattern ||= '*' + @selenium.click("button=#{pattern}") wait_for_result(options[:wait]) end alias_method :click_button, :clicks_button - def clicks_link(link_text, options = {}) - @selenium.click("webratlink=#{link_text}") + def clicks_link(link_text_or_regexp, options = {}) + pattern = adjust_if_regexp(link_text_or_regexp) + @selenium.click("webratlink=#{pattern}") wait_for_result(options[:wait]) end alias_method :click_link, :clicks_link @@ -97,6 +103,20 @@ def dragdrop(*args) protected + def adjust_if_regexp(text_or_regexp) + if text_or_regexp.is_a?(Regexp) + "evalregex:#{text_or_regexp.inspect}" + else + text_or_regexp + end + end + + def extend_selenium + extensions_file = File.join(File.dirname(__FILE__), "selenium_extensions.js") + extenions_js = File.read(extensions_file) + @selenium.get_eval(extenions_js) + end + def define_location_strategies Dir[File.join(File.dirname(__FILE__), "location_strategy_javascript", "*.js")].sort.each do |file| strategy_js = File.read(file)