Implementation:Microsoft Playwright Server Clock
| Knowledge Sources | |
|---|---|
| Domains | Server, Time |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Concrete tool for emulating and controlling time-related APIs (Date, setTimeout, setInterval, etc.) in browser contexts provided by the Playwright library.
Description
The `Clock` class manages fake clock installation and manipulation within a `BrowserContext`. It injects clock source scripts as init scripts that override `Date`, `setTimeout`, `setInterval`, `requestAnimationFrame`, and related timing APIs. The class provides `install` to set up the fake clock at a specific time, `fastForward` to advance time by a specified number of ticks, `pauseAt` to freeze time at a specific point, `resume` to resume real-time progression, and `uninstall` to remove the clock overrides. Each operation adds an init script to ensure new pages in the context inherit the clock state. The `setFixedTime` and `setSystemTime` methods allow updating the clock without triggering timers.
Usage
Use Server Clock when testing time-dependent functionality, such as animations, debouncing, or scheduled tasks, via Playwright's `page.clock` API.
Code Reference
Source Location
- Repository: Microsoft_Playwright
- File: packages/playwright-core/src/server/clock.ts
Signature
export class Clock {
constructor(browserContext: BrowserContext);
async uninstall(progress: Progress): Promise<void>;
async fastForward(progress: Progress, ticks: number | string): Promise<void>;
async install(progress: Progress, time: number | string | undefined): Promise<void>;
async pauseAt(progress: Progress, ticks: number | string): Promise<void>;
resumeNoReply(): void;
async setFixedTime(progress: Progress, time: number | string): Promise<void>;
async setSystemTime(progress: Progress, time: number | string): Promise<void>;
}
Import
import { Clock } from '../server/clock';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| browserContext | BrowserContext | Yes | The browser context in which to install the fake clock |
| progress | Progress | Yes | Progress tracker for cancellation |
| time | number or string | No | Time value (epoch millis or ISO string) for install/pauseAt |
| ticks | number or string | Yes (fastForward) | Number of milliseconds or time string to advance |
Outputs
| Name | Type | Description |
|---|---|---|
| (side effect) | void | Clock state installed/modified in all frames of the browser context |
Usage Examples
import { Clock } from '../server/clock';
const clock = new Clock(browserContext);
await clock.install(progress, '2024-01-01T00:00:00Z');
await clock.fastForward(progress, 60000); // advance 1 minute
await clock.pauseAt(progress, '2024-01-01T01:00:00Z');
await clock.uninstall(progress);