Clock API: controlling time
page.clock replaces the time sources in the browser, including Date and timers, so minutes of waiting are run through in milliseconds. The clock is installed before goto. fastForward fires overdue timers at most once, while runFor executes every tick, so runFor is used for countdowns and setFixedTime gives a stable date. It covers browser time only: a server-side TTL cannot be checked this way, and times are given with a Z suffix.
Playwright · Emulation
A session ends after 15 minutes. Wait for them in a test or wind the time forward?
An application warns about the session ending at the 14th minute of inactivity and logs the user out at the 15th. There are two bad ways to check this: waitForTimeout(15 * 60_000), after which the test fails on a timeout, or not checking at all. There is a third: replace the clock in the browser and "run" the needed minutes in milliseconds. This is what page.clock provides (since version 1.45). Below: what exactly it replaces, how fastForward differs from runFor, and what it cannot do.
How it works and why. clock.install() replaces the built-in time sources in the page: Date, Temporal.Now, setTimeout, setInterval, requestAnimationFrame, requestIdleCallback (and their clear…/cancel… counterparts) and performance. The application does not know about the replacement: it calls the usual APIs, and they return controlled time. Hence the main condition: install the clock before goto, so the application sets its timers into the replaced scheduler from the very start, not into the real one. There are page.clock and context.clock (for the whole context).
clock.install({ time }): install a clock with an initial time. After that, time flows as usual until it is stopped.clock.pauseAt(time): jump to a moment and stop time; timers do not fire untilrunFor,fastForward, anotherpauseAtorresume.clock.resume(): resume the natural flow.clock.fastForward(ticks): a jump forward. All the timers that should have fired do fire, but each at most once; it is like closing a laptop lid and opening it later.clock.runFor(ticks): run time sequentially, executing each timer at its moments; an interval fires as many times as fit.clock.setFixedTime(time):Date.now()always returns one value, while timers work as usual.clock.setSystemTime(time): change the current time without firing timers.
The ticks parameter is a number of milliseconds or a string: '08' means eight seconds, '01:00' one minute, '02:34:10' two hours, 34 minutes and ten seconds.
An interval every minute: five minutes ahead
Example: waiting 14 minutes, and the test dies at second 30. First an attempt "honestly":
// before: real waiting
test('session expiry warning', async ({ page }) => {
await page.goto('/dashboard');
await page.waitForTimeout(14 * 60_000);
await expect(page.getByRole('dialog')).toBeVisible();
});
Error: Test timeout of 30000ms exceeded.
You could raise the test timeout to 15 minutes, but the run becomes absurdly long, and every such test multiplies CI time. The clock solves it without waiting. Assumption: the application sets timers for 14 and 15 minutes on load.
// after: the clock before goto, time wound forward
test('session expiry warning', async ({ page }) => {
await page.clock.install({ time: new Date('2026-10-08T10:00:00Z') });
await page.goto('/dashboard');
await page.clock.fastForward('14:00');
await expect(page.getByRole('dialog', { name: 'Session is ending' }))
.toBeVisible();
await page.clock.fastForward('01:00');
await expect(page).toHaveURL(/login/);
});
Assertions after the wind-forward stay ordinary web-first ones: they retry until the application updates the DOM. Note the Z suffix in the date: a string without it is interpreted in the time zone of the Node process, so the test gives different results on different machines.
When runFor is needed. The difference shows on counters. An application with a countdown that decrements a number on each tick of an interval will see one tick after fastForward('10:00') (09:59), because the interval fired once. If instead the application recalculates the remainder from Date.now() each time, one tick already shows the right value. It is not always visible in advance, so for countdowns take runFor, which runs all the ticks.
test('the offer banner counts down to zero', async ({ page }) => {
await page.clock.install({ time: new Date('2026-10-08T09:59:59Z') });
await page.clock.pauseAt(new Date('2026-10-08T10:00:00Z'));
await page.goto('/offer');
await expect(page.getByTestId('countdown')).toHaveText('10:00');
await page.clock.runFor('00:05');
await expect(page.getByTestId('countdown')).toHaveText('09:55');
await page.clock.runFor('09:55');
await expect(page.getByText('The offer has ended')).toBeVisible();
});
The assumption here: the banner counts 10 minutes from load. pauseAt before goto stops time before the app starts, and then it moves only through runFor, so the test does not depend on the speed of the machine. For a stable date on a page or in a screenshot, setFixedTime is enough.
What the clock sees and what it does not
Controls
JavaScript in the browser: Date, timers, requestAnimationFrame, performance.now(). A client-side session timer, a countdown, "today" on a page.
Does not control
The server, the database, signed tokens with an exp that the backend checks, and what the browser does outside JavaScript, such as cookie expiry.
Limits and alternatives. If the session is closed by the server, after fastForward('16:00') it still considers the session alive: its clock has not changed. Then you need a test endpoint, a short TTL on the test environment or a separate API test. For code that does not depend on the browser (unit tests) there are their own fake-time tools, for example vi.useFakeTimers() or jest.useFakeTimers(); page.clock is made for the page itself.
Common mistakes.
- Calling
installaftergoto: the application has already read the real time and set real timers. - Using
fastForwardfor a counter that decrements on each tick: one tick runs. - Expecting the timer on the server to speed up as well.
- Leaving
waitForTimeout(60_000)where the time could be run through in milliseconds. - Setting the time without
Zor an offset: the result depends on the machine's zone.
Check yourself
A countdown decrements the number on each one-second setInterval tick. A test runs fastForward('10:00') and expects "00:00" but sees "09:59". Why?
Answer
fastForward fires overdue timers at most once, so the interval ran one tick. You need runFor('10:00'), which runs every tick. If the application calculated the remainder from Date.now(), the jump would be enough.
After fastForward('16:00') the client popup "Session ended" appeared, but an API request still passes with the same token. Is that a bug in the application?
Answer
No, it is a limit of the tool. The clock replaces time only in the browser, while the server checks the token against its own real time. The server part is checked separately: with a short TTL on the test environment or an API test.
page.clock: I set the time before opening the page and wind it forward instead of real pauses. Middle/Senior: I install the clock before goto and choose the method by behaviour: fastForward when only the result matters, runFor when the application counts ticks, setFixedTime for a stable date. I remember it is the browser only, so I check the server TTL and token expiry separately, and I set the time with Z so the run does not depend on the zone.