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
--- 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];
}