summaryrefslogtreecommitdiff
path: root/Userland/Services
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2022-11-04 20:28:42 -0400
committerLinus Groh <mail@linusgroh.de>2022-11-05 01:10:03 +0000
commitb0eb45f7c7585c4f8f0aa14fd7120aa74eca6cd4 (patch)
tree4ae1ac9528a53942fa1af4cdd9a273013b059b8c /Userland/Services
parent3c1c2994f1a999d8b6767caffb49b8669cda2936 (diff)
downloadserenity-b0eb45f7c7585c4f8f0aa14fd7120aa74eca6cd4.zip
WebDriver+Browser: Implement `GET /session/{id}/element/{id}/screenshot`
Diffstat (limited to 'Userland/Services')
-rw-r--r--Userland/Services/WebDriver/Client.cpp11
-rw-r--r--Userland/Services/WebDriver/Client.h1
-rw-r--r--Userland/Services/WebDriver/Session.cpp30
-rw-r--r--Userland/Services/WebDriver/Session.h1
4 files changed, 43 insertions, 0 deletions
diff --git a/Userland/Services/WebDriver/Client.cpp b/Userland/Services/WebDriver/Client.cpp
index 844a6316b9..021adf4c7c 100644
--- a/Userland/Services/WebDriver/Client.cpp
+++ b/Userland/Services/WebDriver/Client.cpp
@@ -62,6 +62,7 @@ Vector<Client::Route> Client::s_routes = {
{ HTTP::HttpRequest::Method::DELETE, { "session", ":session_id", "cookie", ":name" }, &Client::handle_delete_cookie },
{ HTTP::HttpRequest::Method::DELETE, { "session", ":session_id", "cookie" }, &Client::handle_delete_all_cookies },
{ HTTP::HttpRequest::Method::GET, { "session", ":session_id", "screenshot" }, &Client::handle_take_screenshot },
+ { HTTP::HttpRequest::Method::GET, { "session", ":session_id", "element", ":element_id", "screenshot" }, &Client::handle_take_element_screenshot },
};
Client::Client(NonnullOwnPtr<Core::Stream::BufferedTCPSocket> socket, Core::Object* parent)
@@ -812,4 +813,14 @@ ErrorOr<JsonValue, WebDriverError> Client::handle_take_screenshot(Vector<StringV
return make_json_value(result);
}
+// 17.2 Take Element Screenshot, https://w3c.github.io/webdriver/#dfn-take-element-screenshot
+// GET /session/{session id}/element/{element id}/screenshot
+ErrorOr<JsonValue, WebDriverError> Client::handle_take_element_screenshot(Vector<StringView> const& parameters, JsonValue const&)
+{
+ dbgln_if(WEBDRIVER_DEBUG, "Handling GET /session/<session_id>/element/<element_id>/screenshot");
+ auto* session = TRY(find_session_with_id(parameters[0]));
+ auto result = TRY(session->take_element_screenshot(parameters[1]));
+ return make_json_value(result);
+}
+
}
diff --git a/Userland/Services/WebDriver/Client.h b/Userland/Services/WebDriver/Client.h
index 8367c98f5e..65820de97e 100644
--- a/Userland/Services/WebDriver/Client.h
+++ b/Userland/Services/WebDriver/Client.h
@@ -87,6 +87,7 @@ private:
ErrorOr<JsonValue, WebDriverError> handle_delete_cookie(Vector<StringView> const&, JsonValue const& payload);
ErrorOr<JsonValue, WebDriverError> handle_delete_all_cookies(Vector<StringView> const&, JsonValue const& payload);
ErrorOr<JsonValue, WebDriverError> handle_take_screenshot(Vector<StringView> const&, JsonValue const& payload);
+ ErrorOr<JsonValue, WebDriverError> handle_take_element_screenshot(Vector<StringView> const&, JsonValue const& payload);
ErrorOr<Session*, WebDriverError> find_session_with_id(StringView session_id);
JsonValue make_json_value(JsonValue const&);
diff --git a/Userland/Services/WebDriver/Session.cpp b/Userland/Services/WebDriver/Session.cpp
index 7912463718..179a7c134f 100644
--- a/Userland/Services/WebDriver/Session.cpp
+++ b/Userland/Services/WebDriver/Session.cpp
@@ -1354,4 +1354,34 @@ ErrorOr<JsonValue, WebDriverError> Session::take_screenshot()
return encoded_string;
}
+// 17.2 Take Element Screenshot, https://w3c.github.io/webdriver/#dfn-take-element-screenshot
+ErrorOr<JsonValue, WebDriverError> Session::take_element_screenshot(StringView parameter_element_id)
+{
+ // 1. If the current top-level browsing context is no longer open, return error with error code no such window.
+ TRY(check_for_open_top_level_browsing_context_or_return_error());
+
+ // FIXME: 2. Handle any user prompts and return its value if it is an error.
+
+ // 3. Let element be the result of trying to get a known connected element with url variable element id.
+ auto element_id = TRY(get_known_connected_element(parameter_element_id));
+
+ // 4. Scroll into view the element.
+ m_browser_connection->scroll_element_into_view(element_id);
+
+ // 5. When the user agent is next to run the animation frame callbacks:
+ // a. Let element rect be element’s rectangle.
+ // b. Let screenshot result be the result of trying to call draw a bounding box from the framebuffer, given element rect as an argument.
+ auto screenshot = m_browser_connection->take_element_screenshot(element_id);
+ if (!screenshot.is_valid())
+ return WebDriverError::from_code(ErrorCode::UnableToCaptureScreen, "Unable to capture screenshot"sv);
+
+ // c. Let canvas be a canvas element of screenshot result’s data.
+ // d. Let encoding result be the result of trying encoding a canvas as Base64 canvas.
+ // e. Let encoded string be encoding result’s data.
+ auto encoded_string = TRY(encode_bitmap_as_canvas_element(*screenshot.bitmap()));
+
+ // 6. Return success with data encoded string.
+ return encoded_string;
+}
+
}
diff --git a/Userland/Services/WebDriver/Session.h b/Userland/Services/WebDriver/Session.h
index 67f9cf68b5..99b0071507 100644
--- a/Userland/Services/WebDriver/Session.h
+++ b/Userland/Services/WebDriver/Session.h
@@ -76,6 +76,7 @@ public:
ErrorOr<JsonValue, WebDriverError> delete_cookie(StringView name);
ErrorOr<JsonValue, WebDriverError> delete_all_cookies();
ErrorOr<JsonValue, WebDriverError> take_screenshot();
+ ErrorOr<JsonValue, WebDriverError> take_element_screenshot(StringView element_id);
private:
void delete_cookies(Optional<StringView> const& name = {});