diff --git a/lib/webrat/selenium/location_strategy_javascript/label.js b/lib/webrat/selenium/location_strategy_javascript/label.js index b5acd100..501fea1a 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 c5133090..91db8346 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 cf4ef46e..16d1ac92 100644 --- a/lib/webrat/selenium/selenium_session.rb +++ b/lib/webrat/selenium/selenium_session.rb @@ -79,7 +79,12 @@ def click_button(button_text_or_regexp = nil, options = {}) 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