From 63e1053afaa76d9276df137f042a779447376326 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Thu, 17 Sep 2009 20:28:19 -0400 Subject: [PATCH] Fix bug in Selenium when dealing with special characters in link text --- .../selenium/location_strategy_javascript/label.js | 14 ++++++++++++-- .../location_strategy_javascript/webrat.js | 1 + lib/webrat/selenium/selenium_session.rb | 7 ++++++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/webrat/selenium/location_strategy_javascript/label.js b/lib/webrat/selenium/location_strategy_javascript/label.js index b5acd10..501fea1 100644 --- a/lib/webrat/selenium/location_strategy_javascript/label.js +++ b/lib/webrat/selenium/location_strategy_javascript/label.js @@ -1,16 +1,26 @@ var allLabels = inDocument.getElementsByTagName("label"); + var candidateLabels = $A(allLabels).select(function(candidateLabel){ var regExp = new RegExp('^' + locator + '\\b', 'i'); var labelText = getText(candidateLabel).strip(); return (labelText.search(regExp) >= 0); }); + if (candidateLabels.length == 0) { return null; } -candidateLabels = candidateLabels.sortBy(function(s) { return s.length * -1; }); //reverse length sort + +//reverse length sort +candidateLabels = candidateLabels.sortBy(function(s) { + return s.length * -1; +}); + var locatedLabel = candidateLabels.first(); var labelFor = locatedLabel.getAttribute('for'); + if ((labelFor == null) && (locatedLabel.hasChildNodes())) { - return locatedLabel.firstChild; //TODO: should find the first form field, not just any node + // TODO: should find the first form field, not just any node + return locatedLabel.firstChild; } + return selenium.browserbot.locationStrategies['id'].call(this, labelFor, inDocument, inWindow); diff --git a/lib/webrat/selenium/location_strategy_javascript/webrat.js b/lib/webrat/selenium/location_strategy_javascript/webrat.js index c513309..91db834 100644 --- a/lib/webrat/selenium/location_strategy_javascript/webrat.js +++ b/lib/webrat/selenium/location_strategy_javascript/webrat.js @@ -1,4 +1,5 @@ var locationStrategies = selenium.browserbot.locationStrategies; + return locationStrategies['id'].call(this, locator, inDocument, inWindow) || locationStrategies['name'].call(this, locator, inDocument, inWindow) || locationStrategies['label'].call(this, locator, inDocument, inWindow) diff --git a/lib/webrat/selenium/selenium_session.rb b/lib/webrat/selenium/selenium_session.rb index cf4ef46..16d1ac9 100644 --- a/lib/webrat/selenium/selenium_session.rb +++ b/lib/webrat/selenium/selenium_session.rb @@ -79,7 +79,12 @@ module Webrat webrat_deprecate :clicks_button, :click_button def click_link(link_text_or_regexp, options = {}) - pattern = adjust_if_regexp(link_text_or_regexp) + if link_text_or_regexp.is_a?(Regexp) + pattern = "evalregex:#{link_text_or_regexp.inspect}" + else + pattern = link_text_or_regexp.to_s + end + locator = "webratlink=#{pattern}" selenium.wait_for_element locator, :timeout_in_seconds => 5 selenium.click locator