Add support in Selenium implementation for clicking a link or button using a regular expression.

This commit is contained in:
Luke Melia 2008-10-29 02:49:08 -04:00
parent e774bd6a99
commit ca9d04422c
4 changed files with 34 additions and 7 deletions

View File

@ -11,6 +11,7 @@
* Minor enhancements * Minor enhancements
* Allow clicking links and buttons by a regular expression in Selenium (Luke Melia)
* Allow clicking links by a regular expression * Allow clicking links by a regular expression
* Add #http_accept for including MIME type HTTP "Accept" headers (Ryan Briones) * Add #http_accept for including MIME type HTTP "Accept" headers (Ryan Briones)
* Add #header to support inclusion of custom HTTP headers (Ryan Briones) * Add #header to support inclusion of custom HTTP headers (Ryan Briones)

View File

@ -6,7 +6,7 @@ return $A(inputs).find(function(candidate){
inputType = candidate.getAttribute('type'); inputType = candidate.getAttribute('type');
if (inputType == 'submit' || inputType == 'image') { if (inputType == 'submit' || inputType == 'image') {
var buttonText = $F(candidate); var buttonText = $F(candidate);
return (PatternMatcher.matches(locator + '*', buttonText)); return (PatternMatcher.matches(locator, buttonText));
} }
return false; return false;
}); });

View File

@ -0,0 +1,6 @@
PatternMatcher.strategies['evalregex'] = function(regexpString) {
this.regexp = eval(regexpString);
this.matches = function(actual) {
return this.regexp.test(actual);
};
};

View File

@ -4,6 +4,7 @@ module Webrat
def initialize(selenium_driver) def initialize(selenium_driver)
super() super()
@selenium = selenium_driver @selenium = selenium_driver
extend_selenium
define_location_strategies define_location_strategies
end end
@ -22,16 +23,21 @@ module Webrat
@selenium.get_html_source @selenium.get_html_source
end end
def clicks_button(button_text = nil, options = {}) def clicks_button(button_text_or_regexp = nil, options = {})
button_text, options = nil, button_text if button_text.is_a?(Hash) && options == {} if button_text_or_regexp.is_a?(Hash) && options == {}
button_text ||= '*' pattern, options = nil, button_text_or_regexp
@selenium.click("button=#{button_text}") else
pattern = adjust_if_regexp(button_text_or_regexp)
end
pattern ||= '*'
@selenium.click("button=#{pattern}")
wait_for_result(options[:wait]) wait_for_result(options[:wait])
end end
alias_method :click_button, :clicks_button alias_method :click_button, :clicks_button
def clicks_link(link_text, options = {}) def clicks_link(link_text_or_regexp, options = {})
@selenium.click("webratlink=#{link_text}") pattern = adjust_if_regexp(link_text_or_regexp)
@selenium.click("webratlink=#{pattern}")
wait_for_result(options[:wait]) wait_for_result(options[:wait])
end end
alias_method :click_link, :clicks_link alias_method :click_link, :clicks_link
@ -97,6 +103,20 @@ module Webrat
protected 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 def define_location_strategies
Dir[File.join(File.dirname(__FILE__), "location_strategy_javascript", "*.js")].sort.each do |file| Dir[File.join(File.dirname(__FILE__), "location_strategy_javascript", "*.js")].sort.each do |file|
strategy_js = File.read(file) strategy_js = File.read(file)