Skip to content

Commit

Permalink
Add support in Selenium implementation for clicking a link or button …
Browse files Browse the repository at this point in the history
…using a regular expression.
  • Loading branch information
lukemelia committed Oct 29, 2008
1 parent e774bd6 commit ca9d044
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
1 change: 1 addition & 0 deletions History.txt
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/webrat/selenium/location_strategy_javascript/button.js
Expand Up @@ -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;
});
6 changes: 6 additions & 0 deletions 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);
};
};
32 changes: 26 additions & 6 deletions lib/webrat/selenium/selenium_session.rb
Expand Up @@ -4,6 +4,7 @@ class SeleniumSession < Session
def initialize(selenium_driver)
super()
@selenium = selenium_driver
extend_selenium
define_location_strategies
end

Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit ca9d044

Please sign in to comment.