eoka 0.13.0

Stealth browser automation for Rust. Puppeteer/Playwright alternative with anti-bot bypass.
Documentation
//! Per-call builders behind the shorthand input methods such as `Page::click`.

use std::{future::Future, future::IntoFuture, pin::Pin, time::Duration};

use super::{Action, ActionOptions, ActionReceipt, Destination, Expectation};
use crate::{DispatchState, Error, ErrorCode, Page, Result};

type ActionFuture<T> = Pin<Box<dyn Future<Output = Result<T>> + Send>>;

/// One shorthand input action, run when awaited.
///
/// Without an expectation, a successful `.await` proves only that the input was
/// dispatched to an actionable target: the receipt's outcome is
/// [`OutcomeUnknown`](crate::ActionOutcome::OutcomeUnknown). Use
/// [`expecting`](Self::expecting) to prove the page reacted, or
/// [`Page::act`] to require that proof. Actions are never replayed after dispatch.
///
/// `press_key` has no target, so it succeeds even if nothing handles the key.
/// `type_text` fails unless an editable element has focus. Input that opens a
/// JavaScript dialog blocks until the deadline, and is then reported as an error.
#[must_use = "actions do nothing until awaited"]
pub struct ActionCall {
    page: Page,
    action: Action,
    options: ActionOptions,
}

impl ActionCall {
    pub(crate) fn new(page: &Page, action: Action) -> Self {
        Self {
            page: page.clone(),
            action,
            options: page.action_defaults.options(),
        }
    }

    /// Require this condition to become true after dispatch, for this call only.
    /// It must not already hold, so that it proves a new result.
    pub fn expecting(mut self, expect: Expectation) -> Self {
        self.options.expect = Some(expect);
        self
    }

    /// Set the total deadline, including queueing and verification. It must be
    /// between 1ms and 24h; a sub-millisecond remainder rounds up to a whole
    /// millisecond, and zero is rejected with `invalid_timeout`.
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.options.timeout_ms = super::types::duration_to_ms(timeout);
        self
    }

    /// Set where the expectation may be observed. Does not change the input target.
    pub fn destination(mut self, destination: Destination) -> Self {
        self.options.destination = destination;
        self
    }
}

impl IntoFuture for ActionCall {
    type Output = Result<ActionReceipt>;
    type IntoFuture = ActionFuture<ActionReceipt>;

    fn into_future(self) -> Self::IntoFuture {
        Box::pin(async move { self.page.explore(self.action, self.options).await })
    }
}

/// An [`ActionCall`] that skips an unavailable target instead of waiting for it.
///
/// Resolves to `Ok(false)` only when the target is missing or not actionable and
/// no input was sent to it. Preparation may still have changed the page: an
/// off-screen target is scrolled into view before its actionability is checked.
/// `Ok(true)` means the input was dispatched; it proves the page reacted only when
/// combined with [`expecting`](Self::expecting).
#[must_use = "actions do nothing until awaited"]
pub struct TryActionCall(ActionCall);

impl TryActionCall {
    pub(crate) fn new(page: &Page, action: Action) -> Self {
        Self(ActionCall::new(page, action))
    }

    /// Require this condition to become true after dispatch, for this call only.
    /// It must not already hold, so that it proves a new result.
    pub fn expecting(self, expect: Expectation) -> Self {
        Self(self.0.expecting(expect))
    }

    /// Set the total deadline, as for [`ActionCall::timeout`].
    pub fn timeout(self, timeout: Duration) -> Self {
        Self(self.0.timeout(timeout))
    }

    /// Set where the expectation may be observed. Does not change the input target.
    pub fn destination(self, destination: Destination) -> Self {
        Self(self.0.destination(destination))
    }
}

impl IntoFuture for TryActionCall {
    type Output = Result<bool>;
    type IntoFuture = ActionFuture<bool>;

    fn into_future(self) -> Self::IntoFuture {
        let ActionCall {
            page,
            action,
            mut options,
        } = self.0;
        options.wait_for_target = false;
        Box::pin(async move {
            match page.explore(action, options).await {
                Ok(_) => Ok(true),
                Err(Error::Action {
                    receipt, source, ..
                }) if receipt.dispatch == DispatchState::NotSent
                    && matches!(
                        *source,
                        Error::ElementNotFound(_)
                            | Error::ElementNotVisible { .. }
                            | Error::Coded {
                                code: ErrorCode::ActionabilityChanged | ErrorCode::TargetStale,
                                ..
                            }
                    ) =>
                {
                    Ok(false)
                }
                Err(error) => Err(error),
            }
        })
    }
}