Bug 1241067: Handle Invalid Selector values when finding elements r=ato
authorDavid Burns <dburns@mozilla.com>
Thu, 21 Jan 2016 21:07:36 +0000
changeset 281006 efa0471875af3cb25ea1698af822e7fc5f66fd12
parent 281005 38c6162cefb9edb248f00b0a2372f60c3f158c76
child 281007 6a0f2536c40713b4941c091b31868a183366b727
push id70654
push userdburns@mozilla.com
push dateThu, 21 Jan 2016 22:09:39 +0000
treeherdermozilla-inbound@efa0471875af [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersato
bugs1241067
milestone46.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 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