rustenium 1.1.5

A modern, robust, high-performance WebDriver BiDi automation library for Rust
Documentation
use std::collections::HashMap;
use std::future::Future;
use serde::Deserialize;
use rustenium_bidi_definitions::browsing_context::types::{BrowsingContext, Locator};
use rustenium_bidi_definitions::script::types::{Handle, SharedId};
use crate::error::bidi::{EvaluateResultError, InputError, MouseInputError, ScreenshotError};
use crate::input::{MouseClickOptions, MouseMoveOptions};
use crate::nodes::bidi::node::BidiNodeScreenshotOptions;

#[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,
}

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

    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_inner_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<(), EvaluateResultError>>;

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

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

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

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

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

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

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

    fn screenshot_with_options(&self, options: BidiNodeScreenshotOptions) -> impl Future<Output = Result<String, ScreenshotError>>;

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