Skip to content

Commit

Permalink
fix(reuse): reset Origin Private File System API
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt committed Mar 15, 2024
1 parent 94348bb commit 05c48d9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
12 changes: 10 additions & 2 deletions packages/playwright-core/src/server/frames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1662,7 +1662,7 @@ export class Frame extends SdkObject {
}

async resetStorageForCurrentOriginBestEffort(newStorage: channels.OriginStorage | undefined) {
const context = await this._utilityContext();
const context = await this._mainContext();
await context.evaluate(async ({ ls }) => {
// Clean DOMStorage.
sessionStorage.clear();
Expand Down Expand Up @@ -1692,7 +1692,15 @@ export class Frame extends SdkObject {
if (db.name)
indexedDB.deleteDatabase(db.name!);
}
}, { ls: newStorage?.localStorage }).catch(() => {});

try {
// Clean StorageManager
const root = await navigator.storage.getDirectory();
// @ts-expect-error
for await (const [name] of await root.entries())
await root.removeEntry(name, { recursive: true });
} catch (e) {}
}, { ls: newStorage?.localStorage })

Check failure on line 1703 in packages/playwright-core/src/server/frames.ts

View workflow job for this annotation

GitHub Actions / docs & lint

Missing semicolon
}

private _asLocator(selector: string) {
Expand Down
39 changes: 35 additions & 4 deletions tests/library/browsercontext-reuse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
*/

import { browserTest, expect } from '../config/browserTest';
import type { BrowserContext } from '@playwright/test';
import type { BrowserContext, BrowserContextOptions } from '@playwright/test';

const test = browserTest.extend<{ reusedContext: () => Promise<BrowserContext> }>({
const test = browserTest.extend<{ reusedContext: (options?: BrowserContextOptions) => Promise<BrowserContext> }>({
reusedContext: async ({ browserType, browser }, use) => {
await use(async () => {
await use(async (options: BrowserContextOptions = {}) => {
const defaultContextOptions = (browserType as any)._defaultContextOptions;
const context = await (browser as any)._newContextForReuse(defaultContextOptions);
const context = await (browser as any)._newContextForReuse({
...defaultContextOptions,
...options,
});
return context;
});
},
Expand Down Expand Up @@ -235,6 +238,34 @@ test('should reset mouse position', async ({ reusedContext, browserName, platfor
await expect(page.locator('#two')).toHaveCSS('background-color', 'rgb(0, 0, 255)');
});

test('should reset Origin Private File System', async ({ reusedContext, httpsServer, browserName }) => {
test.skip(browserName === 'webkit', 'getDirectory is not supported in ephemeral context in WebKit https://github.com/microsoft/playwright/issues/18235#issuecomment-1289792576');
test.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/29901' });

let context = await reusedContext({ ignoreHTTPSErrors: true });
let page = await context.newPage();
await page.goto(httpsServer.EMPTY_PAGE);
await page.evaluate(async () => {
const root = await navigator.storage.getDirectory();
await root.getDirectoryHandle('someDirectoryName', { create: true });
await root.getFileHandle('foo.txt', { create: true });
});

context = await reusedContext({ ignoreHTTPSErrors: true });
page = await context.newPage();
await page.goto(httpsServer.EMPTY_PAGE);

const { directoryExits, fileExits } = await page.evaluate(async () => {
const root = await navigator.storage.getDirectory();
let directoryExits = true, fileExits = true;
await root.getDirectoryHandle('someDirectoryName').catch(() => { directoryExits = false; }).catch(() => { fileExits = false; });
await root.getFileHandle('foo.txt').catch(() => { fileExits = false; });
return { directoryExits, fileExits };
});
expect(directoryExits).toBe(false);
expect(fileExits).toBe(false);
});

test('should reset tracing', async ({ reusedContext, trace }, testInfo) => {
test.skip(trace === 'on');

Expand Down
8 changes: 4 additions & 4 deletions tests/playwright-test/playwright.reuse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ test('should clean storage', async ({ runInlineTest }) => {
let lastContextGuid;
test.beforeEach(async ({ page }) => {
await page.route('**/*', route => route.fulfill('<html></html>'));
await page.route('**/*', route => route.fulfill({ body: '<html></html>', contentType: 'text/html' }));
await page.goto('http://example.com');
});
Expand Down Expand Up @@ -273,7 +273,7 @@ test('should restore localStorage', async ({ runInlineTest }) => {
});
test.beforeEach(async ({ page }) => {
await page.route('**/*', route => route.fulfill('<html></html>'));
await page.route('**/*', route => route.fulfill({ body: '<html></html>', contentType: 'text/html' }));
await page.goto('http://example.com');
});
Expand Down Expand Up @@ -330,7 +330,7 @@ test('should clean db', async ({ runInlineTest }) => {
let lastContextGuid;
test.beforeEach(async ({ page }) => {
await page.route('**/*', route => route.fulfill('<html></html>'));
await page.route('**/*', route => route.fulfill({ body: '<html></html>', contentType: 'text/html' }));
await page.goto('http://example.com');
});
Expand Down Expand Up @@ -380,7 +380,7 @@ test('should restore cookies', async ({ runInlineTest }) => {
});
test.beforeEach(async ({ page }) => {
await page.route('**/*', route => route.fulfill('<html></html>'));
await page.route('**/*', route => route.fulfill({ body: '<html></html>', contentType: 'text/html' }));
await page.goto('http://example.com');
});
Expand Down

0 comments on commit 05c48d9

Please sign in to comment.