Timers & Clock
Countdowns and date-dependent content: test minutes of behaviour in milliseconds, on any day you choose.
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 fromDate.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
Page opened on
Next section · FormsLogin Form →Cover every path of a login form: required fields, wrong and right passwords, the password toggle and the lockout. Sessions are covered separately under Authentication.
Related exercises
- FormsBeginnerLogin FormCover every path of a login form: required fields, wrong and right passwords, the password toggle and the lockout. Sessions are covered separately under Authentication.
- Dynamic ElementsIntermediateProgress BarWait for a value to reach a threshold, then act at the right moment.
- Dialogs & WindowsBeginnerJavaScript DialogsHandle native alert, confirm and prompt dialogs, including the ones that arrive in pairs, late, or when you leave.
Read the guide: Playwright auto-waiting: how to fix flaky tests