Bläddra i källkod

LibWeb+LibWebView+WebContent+WebDriver: Implement Send Alert Text

Timothy Flynn 2 år sedan
förälder
incheckning
f7bb835d09

+ 1 - 0
Userland/Libraries/LibWeb/WebDriver/Client.cpp

@@ -96,6 +96,7 @@ static constexpr auto s_webdriver_endpoints = Array {
     ROUTE(POST, "/session/:session_id/alert/dismiss"sv, dismiss_alert),
     ROUTE(POST, "/session/:session_id/alert/accept"sv, accept_alert),
     ROUTE(GET, "/session/:session_id/alert/text"sv, get_alert_text),
+    ROUTE(POST, "/session/:session_id/alert/text"sv, send_alert_text),
     ROUTE(GET, "/session/:session_id/screenshot"sv, take_screenshot),
     ROUTE(GET, "/session/:session_id/element/:element_id/screenshot"sv, take_element_screenshot),
 };

+ 1 - 0
Userland/Libraries/LibWeb/WebDriver/Client.h

@@ -90,6 +90,7 @@ public:
     virtual Response dismiss_alert(Parameters parameters, JsonValue payload) = 0;
     virtual Response accept_alert(Parameters parameters, JsonValue payload) = 0;
     virtual Response get_alert_text(Parameters parameters, JsonValue payload) = 0;
+    virtual Response send_alert_text(Parameters parameters, JsonValue payload) = 0;
 
     // 17. Screen capture, https://w3c.github.io/webdriver/#screen-capture
     virtual Response take_screenshot(Parameters parameters, JsonValue payload) = 0;

+ 6 - 0
Userland/Libraries/LibWebView/OutOfProcessWebView.cpp

@@ -378,6 +378,12 @@ void OutOfProcessWebView::notify_server_did_request_prompt(Badge<WebContentClien
     m_dialog = nullptr;
 }
 
+void OutOfProcessWebView::notify_server_did_request_set_prompt_text(Badge<WebContentClient>, String const& message)
+{
+    if (m_dialog && is<GUI::InputBox>(*m_dialog))
+        static_cast<GUI::InputBox&>(*m_dialog).set_text_value(message);
+}
+
 void OutOfProcessWebView::notify_server_did_request_accept_dialog(Badge<WebContentClient>)
 {
     if (m_dialog)

+ 1 - 0
Userland/Libraries/LibWebView/OutOfProcessWebView.h

@@ -158,6 +158,7 @@ private:
     virtual void notify_server_did_request_alert(Badge<WebContentClient>, String const& message) override;
     virtual void notify_server_did_request_confirm(Badge<WebContentClient>, String const& message) override;
     virtual void notify_server_did_request_prompt(Badge<WebContentClient>, String const& message, String const& default_) override;
+    virtual void notify_server_did_request_set_prompt_text(Badge<WebContentClient>, String const& message) override;
     virtual void notify_server_did_request_accept_dialog(Badge<WebContentClient>) override;
     virtual void notify_server_did_request_dismiss_dialog(Badge<WebContentClient>) override;
     virtual void notify_server_did_get_source(const AK::URL& url, String const& source) override;

+ 1 - 0
Userland/Libraries/LibWebView/ViewImplementation.h

@@ -44,6 +44,7 @@ public:
     virtual void notify_server_did_request_alert(Badge<WebContentClient>, String const& message) = 0;
     virtual void notify_server_did_request_confirm(Badge<WebContentClient>, String const& message) = 0;
     virtual void notify_server_did_request_prompt(Badge<WebContentClient>, String const& message, String const& default_) = 0;
+    virtual void notify_server_did_request_set_prompt_text(Badge<WebContentClient>, String const& message) = 0;
     virtual void notify_server_did_request_accept_dialog(Badge<WebContentClient>) = 0;
     virtual void notify_server_did_request_dismiss_dialog(Badge<WebContentClient>) = 0;
     virtual void notify_server_did_get_source(const AK::URL& url, String const& source) = 0;

+ 5 - 0
Userland/Libraries/LibWebView/WebContentClient.cpp

@@ -191,6 +191,11 @@ void WebContentClient::did_request_prompt(String const& message, String const& d
     m_view.notify_server_did_request_prompt({}, message, default_);
 }
 
+void WebContentClient::did_request_set_prompt_text(String const& message)
+{
+    m_view.notify_server_did_request_set_prompt_text({}, message);
+}
+
 void WebContentClient::did_request_accept_dialog()
 {
     m_view.notify_server_did_request_accept_dialog({});

+ 1 - 0
Userland/Libraries/LibWebView/WebContentClient.h

@@ -60,6 +60,7 @@ private:
     virtual void did_request_alert(String const&) override;
     virtual void did_request_confirm(String const&) override;
     virtual void did_request_prompt(String const&, String const&) override;
+    virtual void did_request_set_prompt_text(String const& message) override;
     virtual void did_request_accept_dialog() override;
     virtual void did_request_dismiss_dialog() override;
     virtual Messages::WebContentClient::DidRequestAllCookiesResponse did_request_all_cookies(AK::URL const&) override;

+ 1 - 0
Userland/Services/WebContent/WebContentClient.ipc

@@ -32,6 +32,7 @@ endpoint WebContentClient
     did_request_alert(String message) =|
     did_request_confirm(String message) =|
     did_request_prompt(String message, String default_) =|
+    did_request_set_prompt_text(String message) =|
     did_request_accept_dialog() =|
     did_request_dismiss_dialog() =|
     did_get_source(URL url, String source) =|

+ 1 - 0
Userland/Services/WebContent/WebDriverClient.ipc

@@ -46,6 +46,7 @@ endpoint WebDriverClient {
     dismiss_alert() => (Web::WebDriver::Response response)
     accept_alert() => (Web::WebDriver::Response response)
     get_alert_text() => (Web::WebDriver::Response response)
+    send_alert_text(JsonValue payload) => (Web::WebDriver::Response response)
     take_screenshot() => (Web::WebDriver::Response response)
     take_element_screenshot(String element_id) => (Web::WebDriver::Response response)
 }

+ 41 - 0
Userland/Services/WebContent/WebDriverConnection.cpp

@@ -1396,6 +1396,47 @@ Messages::WebDriverClient::GetAlertTextResponse WebDriverConnection::get_alert_t
     return JsonValue {};
 }
 
+// 16.4 Send Alert Text, https://w3c.github.io/webdriver/#send-alert-text
+Messages::WebDriverClient::SendAlertTextResponse WebDriverConnection::send_alert_text(JsonValue const& payload)
+{
+    // 1. Let text be the result of getting the property "text" from parameters.
+    // 2. If text is not a String, return error with error code invalid argument.
+    auto text = TRY(get_property(payload, "text"sv));
+
+    // 3. If the current top-level browsing context is no longer open, return error with error code no such window.
+    TRY(ensure_open_top_level_browsing_context());
+
+    // 4. If there is no current user prompt, return error with error code no such alert.
+    if (!m_page_host.has_pending_dialog())
+        return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchAlert, "No user dialog is currently open"sv);
+
+    // 5. Run the substeps of the first matching current user prompt:
+    switch (m_page_host.pending_dialog()) {
+    // -> alert
+    // -> confirm
+    case PageHost::PendingDialog::Alert:
+    case PageHost::PendingDialog::Confirm:
+        // Return error with error code element not interactable.
+        return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::ElementNotInteractable, "Only prompt dialogs may receive text"sv);
+
+    // -> prompt
+    case PageHost::PendingDialog::Prompt:
+        // Do nothing.
+        break;
+
+    // -> Otherwise
+    default:
+        // Return error with error code unsupported operation.
+        return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::UnsupportedOperation, "Unknown dialog type"sv);
+    }
+
+    // 6. Perform user agent dependent steps to set the value of current user prompt’s text field to text.
+    m_web_content_client.async_did_request_set_prompt_text(move(text));
+
+    // 7. Return success with data null.
+    return JsonValue {};
+}
+
 // 17.1 Take Screenshot, https://w3c.github.io/webdriver/#take-screenshot
 Messages::WebDriverClient::TakeScreenshotResponse WebDriverConnection::take_screenshot()
 {

+ 1 - 0
Userland/Services/WebContent/WebDriverConnection.h

@@ -81,6 +81,7 @@ private:
     virtual Messages::WebDriverClient::DismissAlertResponse dismiss_alert() override;
     virtual Messages::WebDriverClient::AcceptAlertResponse accept_alert() override;
     virtual Messages::WebDriverClient::GetAlertTextResponse get_alert_text() override;
+    virtual Messages::WebDriverClient::SendAlertTextResponse send_alert_text(JsonValue const& payload) override;
     virtual Messages::WebDriverClient::TakeScreenshotResponse take_screenshot() override;
     virtual Messages::WebDriverClient::TakeElementScreenshotResponse take_element_screenshot(String const& element_id) override;
 

+ 9 - 0
Userland/Services/WebDriver/Client.cpp

@@ -566,6 +566,15 @@ Web::WebDriver::Response Client::get_alert_text(Web::WebDriver::Parameters param
     return session->web_content_connection().get_alert_text();
 }
 
+// 16.4 Send Alert Text, https://w3c.github.io/webdriver/#send-alert-text
+// POST /session/{session id}/alert/text
+Web::WebDriver::Response Client::send_alert_text(Web::WebDriver::Parameters parameters, JsonValue payload)
+{
+    dbgln_if(WEBDRIVER_DEBUG, "Handling POST /session/<session_id>/alert/text");
+    auto* session = TRY(find_session_with_id(parameters[0]));
+    return session->web_content_connection().send_alert_text(payload);
+}
+
 // 17.1 Take Screenshot, https://w3c.github.io/webdriver/#take-screenshot
 // GET /session/{session id}/screenshot
 Web::WebDriver::Response Client::take_screenshot(Web::WebDriver::Parameters parameters, JsonValue)

+ 1 - 0
Userland/Services/WebDriver/Client.h

@@ -79,6 +79,7 @@ private:
     virtual Web::WebDriver::Response dismiss_alert(Web::WebDriver::Parameters parameters, JsonValue payload) override;
     virtual Web::WebDriver::Response accept_alert(Web::WebDriver::Parameters parameters, JsonValue payload) override;
     virtual Web::WebDriver::Response get_alert_text(Web::WebDriver::Parameters parameters, JsonValue payload) override;
+    virtual Web::WebDriver::Response send_alert_text(Web::WebDriver::Parameters parameters, JsonValue payload) override;
     virtual Web::WebDriver::Response take_screenshot(Web::WebDriver::Parameters parameters, JsonValue payload) override;
     virtual Web::WebDriver::Response take_element_screenshot(Web::WebDriver::Parameters parameters, JsonValue payload) override;