Skip to content

Commit

Permalink
fix(reuse): clear storage after stopping all scripts (#16275)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman committed Aug 4, 2022
1 parent 4b53be4 commit 97fa251
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 31 deletions.
22 changes: 11 additions & 11 deletions examples/todomvc/tests/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,20 +383,20 @@ async function createDefaultTodos(page: Page) {
}
}

async function checkNumberOfTodosInLocalStorage(page: Page, expected: number) {
return await page.waitForFunction(e => {
return JSON.parse(localStorage['react-todos']).length === e;
}, expected);
async function checkNumberOfCompletedTodosInLocalStorage(page: Page, expected: number) {
await expect.poll(() => {
return page.evaluate(() => JSON.parse(localStorage['react-todos']).filter(i => i.completed).length);
}).toBe(expected);
}

async function checkNumberOfCompletedTodosInLocalStorage(page: Page, expected: number) {
return await page.waitForFunction(e => {
return JSON.parse(localStorage['react-todos']).filter(i => i.completed).length === e;
}, expected);
async function checkNumberOfTodosInLocalStorage(page: Page, expected: number) {
await expect.poll(() => {
return page.evaluate(() => JSON.parse(localStorage['react-todos']).length);
}).toBe(expected);
}

async function checkTodosInLocalStorage(page: Page, title: string) {
return await page.waitForFunction(t => {
return JSON.parse(localStorage['react-todos']).map(i => i.title).includes(t);
}, title);
await expect.poll(() => {
return page.evaluate(() => JSON.parse(localStorage['react-todos']).map(i => i.title));
}).toContain(title);
}
14 changes: 3 additions & 11 deletions packages/playwright-core/src/server/browserContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,9 @@ export abstract class BrowserContext extends SdkObject {

// Unless I do this early, setting extra http headers below does not respond.
await page?._frameManager.closeOpenDialogs();
// This should be before the navigation to about:blank so that we could save on
// a navigation as we clear local storage.
await this._clearStorage();
// Navigate to about:blank first to ensure no page scripts are running after this point.
await page?.mainFrame().goto(metadata, 'about:blank', { timeout: 0 });
await this._clearStorage();
await this._removeExposedBindings();
await this._removeInitScripts();
// TODO: following can be optimized to not perform noops.
Expand Down Expand Up @@ -475,15 +474,7 @@ export abstract class BrowserContext extends SdkObject {
if (!this._origins.size)
return;
let page = this.pages()[0];
const originArray = [...this._origins];

// Fast path.
if (page && originArray.length === 1 && page.mainFrame().url().startsWith(originArray[0])) {
await page.mainFrame().clearStorageForCurrentOriginBestEffort();
return;
}

// Slow path.
const internalMetadata = serverSideCallMetadata();
page = page || await this.newPage(internalMetadata);
await page._setServerRequestInterceptor(handler => {
Expand All @@ -495,6 +486,7 @@ export abstract class BrowserContext extends SdkObject {
await frame.clearStorageForCurrentOriginBestEffort();
}
await page._setServerRequestInterceptor(undefined);
// It is safe to not restore the URL to about:blank since we are doing it in Page::resetForReuse.
}

isSettingStorageState(): boolean {
Expand Down
6 changes: 1 addition & 5 deletions packages/playwright-core/src/server/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,16 +231,12 @@ export class Page extends SdkObject {
this.setDefaultNavigationTimeout(undefined);
this.setDefaultTimeout(undefined);

// Do this first in order to unfreeze evaluates.
await this._frameManager.closeOpenDialogs();

await this._removeExposedBindings();
await this._removeInitScripts();

// TODO: handle pending routes.
await this.setClientRequestInterceptor(undefined);
await this._setServerRequestInterceptor(undefined);
await this.setFileChooserIntercepted(false);
// Re-navigate once init scripts are gone.
await this.mainFrame().goto(metadata, 'about:blank');
this._emulatedSize = undefined;
this._emulatedMedia = {};
Expand Down
12 changes: 8 additions & 4 deletions tests/playwright-test/playwright.ct-reuse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,14 @@ test('should clean storage', async ({ runInlineTest }) => {
test('one', async ({ context, page }) => {
lastContextGuid = context._guid;
await page.evaluate(async () => {
localStorage.foo = 'bar';
sessionStorage.foo = 'bar';
});
// Spam local storage.
page.evaluate(async () => {
while (true) {
localStorage.foo = 'bar';
sessionStorage.foo = 'bar';
await new Promise(f => setTimeout(f, 0));
}
}).catch(() => {});
const local = await page.evaluate('localStorage.foo');
const session = await page.evaluate('sessionStorage.foo');
Expand Down

0 comments on commit 97fa251

Please sign in to comment.