Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add support in Selenium implementation for clicking a link or button …

…using a regular expression.
  • Loading branch information...
commit ca9d04422c3755f2a5ccc809b3f0bcc9fef8d52a 1 parent e774bd6
@lukemelia lukemelia authored
View
1  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)
View
2  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;
});
View
6 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);
+ };
+};
View
32 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)
Please sign in to comment.
Something went wrong with that request. Please try again.