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

fastForward

Added in: v1.45 clock.fastForward

通过向前跳跃来推进时钟。最多只触发一次到期计时器。这相当于用户关闭注意本电脑盖一段时间,然后在给定时间后重新打开。

¥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.

用法

¥Usage

await page.clock.fastForward(1000);
await page.clock.fastForward('30:00');

参数

¥Arguments

时间可能是时钟前进的毫秒数或人类可读的字符串。有效的字符串格式为 "08" 表示八秒、"01:00" 表示一分钟和 "02:34:10" 表示两小时 34 分十秒。

¥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.

返回

¥Returns


install

Added in: v1.45 clock.install

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

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

  • Date

  • setTimeout

  • clearTimeout

  • setInterval

  • clearInterval

  • requestAnimationFrame

  • cancelAnimationFrame

  • requestIdleCallback

  • cancelIdleCallback

  • performance

使用假计时器手动控制测试中的时间流。它们允许你推进时间、触发计时器并控制时间相关函数的行为。有关更多信息,请参阅 clock.runFor()clock.fastForward()

¥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.runFor() and clock.fastForward() for more information.

用法

¥Usage

await clock.install();
await clock.install(options);

参数

¥Arguments

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

¥Time to initialize with, current system time by default.

返回

¥Returns


pauseAt

Added in: v1.45 clock.pauseAt

通过向前跳跃来推进时钟并暂停时间。一旦调用此方法,除非调用 clock.runFor()clock.fastForward()clock.pauseAt()clock.resume(),否则不会触发任何计时器。

¥Advance the clock by jumping forward in time and pause the time. Once this method is called, no timers are fired unless clock.runFor(), clock.fastForward(), clock.pauseAt() or clock.resume() 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.

用法

¥Usage

await page.clock.pauseAt(new Date('2020-02-02'));
await page.clock.pauseAt('2020-02-02');

参数

¥Arguments

暂停时间。

¥Time to pause at.

返回

¥Returns


resume

Added in: v1.45 clock.resume

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

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

用法

¥Usage

await clock.resume();

返回

¥Returns


runFor

Added in: v1.45 clock.runFor

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

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

用法

¥Usage

await page.clock.runFor(1000);
await page.clock.runFor('30:00');

参数

¥Arguments

时间可能是时钟前进的毫秒数或人类可读的字符串。有效的字符串格式为 "08" 表示八秒、"01:00" 表示一分钟和 "02:34:10" 表示两小时 34 分十秒。

¥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.

返回

¥Returns


setFixedTime

Added in: v1.45 clock.setFixedTime

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

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

对于只需要使用预定义时间进行测试的简单场景,请使用此方法。对于更高级的场景,请改用 clock.install()。阅读有关 时钟模拟 的文档以了解更多信息。

¥Use this method for simple scenarios where you only need to test with a predefined time. For more advanced scenarios, use clock.install() instead. Read docs on clock emulation to learn more.

用法

¥Usage

await page.clock.setFixedTime(Date.now());
await page.clock.setFixedTime(new Date('2020-02-02'));
await page.clock.setFixedTime('2020-02-02');

参数

¥Arguments

时间以毫秒为单位设置。

¥Time to be set in milliseconds.

返回

¥Returns


setSystemTime

Added in: v1.45 clock.setSystemTime

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

¥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.

用法

¥Usage

await page.clock.setSystemTime(Date.now());
await page.clock.setSystemTime(new Date('2020-02-02'));
await page.clock.setSystemTime('2020-02-02');

参数

¥Arguments

时间以毫秒为单位设置。

¥Time to be set in milliseconds.

返回

¥Returns