1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 22:27:42 +00:00

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"
  }
}
```
This commit is contained in:
Linus Groh 2023-04-20 17:41:32 +01:00 committed by Tim Flynn
parent 84a231d4e5
commit e6be5c37c0
8 changed files with 88 additions and 5 deletions

View file

@ -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

View file

@ -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();
}
}

View file

@ -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&);

View file

@ -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 windows browsing context.
object.set(identifier, window.associated_browsing_context()->window_handle().to_deprecated_string());
return object;
}
}

View file

@ -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&);
}

View file

@ -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);

View file

@ -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 {

View file

@ -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();