rustenium 1.1.9

A modern, robust, high-performance WebDriver BiDi automation library for Rust
Documentation
use std::collections::HashMap;
use std::fmt::Debug;
use std::future::Future;
use serde::Deserialize;
use rustenium_bidi_definitions::browsing_context::types::{BrowsingContext, Locator};
use rustenium_bidi_definitions::browsing_context::commands::CaptureScreenshotOrigin;
use rustenium_bidi_definitions::browsing_context::types::ImageFormat;
use rustenium_cdp_definitions::browser_protocol::page::commands::CaptureScreenshotFormat;
use rustenium_bidi_definitions::script::types::{Handle, SharedId};
use crate::error::node::{NodeActionError, NodeInputError, NodeMouseError, NodeScreenshotError};
use crate::input::{MouseClickOptions, MouseMoveOptions};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u16)]
pub enum NodeType {
    Element = 1,
    Attribute = 2,   
    Text = 3,
    CDataSection = 4,
    ProcessingInstruction = 7,
    Comment = 8,
    Document = 9,
    DocumentType = 10,
    DocumentFragment = 11,
}

impl NodeType {
    pub fn from_u16(value: u16) -> Option<Self> {
        match value {
            1 => Some(NodeType::Element),
            2 => Some(NodeType::Attribute),
            3 => Some(NodeType::Text),
            4 => Some(NodeType::CDataSection),
            7 => Some(NodeType::ProcessingInstruction),
            8 => Some(NodeType::Comment),
            9 => Some(NodeType::Document),
            10 => Some(NodeType::DocumentType),
            11 => Some(NodeType::DocumentFragment),
            _ => None,
        }
    }

    pub fn as_u16(&self) -> u16 {
        *self as u16
    }

    pub fn is_element(&self) -> bool {
        matches!(self, NodeType::Element)
    }

    pub fn is_text(&self) -> bool {
        matches!(self, NodeType::Text)
    }
}

#[derive(Debug, Clone, PartialEq, Deserialize)]
pub struct NodePosition {
    pub x: f64,
    pub y: f64,
    pub scroll_x: f64,
    pub scroll_y: f64,
    pub width: f64,
    pub height: f64,
}

#[derive(Debug, Clone, Default)]
pub struct NodeScreenShotOptions {
    pub origin: Option<CaptureScreenshotOrigin>,
    pub bidi_format: Option<ImageFormat>,
    pub cdp_format: Option<CaptureScreenshotFormat>,
    /// Normalized quality in the range `0.0..=1.0`.
    pub quality: Option<f64>,
    pub from_surface: Option<bool>,
    pub capture_beyond_viewport: Option<bool>,
    pub optimize_for_speed: Option<bool>,
    pub save_path: Option<String>,
}

pub trait Node {
    fn get_children_nodes(&self) -> &Vec<impl Node>;

    fn get_local_name(&self) -> Option<&str>;

    fn get_node_type(&self) -> Option<NodeType>;

    fn get_bidi_locator(&self) -> &Locator;

    fn get_inner_text(&self) -> impl Future<Output = String>;

    fn get_text_content(&self) -> impl Future<Output = String>;

    fn get_html(&self) -> impl Future<Output = String>;

    fn get_attribute(&self, attribute_name: &str) -> Option<serde_json::Value>;

    fn get_attributes(&self) -> HashMap<String, serde_json::Value>;

    fn get_position(&mut self) -> impl Future<Output = Option<&NodePosition>>;

    fn get_context_id(&self) -> &BrowsingContext;

    fn get_shared_id(&self) -> Option<&SharedId>;

    fn get_handle(&self) -> &Option<Handle>;

    fn set_position(&mut self, position: NodePosition) -> ();

    fn scroll_into_view(&self) -> impl Future<Output = Result<(), NodeActionError>>;

    fn is_visible(&self) -> impl Future<Output = Result<bool, NodeActionError>>;

    fn delete(&self) -> impl Future<Output = Result<(), NodeActionError>>;

    fn mouse_move(&mut self) -> impl Future<Output = Result<(), NodeMouseError>>;

    fn mouse_move_with_options(&mut self, options: MouseMoveOptions) -> impl Future<Output = Result<(), NodeMouseError>>;

    fn mouse_click(&mut self) -> impl Future<Output = Result<(), NodeMouseError>>;

    fn mouse_click_with_options(&mut self, options: MouseClickOptions) -> impl Future<Output = Result<(), NodeMouseError>>;

    fn screenshot(&mut self) -> impl Future<Output = Result<String, NodeScreenshotError>>;

    fn screenshot_with_options(&mut self, options: NodeScreenShotOptions) -> impl Future<Output = Result<String, NodeScreenshotError>>;

    /// Focuses the element and types the given text into it.
    fn type_text(&mut self, text: String) -> impl Future<Output = Result<(), NodeInputError>>;
}