From 1af45a7a27a1dbcab80a0a626ba761173ffaf063 Mon Sep 17 00:00:00 2001 From: Luke Melia Date: Tue, 14 Oct 2008 00:11:19 -0400 Subject: [PATCH] Extracted Selenium location strategies to their own .js files for easier readability and editing. --- .../location_strategy_javascript/button.js | 12 +++ .../location_strategy_javascript/label.js | 16 ++++ .../location_strategy_javascript/webrat.js | 5 + .../webratlink.js | 9 ++ .../webratlinkwithin.js | 15 +++ .../webratselectwithoption.js | 5 + lib/webrat/selenium/selenium_session.rb | 91 ++----------------- 7 files changed, 70 insertions(+), 83 deletions(-) create mode 100644 lib/webrat/selenium/location_strategy_javascript/button.js create mode 100644 lib/webrat/selenium/location_strategy_javascript/label.js create mode 100644 lib/webrat/selenium/location_strategy_javascript/webrat.js create mode 100644 lib/webrat/selenium/location_strategy_javascript/webratlink.js create mode 100644 lib/webrat/selenium/location_strategy_javascript/webratlinkwithin.js create mode 100644 lib/webrat/selenium/location_strategy_javascript/webratselectwithoption.js diff --git a/lib/webrat/selenium/location_strategy_javascript/button.js b/lib/webrat/selenium/location_strategy_javascript/button.js new file mode 100644 index 0000000..42449c7 --- /dev/null +++ b/lib/webrat/selenium/location_strategy_javascript/button.js @@ -0,0 +1,12 @@ +if (locator == '*') { + return selenium.browserbot.locationStrategies['xpath'].call(this, "//input[@type='submit']", inDocument, inWindow) +} +var inputs = inDocument.getElementsByTagName('input'); +return $A(inputs).find(function(candidate){ + inputType = candidate.getAttribute('type'); + if (inputType == 'submit' || inputType == 'image') { + var buttonText = $F(candidate); + return (PatternMatcher.matches(locator + '*', buttonText)); + } + return false; +}); diff --git a/lib/webrat/selenium/location_strategy_javascript/label.js b/lib/webrat/selenium/location_strategy_javascript/label.js new file mode 100644 index 0000000..b5acd10 --- /dev/null +++ b/lib/webrat/selenium/location_strategy_javascript/label.js @@ -0,0 +1,16 @@ +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 +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 +} +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 new file mode 100644 index 0000000..c513309 --- /dev/null +++ b/lib/webrat/selenium/location_strategy_javascript/webrat.js @@ -0,0 +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) + || null; diff --git a/lib/webrat/selenium/location_strategy_javascript/webratlink.js b/lib/webrat/selenium/location_strategy_javascript/webratlink.js new file mode 100644 index 0000000..ad71957 --- /dev/null +++ b/lib/webrat/selenium/location_strategy_javascript/webratlink.js @@ -0,0 +1,9 @@ +var links = inDocument.getElementsByTagName('a'); +var candidateLinks = $A(links).select(function(candidateLink) { + return PatternMatcher.matches(locator, getText(candidateLink)); +}); +if (candidateLinks.length == 0) { + return null; +} +candidateLinks = candidateLinks.sortBy(function(s) { return s.length * -1; }); //reverse length sort +return candidateLinks.first(); diff --git a/lib/webrat/selenium/location_strategy_javascript/webratlinkwithin.js b/lib/webrat/selenium/location_strategy_javascript/webratlinkwithin.js new file mode 100644 index 0000000..3cf00f6 --- /dev/null +++ b/lib/webrat/selenium/location_strategy_javascript/webratlinkwithin.js @@ -0,0 +1,15 @@ +var locatorParts = locator.split('|'); +var cssAncestor = locatorParts[0]; +var linkText = locatorParts[1]; +var matchingElements = cssQuery(cssAncestor, inDocument); +var candidateLinks = matchingElements.collect(function(ancestor){ + var links = ancestor.getElementsByTagName('a'); + return $A(links).select(function(candidateLink) { + return PatternMatcher.matches(linkText, getText(candidateLink)); + }); +}).flatten().compact(); +if (candidateLinks.length == 0) { + return null; +} +candidateLinks = candidateLinks.sortBy(function(s) { return s.length * -1; }); //reverse length sort +return candidateLinks.first(); diff --git a/lib/webrat/selenium/location_strategy_javascript/webratselectwithoption.js b/lib/webrat/selenium/location_strategy_javascript/webratselectwithoption.js new file mode 100644 index 0000000..2a8b403 --- /dev/null +++ b/lib/webrat/selenium/location_strategy_javascript/webratselectwithoption.js @@ -0,0 +1,5 @@ +var optionElements = inDocument.getElementsByTagName('option'); +var locatedOption = $A(optionElements).find(function(candidate){ + return (PatternMatcher.matches(locator, getText(candidate))); +}); +return locatedOption ? locatedOption.parentNode : null; diff --git a/lib/webrat/selenium/selenium_session.rb b/lib/webrat/selenium/selenium_session.rb index 6141269..32ffbd5 100644 --- a/lib/webrat/selenium/selenium_session.rb +++ b/lib/webrat/selenium/selenium_session.rb @@ -87,89 +87,14 @@ module Webrat @selenium.check("webrat=#{label_text}") end - protected - - def define_location_strategies - @selenium.add_location_strategy('label', <<-JS) - 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 - 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 - } - return selenium.browserbot.locationStrategies['id'].call(this, labelFor, inDocument, inWindow); - JS - - @selenium.add_location_strategy('webrat', <<-JS) - 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) - || null; - JS - - @selenium.add_location_strategy('button', <<-JS) - if (locator == '*') { - return selenium.browserbot.locationStrategies['xpath'].call(this, "//input[@type='submit']", inDocument, inWindow) - } - var inputs = inDocument.getElementsByTagName('input'); - return $A(inputs).find(function(candidate){ - inputType = candidate.getAttribute('type'); - if (inputType == 'submit' || inputType == 'image') { - var buttonText = $F(candidate); - return (PatternMatcher.matches(locator + '*', buttonText)); - } - return false; - }); - JS - - @selenium.add_location_strategy('webratlink', <<-JS) - var links = inDocument.getElementsByTagName('a'); - var candidateLinks = $A(links).select(function(candidateLink) { - return PatternMatcher.matches(locator, getText(candidateLink)); - }); - if (candidateLinks.length == 0) { - return null; - } - candidateLinks = candidateLinks.sortBy(function(s) { return s.length * -1; }); //reverse length sort - return candidateLinks.first(); - JS - - @selenium.add_location_strategy('webratlinkwithin', <<-JS) - var locatorParts = locator.split('|'); - var cssAncestor = locatorParts[0]; - var linkText = locatorParts[1]; - var matchingElements = cssQuery(cssAncestor, inDocument); - var candidateLinks = matchingElements.collect(function(ancestor){ - var links = ancestor.getElementsByTagName('a'); - return $A(links).select(function(candidateLink) { - return PatternMatcher.matches(linkText, getText(candidateLink)); - }); - }).flatten().compact(); - if (candidateLinks.length == 0) { - return null; - } - candidateLinks = candidateLinks.sortBy(function(s) { return s.length * -1; }); //reverse length sort - return candidateLinks.first(); - JS - - @selenium.add_location_strategy('webratselectwithoption', <<-JS) - var optionElements = inDocument.getElementsByTagName('option'); - var locatedOption = $A(optionElements).find(function(candidate){ - return (PatternMatcher.matches(locator, getText(candidate))); - }); - return locatedOption ? locatedOption.parentNode : null; - JS - end + protected + def define_location_strategies + Dir[File.join(File.dirname(__FILE__), "location_strategy_javascript", "*.js")].sort.each do |file| + strategy_js = File.read(file) + strategy_name = File.basename(file, '.js') + @selenium.add_location_strategy(strategy_name, strategy_js) + end + end end end \ No newline at end of file