Controlling time with page.clock

Do not wait for real time

Waiting 2 real minutes for a session to expire makes a test slow, and it may still time out. page.clock replaces the page's clock, Date, and its timers with a fake one that your test controls.

Install the clock before the page loads

await page.clock.install();
await page.goto('/dynamic/clock');

await page.getByRole('button', { name: 'Start session' }).click();
await expect(page.getByTestId('session-countdown')).toHaveText('2:00');

await page.clock.fastForward('01:30');
await expect(page.getByTestId('session-countdown')).toHaveText('0:30');

await page.clock.fastForward('00:30');
await expect(page.getByRole('alert')).toHaveText(/Session expired/);

fastForward or runFor?

  • fastForward() jumps ahead and fires each due timer at most once, like a laptop waking from sleep. Pages that compute time from Date.now(), like this one, handle that correctly.
  • runFor() moves time forward step by step, firing every timer as many times as it would really run. Use it when the page counts ticks.

Pretend it is a different day

The promo banner depends on the weekday. Fix the date before loading the page to test both branches on any day:

// 10 October 2026 is a Saturday.
await page.clock.setFixedTime(new Date('2026-10-10T10:00:00'));
await page.goto('/dynamic/clock');
await expect(page.getByTestId('promo-banner')).toHaveText(/Weekend sale/);
setFixedTime() freezes Date but keeps timers running normally. install() fakes both.

Session timeout

A session lasts 2 minutes, then expires.

Weekend promotion

No promotion today. Come back at the weekend.

Page opened on