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.

fast_forward

def fast_forward(ticks)

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")

install

def install(time: nil)

Install 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#run_for and Clock#fast_forward for more information.

run_for

def run_for(ticks)

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

Usage

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

pause_at

def pause_at(time)

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

page.clock.pause_at(Time.parse("2020-02-02"))
page.clock.pause_at("2020-02-02")

resume

def resume

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

set_fixed_time

def set_fixed_time(time)

alias: fixed_time=

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

Usage

page.clock.set_fixed_time(Time.now)
page.clock.set_fixed_time(Time.parse("2020-02-02"))
page.clock.set_fixed_time("2020-02-02")

# or we can use the alias
page.clock.fixed_time = Time.now
page.clock.fixed_time = Time.parse("2020-02-02")
page.clock.fixed_time = "2020-02-02"

set_system_time

def set_system_time(time)

alias: system_time=

Sets current system time but does not trigger any timers.

Usage

page.clock.set_system_time(Time.now)
page.clock.set_system_time(Time.parse("2020-02-02"))
page.clock.set_system_time("2020-02-02")

# or we can use the alias
page.clock.system_time = Time.now
page.clock.system_time = Time.parse("2020-02-02")
page.clock.system_time = "2020-02-02"