Implemented clicks_link_within for selenium session.

This commit is contained in:
Luke Melia 2008-06-23 00:37:31 -04:00
parent 6074ec3e35
commit cfdd66784e
1 changed files with 26 additions and 2 deletions

View File

@ -10,8 +10,9 @@ module Webrat
@selenium.open(url) @selenium.open(url)
end end
def fills_in(label_text, options) def fills_in(field_identifier, options)
@selenium.type("webrat=#{Regexp.escape(label_text)}", "#{options[:with]}") locator = "webrat=#{Regexp.escape(field_identifier)}"
@selenium.type(locator, "#{options[:with]}")
end end
def response_body def response_body
@ -30,6 +31,11 @@ module Webrat
wait_for_result(options[:wait]) wait_for_result(options[:wait])
end end
def clicks_link_within(selector, link_text, options = {})
@selenium.click("webratlinkwithin=#{selector}|#{link_text}")
wait_for_result(options[:wait])
end
def wait_for_result(wait_type) def wait_for_result(wait_type)
if wait_type == :ajax if wait_type == :ajax
wait_for_ajax wait_for_ajax
@ -130,6 +136,24 @@ module Webrat
return candidateLinks.first(); return candidateLinks.first();
JS JS
@selenium.add_location_strategy('webratlinkwithin', <<-JS)
var locatorParts = locator.split('|');
var cssAncestor = locatorParts[0];
var linkText = locatorParts[1];
var matchingElements = cssQuery(cssAncestor, inDocument);
var candidateLinks = matchingElements.collect(function(ancestor){
var links = ancestor.getElementsByTagName('a');
return $A(links).select(function(candidateLink) {
return PatternMatcher.matches(linkText, getText(candidateLink));
});
}).flatten().compact();
if (candidateLinks.length == 0) {
return null;
}
candidateLinks = candidateLinks.sortBy(function(s) { return s.length * -1; }); //reverse length sort
return candidateLinks.first();
JS
@selenium.add_location_strategy('webratselectwithoption', <<-JS) @selenium.add_location_strategy('webratselectwithoption', <<-JS)
var optionElements = inDocument.getElementsByTagName('option'); var optionElements = inDocument.getElementsByTagName('option');
var locatedOption = $A(optionElements).find(function(candidate){ var locatedOption = $A(optionElements).find(function(candidate){