Created
October 9, 2009 15:28
-
-
Save rodreegez/206116 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Credit to: http://simonwillison.net/2006/Jan/20/escape/ | |
RegExp.escape = function(text) { | |
if (!arguments.callee.sRE) { | |
var specials = [ | |
'/', '.', '*', '+', '?', '|', | |
'(', ')', '[', ']', '{', '}', '\\' | |
]; | |
arguments.callee.sRE = new RegExp( | |
'(\\' + specials.join('|\\') + ')', 'g' | |
); | |
} | |
return text.replace(arguments.callee.sRE, '\\$1'); | |
} | |
var allLabels = inDocument.getElementsByTagName("label"); | |
var regExp = new RegExp('^\\W*' + RegExp.escape(locator) + '(\\b|$)', 'i'); | |
var candidateLabels = $A(allLabels).select(function(candidateLabel){ | |
var labelText = getText(candidateLabel).strip(); | |
return (labelText.search(regExp) >= 0); | |
}); | |
if (candidateLabels.length == 0) { | |
return null; | |
} | |
//reverse length sort | |
candidateLabels = candidateLabels.sortBy(function(s) { | |
return getText(s).length; | |
}); | |
var locatedLabel = candidateLabels.first(); | |
var labelFor = locatedLabel.getAttribute('for'); | |
if ((labelFor == null) && (locatedLabel.hasChildNodes())) { | |
return locatedLabel.getElementsByTagName('button')[0] | |
|| locatedLabel.getElementsByTagName('input')[0] | |
|| locatedLabel.getElementsByTagName('textarea')[0] | |
|| locatedLabel.getElementsByTagName('select')[0]; | |
} | |
return selenium.browserbot.locationStrategies['id'].call(this, labelFor, inDocument, inWindow); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment