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

fast_forward

Added in: v1.45 clock.fast_forward

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

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

用法

page.clock.fast_forward(1000)
page.clock.fast_forward("30:00")

参数

  • ticks int | str#

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

返回


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.run_for()clock.fast_forward()

🌐 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.run_for() and clock.fast_forward() for more information.

用法

clock.install()
clock.install(**kwargs)

参数

  • time float | str | datetime (optional)#

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

返回


pause_at

Added in: v1.45 clock.pause_at

通过向前跳跃时间来推进时钟并暂停时间。一旦调用此方法,除非调用 clock.run_for()clock.fast_forward()clock.pause_at()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.run_for(), clock.fast_forward(), clock.pause_at() 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.

用法

page.clock.pause_at(datetime.datetime(2020, 2, 2))
page.clock.pause_at("2020-02-02")

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

🌐 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.pause_at() to pause the clock.

# Initialize clock with some time before the test time and let the page load
# naturally. `Date.now` will progress as the timers fire.
page.clock.install(time=datetime.datetime(2024, 12, 10, 8, 0, 0))
page.goto("http://localhost:3333")
page.clock.pause_at(datetime.datetime(2024, 12, 10, 10, 0, 0))

参数

返回


resume

Added in: v1.45 clock.resume

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

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

用法

clock.resume()

返回


run_for

Added in: v1.45 clock.run_for

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

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

用法

page.clock.run_for(1000);
page.clock.run_for("30:00")

参数

  • ticks int | str#

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

返回


set_fixed_time

Added in: v1.45 clock.set_fixed_time

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

用法

page.clock.set_fixed_time(datetime.datetime.now())
page.clock.set_fixed_time(datetime.datetime(2020, 2, 2))
page.clock.set_fixed_time("2020-02-02")

参数

返回


set_system_time

Added in: v1.45 clock.set_system_time

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

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

用法

page.clock.set_system_time(datetime.datetime.now())
page.clock.set_system_time(datetime.datetime(2020, 2, 2))
page.clock.set_system_time("2020-02-02")

参数

返回