Bug 1283999 - Publish XPath find element functions; r=automatedtester a=test-only
authorAndreas Tolfsen <ato@mozilla.com>
Sat, 02 Jul 2016 21:51:21 +0100
changeset 341937 fa4681fe062f30712caef3b2724bd3ac71862468
parent 341936 42fc9e1652199459ddb667b1fb0b541602550393
child 341938 f3111d767fd777339107a8f7f0bea5e5630cbf6c
push id1183
push userraliiev@mozilla.com
push dateMon, 05 Sep 2016 20:01:49 +0000
treeherdermozilla-release@3148731bed45 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersautomatedtester, test-only
bugs1283999
milestone49.0a2
Bug 1283999 - Publish XPath find element functions; r=automatedtester a=test-only Publish the findByXPath and findAllByXPath functions to match the style of the recently introduces findByLinkText and findByPartialLinkText functions. Do note that this patch rearranges the input arguments these functions take. Possibly we do not have to send the root element as input at all. MozReview-Commit-ID: 4H7kpZBUsTw
testing/marionette/element.js
--- a/testing/marionette/element.js
+++ b/testing/marionette/element.js
@@ -291,57 +291,58 @@ function find_(container, strategy, sele
     return res;
   } else if (res) {
     return [res];
   }
   return [];
 }
 
 /**
- * Find a value by XPATH
+ * Find a single element by XPath expression.
+ *
+ * @param {DOMElement} root
+ *     Document root
+ * @param {DOMElement} startNode
+ *     Where in the DOM hiearchy to begin searching.
+ * @param {string} expr
+ *     XPath search expression.
  *
- * @param nsIDOMElement root
- *        Document root
- * @param string value
- *        XPATH search string
- * @param nsIDOMElement node
- *        start node
- *
- * @return nsIDOMElement
- *        returns the found element
+ * @return {DOMElement}
+ *     First element matching expression.
  */
-function findByXPath(root, value, node) {
-  return root.evaluate(value, node, null,
-          Ci.nsIDOMXPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
-}
+element.findByXPath = function(root, startNode, expr) {
+  let iter = root.evaluate(expr, startNode, null,
+      Ci.nsIDOMXPathResult.FIRST_ORDERED_NODE_TYPE, null)
+  return iter.singleNodeValue;
+};
 
 /**
- * Find values by XPATH
+ * Find elements by XPath expression.
  *
- * @param nsIDOMElement root
- *        Document root
- * @param string value
- *        XPATH search string
- * @param nsIDOMElement node
- *        start node
+ * @param {DOMElement} root
+ *     Document root.
+ * @param {DOMElement} startNode
+ *     Where in the DOM hierarchy to begin searching.
+ * @param {string} expr
+ *     XPath search expression.
  *
- * @return object
- *        returns a list of found nsIDOMElements
+ * @return {Array.<DOMElement>}
+ *     Sequence of found elements matching expression.
  */
-function findByXPathAll(root, value, node) {
-  let values = root.evaluate(value, node, null,
-                    Ci.nsIDOMXPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
-  let elements = [];
-  let element = values.iterateNext();
-  while (element) {
-    elements.push(element);
-    element = values.iterateNext();
+element.findByXPathAll = function(root, startNode, expr) {
+  let rv = [];
+  let iter = root.evaluate(expr, startNode, null,
+      Ci.nsIDOMXPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
+  let el = iter.iterateNext();
+  while (el) {
+    rv.push(el);
+    el = iter.iterateNext();
   }
-  return elements;
-}
+  return rv;
+};
 
 /**
  * Find all hyperlinks dscendant of |node| which link text is |s|.
  *
  * @param {DOMElement} node
  *     Where in the DOM hierarchy to being searching.
  * @param {string} s
  *     Link text to search for.
@@ -411,34 +412,34 @@ function filterLinks(node, predicate) {
  *     If selector expression |value| is malformed.
  */
 function findElement(using, value, rootNode, startNode) {
   switch (using) {
     case element.Strategy.ID:
       if (startNode.getElementById) {
         return startNode.getElementById(value);
       }
-      return findByXPath(rootNode, `.//*[@id="${value}"]`, startNode);
+      return element.findByXPath(rootNode, startNode, `.//*[@id="${value}"]`);
 
     case element.Strategy.Name:
       if (startNode.getElementsByName) {
         return startNode.getElementsByName(value)[0];
       }
-      return findByXPath(rootNode, `.//*[@name="${value}"]`, startNode);
+      return element.findByXPath(rootNode, startNode, `.//*[@name="${value}"]`);
 
     case element.Strategy.ClassName:
       // works for >= Firefox 3
       return  startNode.getElementsByClassName(value)[0];
 
     case element.Strategy.TagName:
       // works for all elements
       return startNode.getElementsByTagName(value)[0];
 
     case element.Strategy.XPath:
-      return  findByXPath(rootNode, value, startNode);
+      return  element.findByXPath(rootNode, startNode, value);
 
     case element.Strategy.LinkText:
       for (let link of startNode.getElementsByTagName("a")) {
         if (link.text === value) {
           return link;
         }
       }
       break;
@@ -492,23 +493,23 @@ function findElement(using, value, rootN
  */
 function findElements(using, value, rootNode, startNode) {
   switch (using) {
     case element.Strategy.ID:
       value = `.//*[@id="${value}"]`;
 
     // fall through
     case element.Strategy.XPath:
-      return findByXPathAll(rootNode, value, startNode);
+      return element.findByXPathAll(rootNode, startNode, value);
 
     case element.Strategy.Name:
       if (startNode.getElementsByName) {
         return startNode.getElementsByName(value);
       }
-      return findByXPathAll(rootNode, `.//*[@name="${value}"]`, startNode);
+      return element.findByXPathAll(rootNode, startNode, `.//*[@name="${value}"]`);
 
     case element.Strategy.ClassName:
       return startNode.getElementsByClassName(value);
 
     case element.Strategy.TagName:
       return startNode.getElementsByTagName(value);
 
     case element.Strategy.LinkText: