Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(clock): keep paused state when the clock log is replayed in parts (#43224)

Pavel Feldman297470a0ddee
diff --git a/packages/injected/src/clock.ts b/packages/injected/src/clock.ts
index b4cda8de57..a322e34778 100644
--- a/packages/injected/src/clock.ts
+++ b/packages/injected/src/clock.ts
@@ -242,6 +242,10 @@ export class ClockController {
callAt,
promise: undefined,
cancel: this._embedder.setTimeout(() => {
+ // Apply the log before the real time moves the clock.
+ this._replayLogOnce();
+ if (this._currentRealTimeTimer !== realTimeTimer)
+ return;
this._syncRealTime();
// eslint-disable-next-line no-console
realTimeTimer.promise = this._runTo(this._now.ticks).catch(e => console.error(e));
@@ -441,7 +445,9 @@ export class ClockController {
return;
let lastLogTime = -1;
- let isPaused = false;
+ // The log can be replayed in parts, when an init script reads the time
+ // in between the clock's init scripts. Continue from the current state.
+ let isPaused = !this._realTime;
for (const { type, time, param } of this._log) {
if (!isPaused && lastLogTime !== -1)
diff --git a/tests/library/page-clock.spec.ts b/tests/library/page-clock.spec.ts
index 9550de2568..02d3444fe6 100644
--- a/tests/library/page-clock.spec.ts
+++ b/tests/library/page-clock.spec.ts
@@ -518,6 +518,29 @@ it.describe('while on pause', () => {
await page.clock.runFor(1);
expect(calls).toEqual([{ params: ['outer'] }, { params: ['inner'] }]);
});
+
+ it('should stay paused on the next page when an init script reads the time', {
+ annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/43214' },
+ }, async ({ page, server }) => {
+ await page.clock.install({ time: 0 });
+ await page.clock.pauseAt(1000);
+ await page.addInitScript(() => Date.now());
+ await page.goto(server.EMPTY_PAGE);
+ await page.clock.runFor(1000);
+ await page.goto(server.PREFIX + '/title.html');
+ expect(await page.evaluate(() => Date.now())).toBe(2000);
+ await page.waitForTimeout(500);
+ expect(await page.evaluate(() => Date.now())).toBe(2000);
+ });
+
+ it('should not advance performance.now() before the first clock call on a new page', {
+ annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/43214' },
+ }, async ({ page, server }) => {
+ await page.clock.pauseAt(1000);
+ await page.goto(server.EMPTY_PAGE);
+ await page.waitForTimeout(500);
+ expect(await page.evaluate(() => [performance.now(), Date.now()])).toEqual([0, 1000]);
+ });
});
it.describe('Date.now', () => {