Bug 1409036 - Return single anon element from WebDriver:FindElement. r=whimboo
authorAndreas Tolfsen <ato@sny.no>
Thu, 05 Oct 2017 12:59:03 +0100
changeset 386457 32dd7d62d4fb1214451959fc6f3e5ef29ef0d938
parent 386456 377ee18d8a597e9aa968f19a8bd7ce68696f8474
child 386458 14e6c96c88d47d4225b45fb4bac539b3f6fc7f18
push id53394
push useratolfsen@mozilla.com
push dateMon, 16 Oct 2017 20:51:57 +0000
treeherderautoland@32dd7d62d4fb [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerswhimboo
bugs1409036
milestone58.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
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
@@ -408,16 +408,34 @@ element.findByXPathAll = function(root, 
  * @return {Array.<DOMAnchorElement>}
  *     Sequence of link elements which text is <var>s</var>.
  */
 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>}
@@ -519,17 +537,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}`);
@@ -582,17 +600,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];
       }