Skip to content

[Bug]: after clock.install() + pauseAt(), an init script that reads the time makes the next page run on real time #43214

Closed

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();
})();
  1. Save it as repro.js.
  2. npm i [email protected] && npx playwright install chromium
  3. node 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 install and pauseAt entries, the user init script, and only then the runFor entry;
  • the user script's Date.now() replays the partial log [install, pauseAt]: paused. The runFor entry is pushed afterwards; at the next clock call it is replayed on its own, with no pause entry before it, so _replayLogOnce ends with isPaused === false and 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions