Control time in Playwright with page.clock
Freeze browser time in Playwright, test a five-minute session timeout without waiting, and choose whether interval callbacks should run for each tick.
Your page checks once a minute whether a session has expired. An end-to-end test needs to reach that five-minute deadline without spending five minutes on the runner. Install a clock in the page and move it to the deadline from the test.
Pause before starting the session
Save this as session.spec.ts in a Playwright 1.45 or later project with Chromium installed. Each test sets the same starting time and creates an interval after the clock has paused.
import { expect, test } from "@playwright/test";
for (const method of ["fastForward", "runFor"] as const) {
test(method, async ({ page }) => {
await page.clock.install({ time: new Date("2026-01-10T12:00:00Z") });
await page.clock.pauseAt(new Date("2026-01-10T12:01:00Z"));
await page.setContent('<output id="status">Active (0)</output>');
await page.evaluate(() => {
const status = document.querySelector("#status")!;
const expiresAt = Date.now() + 5 * 60_000;
let checks = 0;
setInterval(() => {
checks++;
const state = Date.now() >= expiresAt ? "Expired" : "Active";
status.textContent = state + " (" + checks + ")";
}, 60_000);
});
await expect(page.locator("#status")).toHaveText("Active (0)");
await page.clock[method]("05:00");
await expect(page.locator("#status")).toHaveText(
method === "fastForward" ? "Expired (1)" : "Expired (5)",
);
});
}Run pnpm exec playwright test session.spec.ts. Both tests reach the same deadline and report different callback counts.
install replaces the page's clock and timer functions. Do it before navigation in a real app so the app registers its timers on that clock. In the example, pauseAt stops the clock before the test starts the interval. The time the runner spends creating the page stays out of the five minutes.
diagram
One clock advance, two callback counts
Advance by the behavior you need
Use fastForward when the app should handle a jump, like a laptop waking after sleep. A due interval runs once at most. In the demo that one callback sees the deadline because it compares against Date.now().
Use runFor when each intermediate callback matters. A countdown that subtracts one minute per callback will behave differently from one that compares a deadline with the current time. Test each callback when you are checking the countdown, then test a jump if the UI also needs to handle sleep.
If your test only checks a date label, setFixedTime freezes what Date reports and leaves timers running. The page clock leaves server time alone. Supply a controlled server response when the test depends on the server’s view of time.