2022-10-12 11:14:59 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, Florent Castelli <florent.castelli@gmail.com>
|
2022-10-19 16:50:16 +02:00
|
|
|
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
|
2025-02-05 11:36:19 -05:00
|
|
|
* Copyright (c) 2022-2025, Tim Flynn <trflynn89@ladybird.org>
|
2022-10-12 11:14:59 +01:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Error.h>
|
2024-11-04 08:37:16 -05:00
|
|
|
#include <AK/JsonValue.h>
|
2023-03-07 10:36:39 -05:00
|
|
|
#include <AK/RefCounted.h>
|
2022-10-12 11:14:59 +01:00
|
|
|
#include <AK/RefPtr.h>
|
2024-11-01 07:42:21 -04:00
|
|
|
#include <AK/ScopeGuard.h>
|
2023-03-06 01:57:36 +03:00
|
|
|
#include <AK/String.h>
|
2024-11-01 07:42:21 -04:00
|
|
|
#include <LibCore/EventLoop.h>
|
2024-11-17 20:11:13 +05:00
|
|
|
#include <LibCore/Process.h>
|
2022-11-08 10:18:11 -05:00
|
|
|
#include <LibCore/Promise.h>
|
2024-11-04 08:37:16 -05:00
|
|
|
#include <LibWeb/WebDriver/Capabilities.h>
|
2022-11-08 10:10:27 -05:00
|
|
|
#include <LibWeb/WebDriver/Error.h>
|
2022-11-08 09:42:36 -05:00
|
|
|
#include <LibWeb/WebDriver/Response.h>
|
2025-06-06 15:42:12 -04:00
|
|
|
#include <WebDriver/Client.h>
|
2022-11-08 10:18:11 -05:00
|
|
|
#include <WebDriver/WebContentConnection.h>
|
2022-10-12 11:14:59 +01:00
|
|
|
|
|
|
|
namespace WebDriver {
|
|
|
|
|
2023-03-07 10:36:39 -05:00
|
|
|
class Session : public RefCounted<Session> {
|
2022-10-12 11:14:59 +01:00
|
|
|
public:
|
2025-02-07 11:09:28 -05:00
|
|
|
static ErrorOr<NonnullRefPtr<Session>> create(NonnullRefPtr<Client> client, JsonObject& capabilities, Web::WebDriver::SessionFlags flags);
|
2022-10-12 11:14:59 +01:00
|
|
|
~Session();
|
|
|
|
|
2025-02-07 09:04:45 -05:00
|
|
|
enum class AllowInvalidWindowHandle {
|
|
|
|
No,
|
|
|
|
Yes,
|
|
|
|
};
|
2025-02-07 09:16:20 -05:00
|
|
|
static ErrorOr<NonnullRefPtr<Session>, Web::WebDriver::Error> find_session(StringView session_id, Web::WebDriver::SessionFlags = Web::WebDriver::SessionFlags::Default, AllowInvalidWindowHandle = AllowInvalidWindowHandle::No);
|
|
|
|
static size_t session_count(Web::WebDriver::SessionFlags);
|
2022-10-12 11:14:59 +01:00
|
|
|
|
2023-03-06 01:57:36 +03:00
|
|
|
struct Window {
|
|
|
|
String handle;
|
|
|
|
NonnullRefPtr<WebContentConnection> web_content_connection;
|
|
|
|
};
|
|
|
|
|
|
|
|
WebContentConnection& web_content_connection() const
|
2022-11-08 10:42:12 -05:00
|
|
|
{
|
2023-03-07 09:16:01 -05:00
|
|
|
auto current_window = m_windows.get(m_current_window_handle);
|
|
|
|
VERIFY(current_window.has_value());
|
|
|
|
|
|
|
|
return current_window->web_content_connection;
|
2023-03-06 01:57:36 +03:00
|
|
|
}
|
|
|
|
|
2025-02-07 09:04:45 -05:00
|
|
|
void close();
|
|
|
|
|
|
|
|
String session_id() const { return m_session_id; }
|
2025-02-07 09:16:20 -05:00
|
|
|
Web::WebDriver::SessionFlags session_flags() const { return m_session_flags; }
|
2025-02-07 09:04:45 -05:00
|
|
|
String const& current_window_handle() const { return m_current_window_handle; }
|
2022-11-08 10:42:12 -05:00
|
|
|
|
2024-10-21 14:42:23 +01:00
|
|
|
bool has_window_handle(StringView handle) const { return m_windows.contains(handle); }
|
|
|
|
|
2024-11-04 08:37:16 -05:00
|
|
|
Web::WebDriver::Response set_timeouts(JsonValue);
|
2023-03-06 01:57:36 +03:00
|
|
|
Web::WebDriver::Response close_window();
|
|
|
|
Web::WebDriver::Response switch_to_window(StringView);
|
|
|
|
Web::WebDriver::Response get_window_handles() const;
|
2024-09-27 16:53:29 -04:00
|
|
|
ErrorOr<void, Web::WebDriver::Error> ensure_current_window_handle_is_valid() const;
|
2022-10-12 11:14:59 +01:00
|
|
|
|
2024-11-01 07:42:21 -04:00
|
|
|
template<typename Action>
|
|
|
|
Web::WebDriver::Response perform_async_action(Action&& action)
|
|
|
|
{
|
|
|
|
Optional<Web::WebDriver::Response> response;
|
|
|
|
auto& connection = web_content_connection();
|
2024-10-28 23:37:11 -04:00
|
|
|
|
2024-11-01 07:42:21 -04:00
|
|
|
ScopeGuard guard { [&]() { connection.on_driver_execution_complete = nullptr; } };
|
|
|
|
connection.on_driver_execution_complete = [&](auto result) { response = move(result); };
|
2024-10-30 15:26:04 -04:00
|
|
|
|
2024-11-01 07:42:21 -04:00
|
|
|
TRY(action(connection));
|
2024-09-29 09:40:17 -04:00
|
|
|
|
2024-11-01 07:42:21 -04:00
|
|
|
Core::EventLoop::current().spin_until([&]() {
|
|
|
|
return response.has_value();
|
|
|
|
});
|
2024-10-25 12:31:07 -04:00
|
|
|
|
2024-11-01 07:42:21 -04:00
|
|
|
return response.release_value();
|
|
|
|
}
|
2024-10-30 20:29:26 -04:00
|
|
|
|
2022-10-12 11:14:59 +01:00
|
|
|
private:
|
2025-02-07 11:09:28 -05:00
|
|
|
Session(NonnullRefPtr<Client> client, JsonObject const& capabilities, String session_id, Web::WebDriver::SessionFlags flags);
|
2025-02-05 16:05:25 -05:00
|
|
|
|
2025-06-06 15:42:12 -04:00
|
|
|
ErrorOr<void> start(LaunchBrowserCallback const&);
|
2025-02-05 16:05:25 -05:00
|
|
|
|
2022-11-08 10:18:11 -05:00
|
|
|
using ServerPromise = Core::Promise<ErrorOr<void>>;
|
2022-12-15 10:40:11 -05:00
|
|
|
ErrorOr<NonnullRefPtr<Core::LocalServer>> create_server(NonnullRefPtr<ServerPromise> promise);
|
2022-11-08 10:18:11 -05:00
|
|
|
|
2022-10-12 11:14:59 +01:00
|
|
|
NonnullRefPtr<Client> m_client;
|
2022-11-22 07:38:07 -05:00
|
|
|
Web::WebDriver::LadybirdOptions m_options;
|
|
|
|
|
2025-02-07 09:04:45 -05:00
|
|
|
String m_session_id;
|
2025-02-07 11:09:28 -05:00
|
|
|
Web::WebDriver::SessionFlags m_session_flags { Web::WebDriver::SessionFlags::Default };
|
2022-11-22 07:38:07 -05:00
|
|
|
|
2023-03-06 01:57:36 +03:00
|
|
|
HashMap<String, Window> m_windows;
|
|
|
|
String m_current_window_handle;
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
Optional<ByteString> m_web_content_socket_path;
|
2024-11-17 20:11:13 +05:00
|
|
|
Optional<Core::Process> m_browser_process;
|
2023-03-06 01:45:30 +03:00
|
|
|
|
|
|
|
RefPtr<Core::LocalServer> m_web_content_server;
|
2024-11-04 08:37:16 -05:00
|
|
|
|
|
|
|
Web::WebDriver::PageLoadStrategy m_page_load_strategy { Web::WebDriver::PageLoadStrategy::Normal };
|
|
|
|
Optional<JsonValue> m_timeouts_configuration;
|
|
|
|
bool m_strict_file_interactiblity { false };
|
2022-10-12 11:14:59 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|