fix(reuse): reset Origin Private File System API (#29921)

Fixes https://github.com/microsoft/playwright/issues/29901

This clears the Origin Private File System API when using VSCode
extension.
This commit is contained in:
Max Schmitt 2024-03-15 17:40:46 +01:00 committed by GitHub
parent be465c6a1c
commit 048d6669fd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 49 additions and 8 deletions

View file

@ -1692,6 +1692,17 @@ export class Frame extends SdkObject {
if (db.name) if (db.name)
indexedDB.deleteDatabase(db.name!); indexedDB.deleteDatabase(db.name!);
} }
// Clean StorageManager
const root = await navigator.storage.getDirectory();
const entries = await (root as any).entries();
// Manual loop instead of for await because in Firefox's utility context instanceof AsyncIterable is not working.
let entry = await entries.next();
while (!entry.done) {
const [name] = entry.value;
await root.removeEntry(name, { recursive: true });
entry = await entries.next();
}
}, { ls: newStorage?.localStorage }).catch(() => {}); }, { ls: newStorage?.localStorage }).catch(() => {});
} }

View file

@ -15,13 +15,16 @@
*/ */
import { browserTest, expect } from '../config/browserTest'; 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) => { reusedContext: async ({ browserType, browser }, use) => {
await use(async () => { await use(async (options: BrowserContextOptions = {}) => {
const defaultContextOptions = (browserType as any)._defaultContextOptions; 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; return context;
}); });
}, },
@ -235,6 +238,33 @@ test('should reset mouse position', async ({ reusedContext, browserName, platfor
await expect(page.locator('#two')).toHaveCSS('background-color', 'rgb(0, 0, 255)'); 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; });
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('should reset tracing', async ({ reusedContext, trace }, testInfo) => {
test.skip(trace === 'on'); test.skip(trace === 'on');

View file

@ -212,7 +212,7 @@ test('should clean storage', async ({ runInlineTest }) => {
let lastContextGuid; let lastContextGuid;
test.beforeEach(async ({ page }) => { 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'); await page.goto('http://example.com');
}); });
@ -273,7 +273,7 @@ test('should restore localStorage', async ({ runInlineTest }) => {
}); });
test.beforeEach(async ({ page }) => { 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'); await page.goto('http://example.com');
}); });
@ -330,7 +330,7 @@ test('should clean db', async ({ runInlineTest }) => {
let lastContextGuid; let lastContextGuid;
test.beforeEach(async ({ page }) => { 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'); await page.goto('http://example.com');
}); });
@ -380,7 +380,7 @@ test('should restore cookies', async ({ runInlineTest }) => {
}); });
test.beforeEach(async ({ page }) => { 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'); await page.goto('http://example.com');
}); });