Bug 1241067: Handle Invalid Selector values when finding elements r?ato draft
authorDavid Burns <dburns@mozilla.com>
Thu, 21 Jan 2016 21:07:36 +0000
changeset 324097 2f6ceefc910be77e6d88ed300d29650b41a171d9
parent 324096 9eee2ef7aad100c2eb9ffe7e45b065c669ee190f
child 513322 786beb7e2971af7d01c491442fa827c364a3af81
push id9840
push userdburns@mozilla.com
push dateThu, 21 Jan 2016 21:48:39 +0000
reviewersato
bugs1241067
milestone46.0a1
Bug 1241067: Handle Invalid Selector values when finding elements r?ato This allows us to now handle errors and return the appropriate errors to the clients
testing/marionette/client/marionette/tests/unit/test_findelement.py
testing/marionette/elements.js
--- a/testing/marionette/client/marionette/tests/unit/test_findelement.py
+++ b/testing/marionette/client/marionette/tests/unit/test_findelement.py
@@ -176,8 +176,52 @@ class TestElements(MarionetteTestCase):
     def test_should_find_elements_by_link_text(self):
         test_html = self.marionette.absolute_url("nestedElements.html")
         self.marionette.navigate(test_html)
         element = self.marionette.find_element(By.NAME, "div1")
         children = element.find_elements(By.LINK_TEXT, "hello world")
         self.assertEqual(len(children), 2)
         self.assertEqual("link1", children[0].get_attribute("name"))
         self.assertEqual("link2", children[1].get_attribute("name"))
+
+    def test_should_throw_invalidselectorexception_when_invalid_xPath_in_driver_find_element(self):
+        test_html = self.marionette.absolute_url("formPage.html")
+        self.marionette.navigate(test_html)
+        self.assertRaises(InvalidSelectorException, self.marionette.find_element, By.XPATH, "count(//input)")
+
+    def test_should_throw_invalidselectorexception_when_invalid_xpath_in_driver_find_elements(self):
+        test_html = self.marionette.absolute_url("formPage.html")
+        self.marionette.navigate(test_html)
+        self.assertRaises(InvalidSelectorException, self.marionette.find_elements, By.XPATH, "count(//input)")
+
+    def test_should_throw_invalidselectorexception_when_invalid_xpath_in_element_find_element(self):
+        test_html = self.marionette.absolute_url("formPage.html")
+        self.marionette.navigate(test_html)
+        body = self.marionette.find_element(By.TAG_NAME, "body")
+        self.assertRaises(InvalidSelectorException, body.find_element, By.XPATH, "count(//input)")
+
+    def test_should_throw_invalidselectorexception_when_invalid_xpath_in_element_find_elements(self):
+        test_html = self.marionette.absolute_url("formPage.html")
+        self.marionette.navigate(test_html)
+        body = self.marionette.find_element(By.TAG_NAME, "body")
+        self.assertRaises(InvalidSelectorException, body.find_elements, By.XPATH, "count(//input)")
+
+    def test_should_throw_invalidselectorexception_when_css_is_empty_in_driver_find_element(self):
+        test_html = self.marionette.absolute_url("formPage.html")
+        self.marionette.navigate(test_html)
+        self.assertRaises(InvalidSelectorException, self.marionette.find_element, By.CSS_SELECTOR, "")
+
+    def test_should_throw_invalidselectorexception_when_css_is_empty_in_driver_find_elements(self):
+        test_html = self.marionette.absolute_url("formPage.html")
+        self.marionette.navigate(test_html)
+        self.assertRaises(InvalidSelectorException, self.marionette.find_elements, By.CSS_SELECTOR, "")
+
+    def test_should_throw_invalidselectorexception_when_css_is_empty_in_element_find_element(self):
+        test_html = self.marionette.absolute_url("formPage.html")
+        self.marionette.navigate(test_html)
+        body = self.marionette.find_element(By.TAG_NAME, "body")
+        self.assertRaises(InvalidSelectorException, body.find_element, By.CSS_SELECTOR, "")
+
+    def test_should_throw_invalidselectorexception_when_css_is_empty_in_element_find_elements(self):
+        test_html = self.marionette.absolute_url("formPage.html")
+        self.marionette.navigate(test_html)
+        body = self.marionette.find_element(By.TAG_NAME, "body")
+        self.assertRaises(InvalidSelectorException, body.find_elements, By.CSS_SELECTOR, "")
--- a/testing/marionette/elements.js
+++ b/testing/marionette/elements.js
@@ -339,18 +339,24 @@ ElementManager.prototype = {
   find: function EM_find(container, values, searchTimeout, all, on_success, on_error, command_id) {
     let startTime = values.time ? values.time : new Date().getTime();
     let rootNode = container.shadowRoot || container.frame.document;
     let startNode = (values.element != undefined) ?
                     this.getKnownElement(values.element, container) : rootNode;
     if (this.elementStrategies.indexOf(values.using) < 0) {
       throw new InvalidSelectorError(`No such strategy: ${values.using}`);
     }
-    let found = all ? this.findElements(values.using, values.value, rootNode, startNode) :
+
+    let found;
+    try {
+      found = all ? this.findElements(values.using, values.value, rootNode, startNode) :
                       this.findElement(values.using, values.value, rootNode, startNode);
+    } catch (e) {
+      throw new InvalidSelectorError(`Given ${values.using} expression "${values.value}" is invalid`);
+    }
     let type = Object.prototype.toString.call(found);
     let isArrayLike = ((type == '[object Array]') || (type == '[object HTMLCollection]') || (type == '[object NodeList]'));
     if (found == null || (isArrayLike && found.length <= 0)) {
       if (!searchTimeout || new Date().getTime() - startTime > searchTimeout) {
         if (all) {
           on_success([], command_id); // findElements should return empty list
         } else {
           // Format message depending on strategy if necessary