WebDriver: implement get page source command
authorcarlosgc@webkit.org <carlosgc@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 8 Nov 2019 15:09:00 +0000 (15:09 +0000)
committercarlosgc@webkit.org <carlosgc@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 8 Nov 2019 15:09:00 +0000 (15:09 +0000)
https://bugs.webkit.org/show_bug.cgi?id=180399

Reviewed by Carlos Alberto Lopez Perez.

Source/WebDriver:

* Session.cpp:
(WebDriver::Session::getPageSource):
* Session.h:
* WebDriverService.cpp:
(WebDriver::WebDriverService::getPageSource):
* WebDriverService.h:

WebDriverTests:

Unskip tests that are now passing.

* TestExpectations.json:

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

Source/WebDriver/ChangeLog
Source/WebDriver/Session.cpp
Source/WebDriver/Session.h
Source/WebDriver/WebDriverService.cpp
Source/WebDriver/WebDriverService.h
WebDriverTests/ChangeLog
WebDriverTests/TestExpectations.json

index a006f2b..4633e00 100644 (file)
@@ -1,3 +1,17 @@
+2019-11-08  Carlos Garcia Campos  <cgarcia@igalia.com>
+
+        WebDriver: implement get page source command
+        https://bugs.webkit.org/show_bug.cgi?id=180399
+
+        Reviewed by Carlos Alberto Lopez Perez.
+
+        * Session.cpp:
+        (WebDriver::Session::getPageSource):
+        * Session.h:
+        * WebDriverService.cpp:
+        (WebDriver::WebDriverService::getPageSource):
+        * WebDriverService.h:
+
 2019-11-07  Carlos Garcia Campos  <cgarcia@igalia.com>
 
         WebDriver: correctly handle errors when focusing element before sending key events
index d926031..22c66b3 100644 (file)
@@ -1819,6 +1819,42 @@ void Session::elementSendKeys(const String& elementID, const String& text, Funct
     });
 }
 
+void Session::getPageSource(Function<void (CommandResult&&)>&& completionHandler)
+{
+    if (!m_toplevelBrowsingContext) {
+        completionHandler(CommandResult::fail(CommandResult::ErrorCode::NoSuchWindow));
+        return;
+    }
+
+    handleUserPrompts([this, protectedThis = makeRef(*this), completionHandler = WTFMove(completionHandler)](CommandResult&& result) mutable {
+        if (result.isError()) {
+            completionHandler(WTFMove(result));
+            return;
+        }
+        RefPtr<JSON::Object> parameters = JSON::Object::create();
+        parameters->setString("browsingContextHandle"_s, m_toplevelBrowsingContext.value());
+        parameters->setString("function"_s, "function() { return document.documentElement.outerHTML; }"_s);
+        parameters->setArray("arguments"_s, JSON::Array::create());
+        m_host->sendCommandToBackend("evaluateJavaScriptFunction"_s, WTFMove(parameters), [protectedThis = protectedThis.copyRef(), completionHandler = WTFMove(completionHandler)](SessionHost::CommandResponse&& response) {
+            if (response.isError || !response.responseObject) {
+                completionHandler(CommandResult::fail(WTFMove(response.responseObject)));
+                return;
+            }
+            String valueString;
+            if (!response.responseObject->getString("result"_s, valueString)) {
+                completionHandler(CommandResult::fail(CommandResult::ErrorCode::UnknownError));
+                return;
+            }
+            RefPtr<JSON::Value> resultValue;
+            if (!JSON::Value::parseJSON(valueString, resultValue)) {
+                completionHandler(CommandResult::fail(CommandResult::ErrorCode::UnknownError));
+                return;
+            }
+            completionHandler(CommandResult::success(WTFMove(resultValue)));
+        });
+    });
+}
+
 RefPtr<JSON::Value> Session::handleScriptResult(RefPtr<JSON::Value>&& resultValue)
 {
     RefPtr<JSON::Array> resultArray;
index b3d6dad..466abe1 100644 (file)
@@ -107,6 +107,7 @@ public:
     void elementClick(const String& elementID, Function<void (CommandResult&&)>&&);
     void elementClear(const String& elementID, Function<void (CommandResult&&)>&&);
     void elementSendKeys(const String& elementID, const String& text, Function<void (CommandResult&&)>&&);
+    void getPageSource(Function<void (CommandResult&&)>&&);
     void executeScript(const String& script, RefPtr<JSON::Array>&& arguments, ExecuteScriptMode, Function<void (CommandResult&&)>&&);
     void getAllCookies(Function<void (CommandResult&&)>&&);
     void getNamedCookie(const String& name, Function<void (CommandResult&&)>&&);
index d69e149..897850e 100644 (file)
@@ -153,6 +153,7 @@ const WebDriverService::Command WebDriverService::s_commands[] = {
     { HTTPMethod::Post, "/session/$sessionId/element/$elementId/clear", &WebDriverService::elementClear },
     { HTTPMethod::Post, "/session/$sessionId/element/$elementId/value", &WebDriverService::elementSendKeys },
 
+    { HTTPMethod::Get, "/session/$sessionId/source", &WebDriverService::getPageSource },
     { HTTPMethod::Post, "/session/$sessionId/execute/sync", &WebDriverService::executeScript },
     { HTTPMethod::Post, "/session/$sessionId/execute/async", &WebDriverService::executeAsyncScript },
 
@@ -1395,6 +1396,16 @@ void WebDriverService::elementSendKeys(RefPtr<JSON::Object>&& parameters, Functi
     m_session->elementSendKeys(elementID.value(), text, WTFMove(completionHandler));
 }
 
+void WebDriverService::getPageSource(RefPtr<JSON::Object>&& parameters, Function<void (CommandResult&&)>&& completionHandler)
+{
+    // ยง15.1 Getting Page Source.
+    // https://w3c.github.io/webdriver/webdriver-spec.html#getting-page-source
+    if (!findSessionOrCompleteWithError(*parameters, completionHandler))
+        return;
+
+    m_session->getPageSource(WTFMove(completionHandler));
+}
+
 static bool findScriptAndArgumentsOrCompleteWithError(JSON::Object& parameters, Function<void (CommandResult&&)>& completionHandler, String& script, RefPtr<JSON::Array>& arguments)
 {
     if (!parameters.getString("script"_s, script)) {
index c90d619..7fb0ebe 100644 (file)
@@ -99,6 +99,7 @@ private:
     void elementClick(RefPtr<JSON::Object>&&, Function<void (CommandResult&&)>&&);
     void elementClear(RefPtr<JSON::Object>&&, Function<void (CommandResult&&)>&&);
     void elementSendKeys(RefPtr<JSON::Object>&&, Function<void (CommandResult&&)>&&);
+    void getPageSource(RefPtr<JSON::Object>&&, Function<void (CommandResult&&)>&&);
     void executeScript(RefPtr<JSON::Object>&&, Function<void (CommandResult&&)>&&);
     void executeAsyncScript(RefPtr<JSON::Object>&&, Function<void (CommandResult&&)>&&);
     void getAllCookies(RefPtr<JSON::Object>&&, Function<void (CommandResult&&)>&&);
index 49cc5e8..39d8eea 100644 (file)
@@ -1,3 +1,14 @@
+2019-11-08  Carlos Garcia Campos  <cgarcia@igalia.com>
+
+        WebDriver: implement get page source command
+        https://bugs.webkit.org/show_bug.cgi?id=180399
+
+        Reviewed by Carlos Alberto Lopez Perez.
+
+        Unskip tests that are now passing.
+
+        * TestExpectations.json:
+
 2019-11-07  Carlos Garcia Campos  <cgarcia@igalia.com>
 
         WebDriver: correctly handle errors when focusing element before sending key events
index 152aed8..83cd750 100644 (file)
             }
         }
     },
-    "imported/w3c/webdriver/tests/get_page_source/source.py": {
-        "expected": {"all": {"status": ["SKIP"], "bug": "webkit.org/b/180399"}}
-    },
-    "imported/w3c/webdriver/tests/get_page_source/user_prompts.py": {
-        "expected": {"all": {"status": ["SKIP"], "bug": "webkit.org/b/180399"}}
-    },
     "imported/w3c/webdriver/tests/element_click/bubbling.py": {
         "subtests": {
             "test_spin_event_loop": {