Skip to main content

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​

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.

Usage

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

Arguments

  • ticks int | str#

    Time may be a finite 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
  • Temporal.Now
  • 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.run_for() and clock.fast_forward() for more information.

Usage

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

Arguments

  • time float | str | datetime (optional)#

    Time to initialize with, current system time by default. Numeric values are Unix time in seconds.

Returns


pause_at​

Added in: v1.45 clock.pause_at

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.

Usage