Skip to main content

Module clock

Module clock 

Source
Expand description

Clock — manipulate fake timers for deterministic time-dependent tests

The Clock object is accessible via crate::protocol::BrowserContext::clock or crate::protocol::Page::clock. All RPC calls are sent on the BrowserContext channel.

§Example

use playwright_rs::protocol::{Playwright, ClockInstallOptions};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let playwright = Playwright::launch().await?;
    let browser = playwright.chromium().launch().await?;
    let context = browser.new_context().await?;
    let page = context.new_page().await?;

    let clock = context.clock();

    // Install fake timers, optionally setting an initial time (ms since epoch)
    clock.install(Some(ClockInstallOptions { time: Some(0) })).await?;

    // Freeze time at a fixed point
    clock.set_fixed_time(1_000_000).await?;

    // Verify via evaluate
    let now: f64 = page.evaluate_value("Date.now()").await?.parse()?;
    assert_eq!(now as u64, 1_000_000);

    // Advance time by 5 seconds
    clock.fast_forward(5_000).await?;

    // Pause at a specific instant
    clock.pause_at(2_000_000).await?;

    // Resume normal flow
    clock.resume().await?;

    context.close().await?;
    browser.close().await?;
    Ok(())
}

See: https://playwright.dev/docs/api/class-clock

Structs§

Clock
Playwright Clock — provides fake timer control for deterministic tests.
ClockInstallOptions
Options for Clock::install.