Skip to content

Commit

Permalink
Fix bug in Selenium when dealing with special characters in link text
Browse files Browse the repository at this point in the history
  • Loading branch information
brynary committed Sep 18, 2009
1 parent aae82b5 commit 63e1053
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
14 changes: 12 additions & 2 deletions 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);
1 change: 1 addition & 0 deletions 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)
Expand Down
7 changes: 6 additions & 1 deletion lib/webrat/selenium/selenium_session.rb
Expand Up @@ -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
Expand Down

0 comments on commit 63e1053

Please sign in to comment.