Skip to main content

时钟

准确地模拟随时间变化的行为对于验证应用的正确性至关重要。了解更多关于时钟模拟的信息。

🌐 Accurately simulating time-dependent behavior is essential for verifying the correctness of applications. Learn more about clock emulation.

请注意,时钟是为整个 BrowserContext 安装的,因此所有页面和 iframe 的时间都由同一个时钟控制。

🌐 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.45 clock.FastForwardAsync

通过向前跳跃时间来调整时钟。最多只触发一次到期的计时器。这相当于用户关闭注意本电脑一段时间后再重新打开它。

🌐 Advance 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");

参数

  • ticks long | string#

    时间可以是将时钟快进的毫秒数,也可以是人类可读的字符串。有效的字符串格式为“08”代表八秒,“01:00”代表一分钟,“02:34:10”代表两小时三十四分十秒。

返回


InstallAsync

Added in: v1.45 clock.InstallAsync

为以下与时间相关的函数安装虚假实现:

🌐 Install fake implementations for the following time-related functions:

  • Date
  • setTimeout
  • clearTimeout
  • setInterval
  • clearInterval
  • requestAnimationFrame
  • cancelAnimationFrame
  • requestIdleCallback
  • cancelIdleCallback
  • performance

假定时器用于在测试中手动控制时间的流逝。它们允许你推进时间、触发定时器,并控制依赖时间的函数的行为。更多信息请参见 Clock.RunForAsync()Clock.FastForwardAsync()

🌐 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)
    • Time|TimeDate string? | Date? (optional)#

      初始化时间,默认为当前系统时间。

返回


PauseAtAsync

Added in: v1.45 clock.PauseAtAsync

通过跳跃时间来提前时钟并暂停时间。一旦调用此方法,除非调用 Clock.RunForAsync()Clock.FastForwardAsync()Clock.PauseAtAsync()Clock.ResumeAsync(),否则不会触发任何定时器。

🌐 Advance 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");

为了获得最佳效果,请在浏览页面之前安装时钟,并将其设置为略早于预定测试时间的时间。这可以确保在页面加载期间所有计时器正常运行,防止页面卡住。页面完全加载后,你可以安全地使用 Clock.PauseAtAsync() 暂停时钟。

🌐 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.45 clock.ResumeAsync

恢复计时器。一旦调用此方法,时间将继续流动,计时器会照常触发。

🌐 Resumes timers. Once this method is called, time resumes flowing, timers are fired as usual.

用法

await Clock.ResumeAsync();

返回


RunForAsync

Added in: v1.45 clock.RunForAsync

推进时钟,触发所有与时间相关的回调。

🌐 Advance the clock, firing all the time-related callbacks.

用法

await page.Clock.RunForAsync(1000);
await page.Clock.RunForAsync("30:00");

参数

  • ticks long | string#

    时间可以是将时钟快进的毫秒数,也可以是人类可读的字符串。有效的字符串格式为“08”代表八秒,“01:00”代表一分钟,“02:34:10”代表两小时三十四分十秒。

返回


SetFixedTimeAsync

Added in: v1.45 clock.SetFixedTimeAsync

使 Date.nownew Date() 始终返回固定的虚拟时间,同时保持所有计时器运行。

🌐 Makes Date.now and new Date() return fixed fake time at all times, keeps all the timers running.

这种方法适用于只需用预设时间测试的简单场景。对于更高级的场景,可以使用 [Clock.InstallAsync()](/api/class-clock.mdx#clock-install)。阅读关于[时钟模拟](../clock.mdx)的文档以了解更多信息。

🌐 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.45 clock.SetSystemTimeAsync

设置系统时间,但不会触发任何计时器。可以使用此功能测试网页对时间变化的反应,例如从夏令时切换到冬令时,或更改时区。

🌐 Sets 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");

参数

返回