Skip to content

Commit

Permalink
cherry-pick(#31357): fix(clock): under reused context
Browse files Browse the repository at this point in the history
We uninstall all the setInitScript but forgot to mark `installed` as
`false`.

Fixes #31353
  • Loading branch information
mxschmitt committed Jun 19, 2024
1 parent 56ca1e1 commit 2451791
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/playwright-core/src/server/browserContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ export abstract class BrowserContext extends SdkObject {
await this._resetStorage();
await this._removeExposedBindings();
await this._removeInitScripts();
this.clock.markAsUninstalled();
// TODO: following can be optimized to not perform noops.
if (this._options.permissions)
await this.grantPermissions(this._options.permissions);
Expand Down
4 changes: 4 additions & 0 deletions packages/playwright-core/src/server/clock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export class Clock {
this._browserContext = browserContext;
}

markAsUninstalled() {
this._scriptInstalled = false;
}

async fastForward(ticks: number | string) {
await this._installIfNeeded();
const ticksMillis = parseTicks(ticks);
Expand Down
13 changes: 13 additions & 0 deletions tests/library/browsercontext-reuse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,19 @@ test('should reset tracing', async ({ reusedContext, trace }, testInfo) => {
expect(error.message).toContain('Must start tracing before stopping');
});

test('should work with clock emulation', async ({ reusedContext, trace }, testInfo) => {
let context = await reusedContext();

let page = await context.newPage();
await page.clock.setFixedTime(new Date('2020-01-01T00:00:00.000Z'));
expect(await page.evaluate('new Date().toISOString()')).toBe('2020-01-01T00:00:00.000Z');

context = await reusedContext();
page = context.pages()[0];
await page.clock.setFixedTime(new Date('2020-01-01T00:00:00Z'));
expect(await page.evaluate('new Date().toISOString()')).toBe('2020-01-01T00:00:00.000Z');
});

test('should continue issuing events after closing the reused page', async ({ reusedContext, server }) => {
test.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/24574' });

Expand Down

0 comments on commit 2451791

Please sign in to comment.