Bug 1409036 - Return single anon element from WebDriver:FindElement. r?whimboo draft
authorAndreas Tolfsen <ato@sny.no>
Thu, 05 Oct 2017 12:59:03 +0100
changeset 680894 d2d2a0fb3749a41b9f49989653338ddefc89ac64
parent 680782 c6a2643362a67cdf7a87ac165454fce4b383debb
child 680896 cda83b367daa9ea3bb7b1647121d3d7ecf5d428c
push id84665
push userbmo:ato@sny.no
push dateMon, 16 Oct 2017 14:35:19 +0000
reviewerswhimboo
bugs1409036
milestone58.0a1
Bug 1409036 - Return single anon element from WebDriver:FindElement. r?whimboo The WebDriver:FindElement command returned an array of elements when looking up anonymous elements. This patch rectifies the behaviour so that only a single element is returned. It introduces a new helper function called element.findAnonymousNodes, akin to similar helper functions for other strategies. This function returns an iterator of anonymous nodes so that WebDriver:FindElements (plural) and WebDriver:FindElement (singular) can share the same code path. MozReview-Commit-ID: 3IqPyAIZHtf
testing/marionette/element.js
--- a/testing/marionette/element.js
+++ b/testing/marionette/element.js
@@ -407,16 +407,34 @@ element.findByXPathAll = function(root, 
  * @return {Array.<DOMAnchorElement>}
  *     Sequence of link elements which text is |s|.
  */
 element.findByLinkText = function(node, s) {
   return filterLinks(node, link => link.text.trim() === s);
 };
 
 /**
+ * Find anonymous nodes of <var>node</var>.
+ *
+ * @param {XULElement} rootNode
+ *     Root node of the document.
+ * @param {XULElement} node
+ *     Where in the DOM hierarchy to begin searching.
+ *
+ * @return {Iterable.<XULElement>}
+ *     Iterator over anonymous elements.
+ */
+element.findAnonymousNodes = function* (rootNode, node) {
+  let anons = rootNode.getAnonymousNodes(node) || [];
+  for (let node of anons) {
+    yield node;
+  }
+};
+
+/**
  * Find all hyperlinks descendant of |node| which link text contains |s|.
  *
  * @param {DOMElement} node
  *     Where in the DOM hierachy to begin searching.
  * @param {string} s
  *     Link text to search for.
  *
  * @return {Array.<DOMAnchorElement>}
@@ -518,17 +536,17 @@ function findElement(using, value, rootN
     case element.Strategy.Selector:
       try {
         return startNode.querySelector(value);
       } catch (e) {
         throw new InvalidSelectorError(`${e.message}: "${value}"`);
       }
 
     case element.Strategy.Anon:
-      return rootNode.getAnonymousNodes(startNode);
+      return element.findAnonymousNodes(rootNode, startNode).next().value;
 
     case element.Strategy.AnonAttribute:
       let attr = Object.keys(value)[0];
       return rootNode.getAnonymousElementByAttribute(
           startNode, attr, value[attr]);
   }
 
   throw new InvalidSelectorError(`No such strategy: ${using}`);
@@ -581,17 +599,17 @@ function findElements(using, value, root
 
     case element.Strategy.PartialLinkText:
       return element.findByPartialLinkText(startNode, value);
 
     case element.Strategy.Selector:
       return startNode.querySelectorAll(value);
 
     case element.Strategy.Anon:
-      return rootNode.getAnonymousNodes(startNode);
+      return [...element.findAnonymousNodes(rootNode, startNode)];
 
     case element.Strategy.AnonAttribute:
       let attr = Object.keys(value)[0];
       let el = rootNode.getAnonymousElementByAttribute(
           startNode, attr, value[attr]);
       if (el) {
         return [el];
       }