Skip to content

Commit 2451791

Browse files
committed
cherry-pick(#31357): fix(clock): under reused context
We uninstall all the setInitScript but forgot to mark `installed` as `false`. Fixes #31353
1 parent 56ca1e1 commit 2451791

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

packages/playwright-core/src/server/browserContext.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ export abstract class BrowserContext extends SdkObject {
216216
await this._resetStorage();
217217
await this._removeExposedBindings();
218218
await this._removeInitScripts();
219+
this.clock.markAsUninstalled();
219220
// TODO: following can be optimized to not perform noops.
220221
if (this._options.permissions)
221222
await this.grantPermissions(this._options.permissions);

packages/playwright-core/src/server/clock.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ export class Clock {
2626
this._browserContext = browserContext;
2727
}
2828

29+
markAsUninstalled() {
30+
this._scriptInstalled = false;
31+
}
32+
2933
async fastForward(ticks: number | string) {
3034
await this._installIfNeeded();
3135
const ticksMillis = parseTicks(ticks);

tests/library/browsercontext-reuse.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,19 @@ test('should reset tracing', async ({ reusedContext, trace }, testInfo) => {
252252
expect(error.message).toContain('Must start tracing before stopping');
253253
});
254254

255+
test('should work with clock emulation', async ({ reusedContext, trace }, testInfo) => {
256+
let context = await reusedContext();
257+
258+
let page = await context.newPage();
259+
await page.clock.setFixedTime(new Date('2020-01-01T00:00:00.000Z'));
260+
expect(await page.evaluate('new Date().toISOString()')).toBe('2020-01-01T00:00:00.000Z');
261+
262+
context = await reusedContext();
263+
page = context.pages()[0];
264+
await page.clock.setFixedTime(new Date('2020-01-01T00:00:00Z'));
265+
expect(await page.evaluate('new Date().toISOString()')).toBe('2020-01-01T00:00:00.000Z');
266+
});
267+
255268
test('should continue issuing events after closing the reused page', async ({ reusedContext, server }) => {
256269
test.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/24574' });
257270

0 commit comments

Comments
 (0)