Repository navigation
Description
Version
1.64.0 (same with 1.63.0)
Steps to reproduce
Single script, no project needed:
const { chromium } = require('playwright');
(async () => {
const browser = await chromium.launch();
const page = await browser.newPage();
await page.route('https://example.test/**', route => route.fulfill({ contentType: 'text/html', body: '<p>page</p>' }));
await page.clock.install({ time: new Date('2024-02-02T08:00:00Z') });
await page.clock.pauseAt(new Date('2024-02-02T10:00:00Z'));
await page.addInitScript(() => { Date.now(); }); // any clock read at document start
await page.goto('https://example.test/a');
await page.clock.runFor(1000); // any clock call made after the init script was added
await page.goto('https://example.test/b');
const before = await page.evaluate(() => Date.now());
await page.waitForTimeout(1000);
const after = await page.evaluate(() => Date.now());
console.log('Date.now() moved by', after - before, 'ms during one real second');
await browser.close();
})();- Save it as
repro.js. npm i [email protected] && npx playwright install chromiumnode repro.js
Expected behavior
Date.now() moved by 0 ms during one real second: the clock is paused, only runFor(), fastForward(), pauseAt() or resume() should move it.
Actual behavior
Date.now() moved by 1003 ms during one real second: on the second page the clock runs in real time.
Without the init script, or without the runFor() between the two navigations, it stays paused (0 ms).
Additional context
What seems to happen, reading packages/injected/src/clock.ts:
- each clock call (
install,pauseAt,runFor…) is recorded as a context init script that pushes an entry to the controller's log, and a new document replays that log lazily, at its first clock call (_replayLogOnce); - on an existing page, init scripts run in the order they were added, so the second page runs the clock source, the
installandpauseAtentries, the user init script, and only then therunForentry; - the user script's
Date.now()replays the partial log[install, pauseAt]: paused. TherunForentry is pushed afterwards; at the next clock call it is replayed on its own, with no pause entry before it, so_replayLogOnceends withisPaused === falseand calls_innerResume(): real time.
A related symptom, same mechanism: inject() calls controller.resume() before the log entries are pushed, so until a document's first clock call its clock is in real-time mode. With install() the replay resets ticks to 0 and nothing shows. With pauseAt() called without install() (it installs the clock itself), it does: a page whose first script arrives 1.5 s after the document is created reads performance.now() ≈ 1416 at its first call, and requestAnimationFrame timestamps follow, while Date.now() is the paused time (1.63 and 1.64).
Environment
- OS: macOS 27.0 (arm64)
- Node.js: 22.17.0
- Playwright: 1.64.0 (also 1.63.0)
- Browser: Chromium headless shell
Activity