fix: don't throw error on about:blank when blocking ServiceWorker (#32310)

Fixes https://github.com/microsoft/playwright/issues/32292
This commit is contained in:
Max Schmitt 2024-08-26 17:27:21 +02:00 committed by GitHub
parent 5acd2dbf48
commit 596f497633
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 1 deletions

View File

@ -142,7 +142,7 @@ export abstract class BrowserContext extends SdkObject {
if (debugMode() === 'console')
await this.extendInjectedScript(consoleApiSource.source);
if (this._options.serviceWorkers === 'block')
await this.addInitScript(`\nnavigator.serviceWorker.register = async () => { console.warn('Service Worker registration blocked by Playwright'); };\n`);
await this.addInitScript(`\nif (navigator.serviceWorker) navigator.serviceWorker.register = async () => { console.warn('Service Worker registration blocked by Playwright'); };\n`);
if (this._options.permissions)
await this.grantPermissions(this._options.permissions);

View File

@ -29,4 +29,12 @@ it.describe('block', () => {
page.goto(server.PREFIX + '/serviceworkers/empty/sw.html'),
]);
});
it('should not throw error on about:blank', async ({ page }) => {
it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/32292' });
const errors = [];
page.on('pageerror', error => errors.push(error));
await page.goto('about:blank');
expect(errors).toEqual([]);
});
});