Fix bug in Selenium when dealing with special characters in link text

This commit is contained in:
Bryan Helmkamp 2009-09-17 20:28:19 -04:00
parent aae82b5111
commit 63e1053afa
3 changed files with 19 additions and 3 deletions

View File

@ -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);

View File

@ -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)

View File

@ -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