summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2023-04-20 17:41:32 +0100
committerTim Flynn <trflynn89@pm.me>2023-04-20 14:41:31 -0400
commite6be5c37c0ef16b2545ac2bd9cabfb74836a1622 (patch)
tree20a4f7d35c49778767dd83ec9502a9bb7294399b
parent84a231d4e51c7729480bb33e9bb342449272c01e (diff)
downloadserenity-e6be5c37c0ef16b2545ac2bd9cabfb74836a1622.zip
LibWeb/WebDriver: Handle WindowProxy in internal_json_clone_algorithm()
To test: ```console curl http://0.0.0.0:8000/session \ -H 'Content-Type: application/json' \ -d '{"capabilities": {}}' curl http://0.0.0.0:8000/session/0/execute/sync \ -H 'Content-Type: application/json' \ -d '{"script": "return window;", "args": []}' ``` Which should result in: ```json { "value": { "window-fcc6-11e5-b4f8-330a88ab9d7f": "86307df6-e2f1-4175-85cb-77295ff90898" } } ```
-rw-r--r--Userland/Libraries/LibWeb/CMakeLists.txt1
-rw-r--r--Userland/Libraries/LibWeb/HTML/WindowProxy.cpp6
-rw-r--r--Userland/Libraries/LibWeb/HTML/WindowProxy.h2
-rw-r--r--Userland/Libraries/LibWeb/WebDriver/Contexts.cpp33
-rw-r--r--Userland/Libraries/LibWeb/WebDriver/Contexts.h23
-rw-r--r--Userland/Libraries/LibWeb/WebDriver/ExecuteScript.cpp21
-rw-r--r--Userland/Libraries/LibWeb/WebDriver/ExecuteScript.h3
-rw-r--r--Userland/Services/WebContent/WebDriverConnection.cpp4
8 files changed, 88 insertions, 5 deletions
diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt
index 4f43fe77d8..9aeef1a262 100644
--- a/Userland/Libraries/LibWeb/CMakeLists.txt
+++ b/Userland/Libraries/LibWeb/CMakeLists.txt
@@ -521,6 +521,7 @@ set(SOURCES
WebAssembly/WebAssembly.cpp
WebDriver/Capabilities.cpp
WebDriver/Client.cpp
+ WebDriver/Contexts.cpp
WebDriver/ElementLocationStrategies.cpp
WebDriver/Error.cpp
WebDriver/ExecuteScript.cpp
diff --git a/Userland/Libraries/LibWeb/HTML/WindowProxy.cpp b/Userland/Libraries/LibWeb/HTML/WindowProxy.cpp
index e6841461d0..ea854f1790 100644
--- a/Userland/Libraries/LibWeb/HTML/WindowProxy.cpp
+++ b/Userland/Libraries/LibWeb/HTML/WindowProxy.cpp
@@ -10,6 +10,7 @@
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/PropertyDescriptor.h>
#include <LibJS/Runtime/PropertyKey.h>
+#include <LibWeb/DOM/Document.h>
#include <LibWeb/HTML/CrossOrigin/AbstractOperations.h>
#include <LibWeb/HTML/CrossOrigin/Reporting.h>
#include <LibWeb/HTML/Scripting/Environments.h>
@@ -260,4 +261,9 @@ void WindowProxy::set_window(Badge<BrowsingContext>, JS::NonnullGCPtr<Window> wi
m_window = window;
}
+JS::NonnullGCPtr<BrowsingContext> WindowProxy::associated_browsing_context() const
+{
+ return *m_window->associated_document().browsing_context();
+}
+
}
diff --git a/Userland/Libraries/LibWeb/HTML/WindowProxy.h b/Userland/Libraries/LibWeb/HTML/WindowProxy.h
index 0b1de46879..7cccafb354 100644
--- a/Userland/Libraries/LibWeb/HTML/WindowProxy.h
+++ b/Userland/Libraries/LibWeb/HTML/WindowProxy.h
@@ -34,6 +34,8 @@ public:
JS::GCPtr<Window> window() const { return m_window; }
void set_window(Badge<BrowsingContext>, JS::NonnullGCPtr<Window>);
+ JS::NonnullGCPtr<BrowsingContext> associated_browsing_context() const;
+
private:
explicit WindowProxy(JS::Realm&);
diff --git a/Userland/Libraries/LibWeb/WebDriver/Contexts.cpp b/Userland/Libraries/LibWeb/WebDriver/Contexts.cpp
new file mode 100644
index 0000000000..a36b08df94
--- /dev/null
+++ b/Userland/Libraries/LibWeb/WebDriver/Contexts.cpp
@@ -0,0 +1,33 @@
+/*
+ * Copyright (c) 2023, Linus Groh <linusg@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <AK/JsonObject.h>
+#include <LibWeb/HTML/BrowsingContext.h>
+#include <LibWeb/HTML/WindowProxy.h>
+#include <LibWeb/WebDriver/Contexts.h>
+
+namespace Web::WebDriver {
+
+// https://w3c.github.io/webdriver/#dfn-windowproxy-reference-object
+JsonObject window_proxy_reference_object(HTML::WindowProxy const& window)
+{
+ // 1. Let identifier be the web window identifier if the associated browsing context of window is a top-level browsing context.
+ // Otherwise let it be the web frame identifier.
+ auto identifier = window.associated_browsing_context()->is_top_level()
+ ? WEB_WINDOW_IDENTIFIER
+ : WEB_FRAME_IDENTIFIER;
+
+ // 2. Return a JSON Object initialized with the following properties:
+ JsonObject object;
+
+ // identifier
+ // Associated window handle of the window’s browsing context.
+ object.set(identifier, window.associated_browsing_context()->window_handle().to_deprecated_string());
+
+ return object;
+}
+
+}
diff --git a/Userland/Libraries/LibWeb/WebDriver/Contexts.h b/Userland/Libraries/LibWeb/WebDriver/Contexts.h
new file mode 100644
index 0000000000..11fa2c82f7
--- /dev/null
+++ b/Userland/Libraries/LibWeb/WebDriver/Contexts.h
@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 2023, Linus Groh <linusg@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/Forward.h>
+#include <AK/StringView.h>
+#include <LibWeb/Forward.h>
+
+namespace Web::WebDriver {
+
+// https://w3c.github.io/webdriver/#dfn-web-window-identifier
+static constexpr auto WEB_WINDOW_IDENTIFIER = "window-fcc6-11e5-b4f8-330a88ab9d7f"sv;
+
+// https://w3c.github.io/webdriver/#dfn-web-frame-identifier
+static constexpr auto WEB_FRAME_IDENTIFIER = "frame-075b-4da1-b6ba-e579c2d3230a"sv;
+
+JsonObject window_proxy_reference_object(HTML::WindowProxy const&);
+
+}
diff --git a/Userland/Libraries/LibWeb/WebDriver/ExecuteScript.cpp b/Userland/Libraries/LibWeb/WebDriver/ExecuteScript.cpp
index ec1bb716c8..f19dc11b7f 100644
--- a/Userland/Libraries/LibWeb/WebDriver/ExecuteScript.cpp
+++ b/Userland/Libraries/LibWeb/WebDriver/ExecuteScript.cpp
@@ -27,6 +27,7 @@
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/Page/Page.h>
+#include <LibWeb/WebDriver/Contexts.h>
#include <LibWeb/WebDriver/ExecuteScript.h>
namespace Web::WebDriver {
@@ -104,10 +105,22 @@ static ErrorOr<JsonValue, ExecuteScriptResultType> internal_json_clone_algorithm
if (value.is_bigint() || value.is_symbol())
return ExecuteScriptResultType::JavaScriptError;
- // FIXME: - a collection
- // FIXME: - instance of element
- // FIXME: - instance of shadow root
- // FIXME: - a WindowProxy object
+ // FIXME: -> a collection
+ // FIXME: -> instance of element
+ // FIXME: -> instance of shadow root
+
+ // -> a WindowProxy object
+ if (is<HTML::WindowProxy>(value.as_object())) {
+ auto const& window_proxy = static_cast<HTML::WindowProxy&>(value.as_object());
+
+ // If the associated browsing context of the WindowProxy object in value has been discarded, return error with
+ // error code stale element reference.
+ if (window_proxy.associated_browsing_context()->has_been_discarded())
+ return ExecuteScriptResultType::BrowsingContextDiscarded;
+
+ // Otherwise return success with data set to WindowProxy reference object for value.
+ return window_proxy_reference_object(window_proxy);
+ }
// -> has an own property named "toJSON" that is a Function
auto to_json = value.as_object().get_without_side_effects(vm.names.toJSON);
diff --git a/Userland/Libraries/LibWeb/WebDriver/ExecuteScript.h b/Userland/Libraries/LibWeb/WebDriver/ExecuteScript.h
index 849bd2b1f1..16b2ec7b5f 100644
--- a/Userland/Libraries/LibWeb/WebDriver/ExecuteScript.h
+++ b/Userland/Libraries/LibWeb/WebDriver/ExecuteScript.h
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
+ * Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@@ -19,6 +19,7 @@ enum class ExecuteScriptResultType {
PromiseRejected,
Timeout,
JavaScriptError,
+ BrowsingContextDiscarded,
};
struct ExecuteScriptResult {
diff --git a/Userland/Services/WebContent/WebDriverConnection.cpp b/Userland/Services/WebContent/WebDriverConnection.cpp
index 7b3401b0cd..f718189823 100644
--- a/Userland/Services/WebContent/WebDriverConnection.cpp
+++ b/Userland/Services/WebContent/WebDriverConnection.cpp
@@ -1460,6 +1460,8 @@ Messages::WebDriverClient::ExecuteScriptResponse WebDriverConnection::execute_sc
case Web::WebDriver::ExecuteScriptResultType::PromiseRejected:
case Web::WebDriver::ExecuteScriptResultType::JavaScriptError:
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::JavascriptError, "Script returned an error", move(result.value));
+ case Web::WebDriver::ExecuteScriptResultType::BrowsingContextDiscarded:
+ return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::StaleElementReference, "Browsing context has been discarded", move(result.value));
}
VERIFY_NOT_REACHED();
@@ -1492,6 +1494,8 @@ Messages::WebDriverClient::ExecuteAsyncScriptResponse WebDriverConnection::execu
case Web::WebDriver::ExecuteScriptResultType::PromiseRejected:
case Web::WebDriver::ExecuteScriptResultType::JavaScriptError:
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::JavascriptError, "Script returned an error", move(result.value));
+ case Web::WebDriver::ExecuteScriptResultType::BrowsingContextDiscarded:
+ return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::StaleElementReference, "Browsing context has been discarded", move(result.value));
}
VERIFY_NOT_REACHED();