From cfdd66784e2f963eb8c6f1e6a0c597c4744f2646 Mon Sep 17 00:00:00 2001 From: Luke Melia Date: Mon, 23 Jun 2008 00:37:31 -0400 Subject: [PATCH] Implemented clicks_link_within for selenium session. --- lib/webrat/selenium/selenium_session.rb | 28 +++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/lib/webrat/selenium/selenium_session.rb b/lib/webrat/selenium/selenium_session.rb index aaf69f4..87d9745 100644 --- a/lib/webrat/selenium/selenium_session.rb +++ b/lib/webrat/selenium/selenium_session.rb @@ -10,8 +10,9 @@ module Webrat @selenium.open(url) end - def fills_in(label_text, options) - @selenium.type("webrat=#{Regexp.escape(label_text)}", "#{options[:with]}") + def fills_in(field_identifier, options) + locator = "webrat=#{Regexp.escape(field_identifier)}" + @selenium.type(locator, "#{options[:with]}") end def response_body @@ -29,6 +30,11 @@ module Webrat @selenium.click("webratlink=#{link_text}") wait_for_result(options[:wait]) 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) if wait_type == :ajax @@ -130,6 +136,24 @@ module Webrat return candidateLinks.first(); 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) var optionElements = inDocument.getElementsByTagName('option'); var locatedOption = $A(optionElements).find(function(candidate){