WebDriver: locator strategy should be validated before trying to find elements
authorcarlosgc@webkit.org <carlosgc@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 30 Nov 2017 13:48:46 +0000 (13:48 +0000)
committercarlosgc@webkit.org <carlosgc@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 30 Nov 2017 13:48:46 +0000 (13:48 +0000)
https://bugs.webkit.org/show_bug.cgi?id=180187

Reviewed by Carlos Alberto Lopez Perez.

We currently rely on the js atom to raise an exception in case the locator strategy is not valid, but in case of
find element from element, if the element doesn't exist we fail with stale element error instead of invalid
argument as expected. So, let's validate the strategies when parsing them, which would also avoid going to the
browser in cae of invalid strategy.

Fixes: imported/w3c/webdriver/tests/retrieval/find_element_from_element.py::test_invalid_using_argument[a]

* WebDriverService.cpp:
(WebDriver::isValidStrategy):
(WebDriver::findStrategyAndSelectorOrCompleteWithError):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@225326 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebDriver/ChangeLog
Source/WebDriver/WebDriverService.cpp

index b85c3d9..a8e200d 100644 (file)
@@ -1,5 +1,23 @@
 2017-11-30  Carlos Garcia Campos  <cgarcia@igalia.com>
 
+        WebDriver: locator strategy should be validated before trying to find elements
+        https://bugs.webkit.org/show_bug.cgi?id=180187
+
+        Reviewed by Carlos Alberto Lopez Perez.
+
+        We currently rely on the js atom to raise an exception in case the locator strategy is not valid, but in case of
+        find element from element, if the element doesn't exist we fail with stale element error instead of invalid
+        argument as expected. So, let's validate the strategies when parsing them, which would also avoid going to the
+        browser in cae of invalid strategy.
+
+        Fixes: imported/w3c/webdriver/tests/retrieval/find_element_from_element.py::test_invalid_using_argument[a]
+
+        * WebDriverService.cpp:
+        (WebDriver::isValidStrategy):
+        (WebDriver::findStrategyAndSelectorOrCompleteWithError):
+
+2017-11-30  Carlos Garcia Campos  <cgarcia@igalia.com>
+
         WebDriver: remove elementSubmit command
         https://bugs.webkit.org/show_bug.cgi?id=180186
 
index e36b5cf..2e09764 100644 (file)
@@ -984,9 +984,20 @@ static std::optional<String> findElementOrCompleteWithError(JSON::Object& parame
     return elementID;
 }
 
+static inline bool isValidStrategy(const String& strategy)
+{
+    // ยง12.1 Locator Strategies.
+    // https://w3c.github.io/webdriver/webdriver-spec.html#dfn-table-of-location-strategies
+    return strategy == "css selector"
+        || strategy == "link text"
+        || strategy == "partial link text"
+        || strategy == "tag name"
+        || strategy == "xpath";
+}
+
 static bool findStrategyAndSelectorOrCompleteWithError(JSON::Object& parameters, Function<void (CommandResult&&)>& completionHandler, String& strategy, String& selector)
 {
-    if (!parameters.getString(ASCIILiteral("using"), strategy)) {
+    if (!parameters.getString(ASCIILiteral("using"), strategy) || !isValidStrategy(strategy)) {
         completionHandler(CommandResult::fail(CommandResult::ErrorCode::InvalidArgument));
         return false;
     }