Table of Contents

Interface IClock

Namespace
Microsoft.Playwright
Assembly
Microsoft.Playwright.dll

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 IBrowserContext, so the time in all the pages and iframes is controlled by the same clock.

public interface IClock

Methods

FastForwardAsync(long)

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.FastForwardAsync(1000);
await page.Clock.FastForwardAsync("30:00");
Task FastForwardAsync(long ticks)

Parameters

ticks long

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

Task

FastForwardAsync(string)

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.FastForwardAsync(1000);
await page.Clock.FastForwardAsync("30:00");
Task FastForwardAsync(string ticks)

Parameters

ticks string

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

Task

InstallAsync(ClockInstallOptions?)

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 RunForAsync(long) and FastForwardAsync(long) for more information.

Task InstallAsync(ClockInstallOptions? options = null)

Parameters

options ClockInstallOptions

Call options

Returns

Task

PauseAtAsync(DateTime)

Advance the clock by jumping forward in time and pause the time. Once this method is called, no timers are fired unless RunForAsync(long), FastForwardAsync(long), PauseAtAsync(DateTime) or 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.

**Usage**

await page.Clock.PauseAtAsync(DateTime.Parse("2020-02-02"));
await page.Clock.PauseAtAsync("2020-02-02");
Task PauseAtAsync(DateTime time)

Parameters

time DateTime

Time to pause at.

Returns

Task

PauseAtAsync(string)

Advance the clock by jumping forward in time and pause the time. Once this method is called, no timers are fired unless RunForAsync(long), FastForwardAsync(long), PauseAtAsync(DateTime) or 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.

**Usage**

await page.Clock.PauseAtAsync(DateTime.Parse("2020-02-02"));
await page.Clock.PauseAtAsync("2020-02-02");
Task PauseAtAsync(string time)

Parameters

time string

Time to pause at.

Returns

Task

ResumeAsync()

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

Task ResumeAsync()

Returns

Task

RunForAsync(long)

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

**Usage**

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

Parameters

ticks long

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

Task

RunForAsync(string)

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

**Usage**

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

Parameters

ticks string

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

Task

SetFixedTimeAsync(DateTime)

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

**Usage**

await page.Clock.SetFixedTimeAsync(DateTime.Now);
await page.Clock.SetFixedTimeAsync(new DateTime(2020, 2, 2));
await page.Clock.SetFixedTimeAsync("2020-02-02");
Task SetFixedTimeAsync(DateTime time)

Parameters

time DateTime

Time to be set.

Returns

Task

SetFixedTimeAsync(string)

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

**Usage**

await page.Clock.SetFixedTimeAsync(DateTime.Now);
await page.Clock.SetFixedTimeAsync(new DateTime(2020, 2, 2));
await page.Clock.SetFixedTimeAsync("2020-02-02");
Task SetFixedTimeAsync(string time)

Parameters

time string

Time to be set.

Returns

Task

SetSystemTimeAsync(DateTime)

Sets current system time but does not trigger any timers.

**Usage**

await page.Clock.SetSystemTimeAsync(DateTime.Now);
await page.Clock.SetSystemTimeAsync(new DateTime(2020, 2, 2));
await page.Clock.SetSystemTimeAsync("2020-02-02");
Task SetSystemTimeAsync(DateTime time)

Parameters

time DateTime

Time to be set.

Returns

Task

SetSystemTimeAsync(string)

Sets current system time but does not trigger any timers.

**Usage**

await page.Clock.SetSystemTimeAsync(DateTime.Now);
await page.Clock.SetSystemTimeAsync(new DateTime(2020, 2, 2));
await page.Clock.SetSystemTimeAsync("2020-02-02");
Task SetSystemTimeAsync(string time)

Parameters

time string

Time to be set.

Returns

Task