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 Clock<'_>
impl Clock<'_>
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.
§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.
§Errors
Returns an error if advancing time fails.
Sourcepub async fn resume(&self) -> Result<(), PageError>
pub async fn resume(&self) -> Result<(), PageError>
Resume normal time flow.
After calling this, time will advance normally from the current mocked time value.
§Errors
Returns an error if resuming fails.