pub struct Clock<'a> { /* private fields */ }Expand description
Clock controller for mocking time in a browser page.
The Clock API allows you to control JavaScript’s time-related functions
including Date, setTimeout, setInterval, requestAnimationFrame, and
performance.now().
§Example
use std::time::Duration;
// Install clock mocking
page.clock().install().await.unwrap();
// Freeze time at a specific moment
page.clock().set_fixed_time("2024-01-01T00:00:00Z").await.unwrap();
// Advance time and fire scheduled timers
page.clock().run_for(Duration::from_secs(5)).await.unwrap();
// Uninstall when done
page.clock().uninstall().await.unwrap();Implementations§
Source§impl<'a> Clock<'a>
impl<'a> Clock<'a>
Sourcepub async fn set_fixed_time(
&self,
time: impl Into<TimeValue>,
) -> Result<(), PageError>
pub async fn set_fixed_time( &self, time: impl Into<TimeValue>, ) -> Result<(), PageError>
Set a fixed time that doesn’t advance.
All calls to Date.now() and new Date() will return this time.
Time remains frozen until you call run_for, fast_forward,
set_system_time, or resume.
§Arguments
time- The time to set, either as an ISO 8601 string (e.g., “2024-01-01T00:00:00Z”) or a Unix timestamp in milliseconds.
§Example
// Using ISO string
page.clock().set_fixed_time("2024-01-01T00:00:00Z").await?;
// Using timestamp
page.clock().set_fixed_time(1704067200000i64).await?;§Errors
Returns an error if setting the time fails.
Sourcepub async fn set_system_time(
&self,
time: impl Into<TimeValue>,
) -> Result<(), PageError>
pub async fn set_system_time( &self, time: impl Into<TimeValue>, ) -> Result<(), PageError>
Set the system time that flows normally.
Time starts from the specified value and advances in real time.
§Arguments
time- The starting time, either as an ISO 8601 string or Unix timestamp.
§Example
page.clock().set_system_time("2024-01-01T00:00:00Z").await?;
// Time will now flow from 2024-01-01§Errors
Returns an error if setting the time fails.
Sourcepub async fn run_for(&self, duration: Duration) -> Result<u32, PageError>
pub async fn run_for(&self, duration: Duration) -> Result<u32, PageError>
Advance time by a duration, firing any scheduled timers.
This advances the clock and executes any setTimeout/setInterval callbacks that were scheduled to fire during that period.
§Arguments
duration- The amount of time to advance.
§Returns
The number of timers that were fired.
§Example
// Advance 5 seconds, firing any timers scheduled in that period
let fired = page.clock().run_for(Duration::from_secs(5)).await?;
println!("Fired {} timers", fired);§Errors
Returns an error if advancing time fails.
Sourcepub async fn fast_forward(&self, duration: Duration) -> Result<(), PageError>
pub async fn fast_forward(&self, duration: Duration) -> Result<(), PageError>
Fast-forward time without firing timers.
This advances the clock but does NOT execute scheduled timers. Use this when you want to jump ahead in time quickly.
§Arguments
duration- The amount of time to skip.
§Example
// Skip ahead 1 hour without firing any timers
page.clock().fast_forward(Duration::from_secs(3600)).await?;§Errors
Returns an error if fast-forwarding fails.
Sourcepub async fn run_all_timers(&self) -> Result<u32, PageError>
pub async fn run_all_timers(&self) -> Result<u32, PageError>
Run all pending timers.
This advances time to execute all scheduled setTimeout and setInterval callbacks, as well as requestAnimationFrame callbacks.
§Returns
The number of timers that were fired.
§Example
let fired = page.clock().run_all_timers().await?;§Errors
Returns an error if running timers fails.