Bug 1285240: Trim leading and trailing space when finding elements by link text r?ato
This gives up parity with the WebDriver project and brings us in line with the current
specification http://w3c.github.io/webdriver/webdriver-spec.html#dfn-link-text
MozReview-Commit-ID: 9OD6B2pKTfx
--- a/testing/marionette/element.js
+++ b/testing/marionette/element.js
@@ -346,17 +346,17 @@ element.findByXPathAll = function(root,
* Where in the DOM hierarchy to being searching.
* @param {string} s
* Link text to search for.
*
* @return {Array.<DOMAnchorElement>}
* Sequence of link elements which text is |s|.
*/
element.findByLinkText = function(node, s) {
- return filterLinks(node, link => link.text === s);
+ return filterLinks(node, link => link.text.trim() === s);
};
/**
* 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
@@ -433,17 +433,17 @@ function findElement(using, value, rootN
// works for all elements
return startNode.getElementsByTagName(value)[0];
case element.Strategy.XPath:
return element.findByXPath(rootNode, startNode, value);
case element.Strategy.LinkText:
for (let link of startNode.getElementsByTagName("a")) {
- if (link.text === value) {
+ if (link.text.trim() === value) {
return link;
}
}
break;
case element.Strategy.PartialLinkText:
for (let link of startNode.getElementsByTagName("a")) {
if (link.text.indexOf(value) != -1) {
--- a/testing/marionette/harness/marionette/tests/unit/test_element_retrieval.py
+++ b/testing/marionette/harness/marionette/tests/unit/test_element_retrieval.py
@@ -35,16 +35,17 @@ parent_child_html = inline("<div id=pare
parent_child_xhtml = inline('<div id="parent"><p id="child"></p></div>', doctype="xhtml")
children_html = inline("<div><p>foo <p>bar</div>", doctype="html")
children_xhtml = inline("<div><p>foo</p> <p>bar</p></div>", doctype="xhtml")
class_html = inline("<p class='foo bar'>", doctype="html")
class_xhtml = inline('<p class="foo bar"></p>', doctype="xhtml")
name_html = inline("<p name=foo>", doctype="html")
name_xhtml = inline('<p name="foo"></p>', doctype="xhtml")
link_html = inline("<p><a href=#>foo bar</a>", doctype="html")
+link_html_with_trailing_space = inline("<p><a href=#>a link with a trailing space </a>")
link_xhtml = inline('<p><a href="#">foo bar</a></p>', doctype="xhtml")
class TestFindElementHTML(MarionetteTestCase):
def setUp(self):
MarionetteTestCase.setUp(self)
self.marionette.set_search_timeout(0)
@@ -98,16 +99,23 @@ class TestFindElementHTML(MarionetteTest
def test_link_text(self):
self.marionette.navigate(link_html)
el = self.marionette.execute_script("return document.querySelector('a')")
found = self.marionette.find_element(By.LINK_TEXT, "foo bar")
self.assertIsInstance(found, HTMLElement)
self.assertEqual(el, found)
+ def test_link_text_with_trailing_space(self):
+ self.marionette.navigate(link_html_with_trailing_space)
+ el = self.marionette.execute_script("return document.querySelector('a')")
+ found = self.marionette.find_element(By.LINK_TEXT, "a link with a trailing space")
+ self.assertIsInstance(found, HTMLElement)
+ self.assertEqual(el, found)
+
def test_partial_link_text(self):
self.marionette.navigate(link_html)
el = self.marionette.execute_script("return document.querySelector('a')")
found = self.marionette.find_element(By.PARTIAL_LINK_TEXT, "foo")
self.assertIsInstance(found, HTMLElement)
self.assertEqual(el, found)
def test_xpath(self):
@@ -338,16 +346,24 @@ class TestFindElementsHTML(MarionetteTes
def test_link_text(self):
self.marionette.navigate(link_html)
els = self.marionette.execute_script("return document.querySelectorAll('a')")
found = self.marionette.find_elements(By.LINK_TEXT, "foo bar")
self.assertItemsIsInstance(found, HTMLElement)
self.assertSequenceEqual(els, found)
+ def test_link_text_with_trailing_space(self):
+ self.marionette.navigate(link_html_with_trailing_space)
+ els = self.marionette.execute_script("return document.querySelectorAll('a')")
+ found = self.marionette.find_elements(By.LINK_TEXT, "a link with a trailing space")
+ self.assertItemsIsInstance(found, HTMLElement)
+ self.assertSequenceEqual(els, found)
+
+
def test_partial_link_text(self):
self.marionette.navigate(link_html)
els = self.marionette.execute_script("return document.querySelectorAll('a')")
found = self.marionette.find_elements(By.PARTIAL_LINK_TEXT, "foo")
self.assertItemsIsInstance(found, HTMLElement)
self.assertSequenceEqual(els, found)
def test_xpath(self):