/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ #![allow(missing_docs)] use std::collections::HashMap; use base::generic_channel::{GenericOneshotSender, GenericSender}; use base::id::{BrowsingContextId, WebViewId}; use cookie::Cookie; use crossbeam_channel::Sender; use euclid::default::Rect as UntypedRect; use euclid::{Rect, Size2D}; use hyper_serde::Serde; use image::RgbaImage; use malloc_size_of_derive::MallocSizeOf; use rustc_hash::FxHashMap; use serde::{Deserialize, Serialize}; use servo_geometry::{DeviceIndependentIntRect, DeviceIndependentPixel}; use style_traits::CSSPixel; use url::Url; use webdriver::error::ErrorStatus; use crate::{InputEvent, JSValue, JavaScriptEvaluationError, ScreenshotCaptureError, TraversalId}; #[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)] pub enum WebDriverUserPrompt { Alert, BeforeUnload, Confirm, Default, File, Prompt, FallbackDefault, } impl WebDriverUserPrompt { pub fn new_from_str(s: &str) -> Option { match s { "alert" => Some(WebDriverUserPrompt::Alert), "beforeUnload" => Some(WebDriverUserPrompt::BeforeUnload), "confirm" => Some(WebDriverUserPrompt::Confirm), "default" => Some(WebDriverUserPrompt::Default), "file" => Some(WebDriverUserPrompt::File), "prompt" => Some(WebDriverUserPrompt::Prompt), "fallbackDefault" => Some(WebDriverUserPrompt::FallbackDefault), _ => None, } } } #[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] pub enum WebDriverUserPromptAction { Accept, Dismiss, Ignore, } impl WebDriverUserPromptAction { pub fn new_from_str(s: &str) -> Option { match s { "accept" => Some(WebDriverUserPromptAction::Accept), "dismiss" => Some(WebDriverUserPromptAction::Dismiss), "ignore" => Some(WebDriverUserPromptAction::Ignore), _ => None, } } } /// #[derive(Clone, Copy, Debug, Default, Deserialize, Eq, MallocSizeOf, PartialEq, Serialize)] pub enum CustomHandlersAutomationMode { AutoAccept, AutoReject, #[default] None, } /// #[derive(Clone, Copy, Debug, Deserialize, Eq, MallocSizeOf, PartialEq, Serialize)] pub enum NewWindowTypeHint { Auto, Tab, Window, } /// Messages to the constellation originating from the WebDriver server. #[derive(Debug)] pub enum WebDriverCommandMsg { /// Get the window rectangle. GetWindowRect(WebViewId, GenericOneshotSender), /// Get the viewport size. GetViewportSize( WebViewId, GenericOneshotSender>, ), /// Load a URL in the top-level browsing context with the given ID. LoadUrl(WebViewId, Url, GenericSender), /// Refresh the top-level browsing context with the given ID. Refresh(WebViewId, GenericSender), /// Navigate the webview with the given ID to the previous page in the browsing context's history. GoBack(WebViewId, GenericSender), /// Navigate the webview with the given ID to the next page in the browsing context's history. GoForward(WebViewId, GenericSender), /// Pass a webdriver command to the script thread of the current pipeline /// of a browsing context. ScriptCommand(BrowsingContextId, WebDriverScriptCommand), /// Dispatch an input event to the given [`WebView`]. Once the event has been handled in the /// page DOM a single message should be sent through the [`Sender`], if provided, informing the /// WebDriver server that the inpute event has been handled. InputEvent(WebViewId, InputEvent, Option>), /// Set the outer window rectangle. SetWindowRect( WebViewId, DeviceIndependentIntRect, GenericOneshotSender, ), /// Maximize the window. Send back result window rectangle. MaximizeWebView(WebViewId, GenericOneshotSender), /// Take a screenshot of the viewport. TakeScreenshot( WebViewId, Option>, Sender>, ), /// Create a new webview that loads about:blank. The embedder will use /// the provided channels to return the top level browsing context id /// associated with the new webview, and sets a "load status sender" if provided. NewWindow( NewWindowTypeHint, GenericOneshotSender, Option>, ), /// Close the webview associated with the provided id. CloseWebView(WebViewId, GenericOneshotSender<()>), /// Focus the webview associated with the provided id. FocusWebView(WebViewId), /// Get focused webview. For now, this is only used when start new session. GetFocusedWebView(GenericOneshotSender>), /// Get webviews state GetAllWebViews(GenericOneshotSender>), /// Check whether top-level browsing context is open. IsWebViewOpen(WebViewId, GenericOneshotSender), /// Check whether browsing context is open. IsBrowsingContextOpen(BrowsingContextId, GenericOneshotSender), CurrentUserPrompt(WebViewId, GenericOneshotSender>), HandleUserPrompt( WebViewId, WebDriverUserPromptAction, GenericOneshotSender>, ), GetAlertText(WebViewId, GenericOneshotSender>), SendAlertText(WebViewId, String), FocusBrowsingContext(BrowsingContextId), Shutdown, ResetAllCookies(Sender<()>), } #[derive(Debug, Deserialize, Serialize)] pub enum WebDriverScriptCommand { AddCookie( #[serde( deserialize_with = "::hyper_serde::deserialize", serialize_with = "::hyper_serde::serialize" )] Cookie<'static>, GenericSender>, ), DeleteCookies(GenericSender>), DeleteCookie(String, GenericSender>), ElementClear(String, GenericSender>), ExecuteScriptWithCallback(String, GenericSender), FindElementsCSSSelector(String, GenericSender, ErrorStatus>>), FindElementsLinkText( String, bool, GenericSender, ErrorStatus>>, ), FindElementsTagName(String, GenericSender, ErrorStatus>>), FindElementsXpathSelector(String, GenericSender, ErrorStatus>>), FindElementElementsCSSSelector( String, String, GenericSender, ErrorStatus>>, ), FindElementElementsLinkText( String, String, bool, GenericSender, ErrorStatus>>, ), FindElementElementsTagName( String, String, GenericSender, ErrorStatus>>, ), FindElementElementsXPathSelector( String, String, GenericSender, ErrorStatus>>, ), FindShadowElementsCSSSelector( String, String, GenericSender, ErrorStatus>>, ), FindShadowElementsLinkText( String, String, bool, GenericSender, ErrorStatus>>, ), FindShadowElementsTagName( String, String, GenericSender, ErrorStatus>>, ), FindShadowElementsXPathSelector( String, String, GenericSender, ErrorStatus>>, ), GetElementShadowRoot(String, GenericSender, ErrorStatus>>), ElementClick(String, GenericSender, ErrorStatus>>), GetKnownElement(String, GenericSender>), GetKnownShadowRoot(String, GenericSender>), GetKnownWindow(String, GenericSender>), GetActiveElement(GenericSender>), GetComputedRole(String, GenericSender, ErrorStatus>>), GetCookie( String, GenericSender>>, ErrorStatus>>, ), GetCookies(GenericSender>>, ErrorStatus>>), GetElementAttribute( String, String, GenericSender, ErrorStatus>>, ), GetElementProperty(String, String, GenericSender>), GetElementCSS(String, String, GenericSender>), GetElementRect(String, GenericSender, ErrorStatus>>), GetElementTagName(String, GenericSender>), GetElementText(String, GenericSender>), GetElementInViewCenterPoint( String, GenericOneshotSender, ErrorStatus>>, ), ScrollAndGetBoundingClientRect(String, GenericSender, ErrorStatus>>), GetBrowsingContextId( WebDriverFrameId, GenericSender>, ), GetParentFrameId(GenericSender>), GetUrl(GenericSender), GetPageSource(GenericSender>), IsEnabled(String, GenericSender>), IsSelected(String, GenericSender>), GetTitle(GenericSender), /// Deal with the case of input element for Element Send Keys, which does not send keys. WillSendKeys( String, String, bool, GenericSender>, ), AddLoadStatusSender(WebViewId, GenericSender), RemoveLoadStatusSender(WebViewId), SetProtocolHandlerAutomationMode(CustomHandlersAutomationMode), } pub type WebDriverJSResult = Result; #[derive(Debug, Deserialize, Serialize)] pub enum WebDriverFrameId { Short(u16), Element(String), } #[derive(Debug, Deserialize, Serialize)] pub enum WebDriverLoadStatus { NavigationStart, // Navigation stops for any reason NavigationStop, // Document ready state is complete Complete, // Load timeout Timeout, // Navigation is blocked by a user prompt Blocked, } /// A collection of [`GenericSender`]s that are used to asynchronously communicate /// to a WebDriver server with information about application state. #[derive(Clone, Default)] pub struct WebDriverSenders { pub load_status_senders: FxHashMap>, pub script_evaluation_interrupt_sender: Option>, pub pending_traversals: HashMap>, }