Bug 1251763 - Correct type check on singular returned elements; r?automatedtester draft
authorAndreas Tolfsen <ato@mozilla.com>
Tue, 08 Mar 2016 16:23:50 +0000
changeset 338134 9c12de3a626204dc9e5f26c5f91a1990b7c48894
parent 338095 05c087337043dd8e71cc27bdb5b9d55fd00aaa26
child 338135 5dbdf75985f75badcd62954b2c9c3b3199ab1e2b
push id12440
push userbmo:ato@mozilla.com
push dateTue, 08 Mar 2016 16:50:42 +0000
reviewersautomatedtester
bugs1251763
milestone48.0a1
Bug 1251763 - Correct type check on singular returned elements; r?automatedtester The return values from the different element search functions in searchFn have inconsistent return types, and a null check is usually not what we want. In order to have findElement consistently return a no such element error, we need to do a loose false test. This bug has affected the findElement command when using the tag name, name, class name, link text, and partial link text selectors. Other selectors were unaffected. MozReview-Commit-ID: C26R3YrqKyf
testing/marionette/element.js
testing/marionette/harness/marionette/tests/unit/test_findelement.py
--- a/testing/marionette/element.js
+++ b/testing/marionette/element.js
@@ -428,17 +428,17 @@ ElementManager.prototype = {
     try {
       res = searchFn(strategy, selector, rootNode, startNode);
     } catch (e) {
       throw new InvalidSelectorError(`Given ${strategy} expression "${selector}" is invalid`);
     }
 
     if (element.isElementCollection(res)) {
       return res;
-    } else if (res !== null) {
+    } else if (res) {
       return [res];
     }
     return [];
   },
 
   /**
    * Find a value by XPATH
    *
--- a/testing/marionette/harness/marionette/tests/unit/test_findelement.py
+++ b/testing/marionette/harness/marionette/tests/unit/test_findelement.py
@@ -4,16 +4,20 @@
 
 from marionette import MarionetteTestCase
 from marionette_driver.marionette import HTMLElement
 from marionette_driver.by import By
 from marionette_driver.errors import NoSuchElementException, InvalidSelectorException
 
 
 class TestElements(MarionetteTestCase):
+    def setUp(self):
+        MarionetteTestCase.setUp(self)
+        self.marionette.set_search_timeout(0)
+
     def test_id(self):
         test_html = self.marionette.absolute_url("test.html")
         self.marionette.navigate(test_html)
         el = self.marionette.execute_script("return window.document.getElementById('mozLink');")
         found_el = self.marionette.find_element(By.ID, "mozLink")
         self.assertEqual(HTMLElement, type(found_el))
         self.assertEqual(el, found_el)
 
@@ -114,22 +118,36 @@ class TestElements(MarionetteTestCase):
         found_el = self.marionette.find_element(By.XPATH, "id('mozLink')")
         self.assertEqual(HTMLElement, type(found_el))
         self.assertEqual(el, found_el)
         found_el = self.marionette.find_elements(By.XPATH, "id('mozLink')")[0]
         self.assertEqual(HTMLElement, type(found_el))
         self.assertEqual(el, found_el)
 
     def test_not_found(self):
-        test_html = self.marionette.absolute_url("test.html")
-        self.marionette.navigate(test_html)
-        self.marionette.set_search_timeout(1000)
-        self.assertRaises(NoSuchElementException, self.marionette.find_element, By.ID, "I'm not on the page")
         self.marionette.set_search_timeout(0)
-        self.assertRaises(NoSuchElementException, self.marionette.find_element, By.ID, "I'm not on the page")
+        self.assertRaises(NoSuchElementException, self.marionette.find_element, By.CLASS_NAME, "cheese")
+        self.assertRaises(NoSuchElementException, self.marionette.find_element, By.CSS_SELECTOR, "cheese")
+        self.assertRaises(NoSuchElementException, self.marionette.find_element, By.ID, "cheese")
+        self.assertRaises(NoSuchElementException, self.marionette.find_element, By.LINK_TEXT, "cheese")
+        self.assertRaises(NoSuchElementException, self.marionette.find_element, By.NAME, "cheese")
+        self.assertRaises(NoSuchElementException, self.marionette.find_element, By.PARTIAL_LINK_TEXT, "cheese")
+        self.assertRaises(NoSuchElementException, self.marionette.find_element, By.TAG_NAME, "cheese")
+        self.assertRaises(NoSuchElementException, self.marionette.find_element, By.XPATH, "cheese")
+
+    def test_not_found_implicit_wait(self):
+        self.marionette.set_search_timeout(50)
+        self.assertRaises(NoSuchElementException, self.marionette.find_element, By.CLASS_NAME, "cheese")
+        self.assertRaises(NoSuchElementException, self.marionette.find_element, By.CSS_SELECTOR, "cheese")
+        self.assertRaises(NoSuchElementException, self.marionette.find_element, By.ID, "cheese")
+        self.assertRaises(NoSuchElementException, self.marionette.find_element, By.LINK_TEXT, "cheese")
+        self.assertRaises(NoSuchElementException, self.marionette.find_element, By.NAME, "cheese")
+        self.assertRaises(NoSuchElementException, self.marionette.find_element, By.PARTIAL_LINK_TEXT, "cheese")
+        self.assertRaises(NoSuchElementException, self.marionette.find_element, By.TAG_NAME, "cheese")
+        self.assertRaises(NoSuchElementException, self.marionette.find_element, By.XPATH, "cheese")
 
     def test_timeout_element(self):
         test_html = self.marionette.absolute_url("test.html")
         self.marionette.navigate(test_html)
         button = self.marionette.find_element("id", "createDivButton")
         button.click()
         self.assertRaises(NoSuchElementException, self.marionette.find_element, By.ID, "newDiv")
         self.assertTrue(True, self.marionette.set_search_timeout(8000))