Clock
Accurately simulating time-dependent behavior is essential for verifying the correctness of applications. Learn more about clock emulation.
Note that clock is installed for the entire BrowserContext, so the time in all the pages and iframes is controlled by the same clock.
方法 (Methods)
FastForwardAsync
Added in: v1.45Advance the clock by jumping forward in time. Only fires due timers at most once. This is equivalent to user closing the laptop lid for a while and reopening it later, after given time.
使用方式
await page.Clock.FastForwardAsync(1000);
await page.Clock.FastForwardAsync("30:00");
參數
-
Time may be the number of milliseconds to advance the clock by or a human-readable string. Valid string formats are "08" for eight seconds, "01:00" for one minute and "02:34:10" for two hours, 34 minutes and ten seconds.
傳回值
InstallAsync
Added in: v1.45Install fake implementations for the following time-related functions:
Date
setTimeout
clearTimeout
setInterval
clearInterval
requestAnimationFrame
cancelAnimationFrame
requestIdleCallback
cancelIdleCallback
performance
Fake timers are used to manually control the flow of time in tests. They allow you to advance time, fire timers, and control the behavior of time-dependent functions. See Clock.RunForAsync() and Clock.FastForwardAsync() for more information.
使用方式
await Clock.InstallAsync(options);
參數
options
ClockInstallOptions?
(optional)
傳回值
PauseAtAsync
Added in: v1.45Advance the clock by jumping forward in time and pause the time. Once this method is called, no timers are fired unless Clock.RunForAsync(), Clock.FastForwardAsync(), Clock.PauseAtAsync() or Clock.ResumeAsync() is called.
Only fires due timers at most once. This is equivalent to user closing the laptop lid for a while and reopening it at the specified time and pausing.
使用方式
await page.Clock.PauseAtAsync(DateTime.Parse("2020-02-02"));
await page.Clock.PauseAtAsync("2020-02-02");
For best results, install the clock before navigating the page and set it to a time slightly before the intended test time. This ensures that all timers run normally during page loading, preventing the page from getting stuck. Once the page has fully loaded, you can safely use Clock.PauseAtAsync() to pause the clock.
參數
傳回值
ResumeAsync
Added in: v1.45Resumes timers. Once this method is called, time resumes flowing, timers are fired as usual.
使用方式
await Clock.ResumeAsync();
傳回值
RunForAsync
Added in: v1.45Advance the clock, firing all the time-related callbacks.
使用方式
await page.Clock.RunForAsync(1000);
await page.Clock.RunForAsync("30:00");
參數
-
Time may be the number of milliseconds to advance the clock by or a human-readable string. Valid string formats are "08" for eight seconds, "01:00" for one minute and "02:34:10" for two hours, 34 minutes and ten seconds.
傳回值
SetFixedTimeAsync
Added in: v1.45Makes Date.now
and new Date()
return fixed fake time at all times, keeps all the timers running.
Use this method for simple scenarios where you only need to test with a predefined time. For more advanced scenarios, use Clock.InstallAsync() instead. Read docs on clock emulation to learn more.
使用方式
await page.Clock.SetFixedTimeAsync(DateTime.Now);
await page.Clock.SetFixedTimeAsync(new DateTime(2020, 2, 2));
await page.Clock.SetFixedTimeAsync("2020-02-02");
參數
傳回值
SetSystemTimeAsync
Added in: v1.45Sets system time, but does not trigger any timers. Use this to test how the web page reacts to a time shift, for example switching from summer to winter time, or changing time zones.
使用方式
await page.Clock.SetSystemTimeAsync(DateTime.Now);
await page.Clock.SetSystemTimeAsync(new DateTime(2020, 2, 2));
await page.Clock.SetSystemTimeAsync("2020-02-02");
參數
傳回值