MouseSimulator

Struct MouseSimulator 

Source
pub struct MouseSimulator { /* private fields */ }
Expand description

Simulates human-like mouse movement via distance-aware Bézier curve trajectories.

Each call to move_to computes a cubic Bézier path between the current cursor position and the target, then replays it as a sequence of Input.dispatchMouseEvent CDP commands with randomised inter-event delays (10–50 ms per segment). Movement speed naturally slows for long distances and accelerates for short ones — matching human motor-control patterns.

§Example

use stygian_browser::behavior::MouseSimulator;

let mut mouse = MouseSimulator::new();
mouse.move_to(page, 640.0, 400.0).await?;
mouse.click(page, 640.0, 400.0).await?;

Implementations§

Source§

impl MouseSimulator

Source

pub fn new() -> Self

Create a simulator seeded from wall-clock time, positioned at (0, 0).

§Example
use stygian_browser::behavior::MouseSimulator;
let mouse = MouseSimulator::new();
assert_eq!(mouse.position(), (0.0, 0.0));
Source

pub const fn with_seed_and_position(seed: u64, x: f64, y: f64) -> Self

Create a simulator with a known initial position and deterministic seed.

Useful for unit-testing path generation without CDP.

§Example
use stygian_browser::behavior::MouseSimulator;
let mouse = MouseSimulator::with_seed_and_position(42, 100.0, 200.0);
assert_eq!(mouse.position(), (100.0, 200.0));
Source

pub const fn position(&self) -> (f64, f64)

Returns the current cursor position as (x, y).

§Example
use stygian_browser::behavior::MouseSimulator;
let mouse = MouseSimulator::new();
let (x, y) = mouse.position();
assert_eq!((x, y), (0.0, 0.0));
Source

pub fn compute_path( &mut self, from_x: f64, from_y: f64, to_x: f64, to_y: f64, ) -> Vec<(f64, f64)>

Compute Bézier waypoints for a move from (from_x, from_y) to (to_x, to_y).

The number of waypoints scales with Euclidean distance — roughly one point every 8 pixels — with a minimum of 12 and maximum of 120 steps. Random perpendicular offsets are applied to the two interior control points to produce natural curved paths. Each waypoint receives sub-pixel jitter (±0.8 px) for micro-tremor realism.

This method is pure (no I/O) and is exposed for testing.

§Example
use stygian_browser::behavior::MouseSimulator;
let mut mouse = MouseSimulator::with_seed_and_position(1, 0.0, 0.0);
let path = mouse.compute_path(0.0, 0.0, 200.0, 0.0);
// always at least 12 steps
assert!(path.len() >= 13);
// starts near origin
assert!((path[0].0).abs() < 5.0);
// ends near target
let last = path[path.len() - 1];
assert!((last.0 - 200.0).abs() < 5.0);
Source

pub async fn move_to(&mut self, page: &Page, to_x: f64, to_y: f64) -> Result<()>

Move the cursor to (to_x, to_y) using a human-like Bézier trajectory.

Dispatches Input.dispatchMouseEvent(mouseMoved) for each waypoint with randomised 10–50 ms delays. Updates position on success.

§Errors

Returns BrowserError::CdpError if any CDP event dispatch fails.

Source

pub async fn click(&mut self, page: &Page, x: f64, y: f64) -> Result<()>

Move to (x, y) then perform a human-like left-click.

After arriving at the target the simulator pauses (20–80 ms), sends mousePressed, holds (50–150 ms), then sends mouseReleased.

§Errors

Returns BrowserError::CdpError if any CDP event dispatch fails.

Trait Implementations§

Source§

impl Default for MouseSimulator

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> PolicyExt for T
where T: ?Sized,

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more