eoka 0.17.1

Stealth browser automation for Rust. Puppeteer/Playwright alternative with anti-bot bypass.
Documentation
use super::{Element, Page};
use crate::action::ActionCall;
use crate::error::{Error, Result};
use crate::{Action, LocatorSpec};

impl Page {
    /// Run `action` with this page's [`crate::ActionDefaults`], discarding the receipt.
    pub(crate) async fn act_default(&self, action: Action) -> Result<()> {
        self.act(action, self.action_options()).await.map(|_| ())
    }

    /// Drag horizontally from the selected element's center.
    /// For explicit cancellation cleanup, retain a [`crate::stealth::Human`] helper.
    pub fn human_drag(&self, selector: &str, dx: f64) -> ActionCall {
        ActionCall::new(
            self,
            Action::Drag {
                target: LocatorSpec::css(selector),
                dx,
                dy: 0.0,
            },
        )
    }

    /// Find the first element matching a CSS selector, in document order.
    /// Several matches are not an error. Top frame only.
    pub async fn find(&self, selector: &str) -> Result<Element> {
        crate::action::first_element(self, LocatorSpec::css(selector))
            .await?
            .ok_or_else(|| Error::element_not_found(selector.to_string()))
    }

    /// Find all elements matching a CSS selector, in document order.
    pub async fn find_all(&self, selector: &str) -> Result<Vec<Element>> {
        crate::action::all_elements(self, &LocatorSpec::css(selector)).await
    }

    /// Check if an element exists
    #[must_use = "returns true if element exists"]
    pub async fn exists(&self, selector: &str) -> bool {
        self.find(selector).await.is_ok()
    }

    /// Find the first element whose text contains `text` (case-insensitive).
    ///
    /// Interactive elements (a, button, input) win over static ones; among
    /// static elements the innermost match is returned, never a container
    /// that merely includes the text. Matching is the same as
    /// [`Page::locator_text`], but several matches are not an error.
    pub async fn find_by_text(&self, text: &str) -> Result<Element> {
        crate::action::first_element(self, LocatorSpec::text(text))
            .await?
            .ok_or_else(|| Error::element_not_found(format!("text: {text}")))
    }

    /// Click an element by selector. Awaiting proves dispatch only; add
    /// [`ActionCall::expecting`] to prove the page reacted.
    pub fn click(&self, selector: &str) -> ActionCall {
        ActionCall::new(self, Action::click(LocatorSpec::css(selector), false))
    }

    /// Type text into an element by selector. Awaiting proves dispatch only; see [`ActionCall`].
    pub fn type_into(&self, selector: &str, text: &str) -> ActionCall {
        ActionCall::new(self, Action::type_text(LocatorSpec::css(selector), text))
    }

    /// Fill a form field: click, clear, type, and verify the resulting value.
    ///
    /// Range inputs are assigned through their DOM property rather than text
    /// insertion. The requested value must be finite and satisfy the range's
    /// min/max/step constraints; accepted range values dispatch bubbling
    /// `input` and `change` events.
    pub async fn fill(&self, selector: &str, value: &str) -> Result<()> {
        self.act_default(Action::fill(LocatorSpec::css(selector), value))
            .await
    }

    /// Upload file(s) to a file input element
    pub async fn upload_file(&self, selector: &str, path: &str) -> Result<()> {
        self.upload_files(selector, &[path]).await
    }

    /// Upload multiple files to a file input element
    pub async fn upload_files(&self, selector: &str, paths: &[&str]) -> Result<()> {
        self.act_default(Action::Upload {
            target: LocatorSpec::css(selector),
            files: paths.iter().map(|v| (*v).into()).collect(),
        })
        .await
    }

    /// Select option by value
    pub async fn select(&self, selector: &str, value: &str) -> Result<()> {
        self.act_default(Action::Select {
            target: LocatorSpec::css(selector),
            values: vec![value.into()],
        })
        .await
    }

    /// Select option by visible text
    pub async fn select_by_text(&self, selector: &str, text: &str) -> Result<()> {
        self.act_default(Action::SelectText {
            target: LocatorSpec::css(selector),
            text: text.into(),
        })
        .await
    }

    /// Select multiple options by values
    pub async fn select_multiple(&self, selector: &str, values: &[&str]) -> Result<()> {
        self.act_default(Action::Select {
            target: LocatorSpec::css(selector),
            values: values.iter().map(|v| (*v).into()).collect(),
        })
        .await
    }

    /// Hover over element (for revealing menus)
    pub async fn hover(&self, selector: &str) -> Result<()> {
        self.act_default(Action::hover(LocatorSpec::css(selector)))
            .await
    }
}