diff --git a/docs/src/api/class-browsercontext.md b/docs/src/api/class-browsercontext.md index 94225adb71fec..bf2ee3f3e3200 100644 --- a/docs/src/api/class-browsercontext.md +++ b/docs/src/api/class-browsercontext.md @@ -1415,6 +1415,14 @@ Returns storage state for this browser context, contains current cookies and loc * since: v1.12 - type: <[Tracing]> +## async method: BrowserContext.unrouteAll +* since: v1.41 + +Removes all routes created with [`method: BrowserContext.route`]. + +### option: BrowserContext.unrouteAll.behavior = %%-unroute-all-options-behavior-%% +* since: v1.41 + ## async method: BrowserContext.unroute * since: v1.8 diff --git a/docs/src/api/class-page.md b/docs/src/api/class-page.md index 7860c57e08bbc..041653f3e42b3 100644 --- a/docs/src/api/class-page.md +++ b/docs/src/api/class-page.md @@ -3870,6 +3870,14 @@ When all steps combined have not finished during the specified [`option: timeout ### option: Page.uncheck.trial = %%-input-trial-%% * since: v1.11 +## async method: Page.unrouteAll +* since: v1.41 + +Removes all routes created with [`method: Page.route`]. + +### option: Page.unrouteAll.behavior = %%-unroute-all-options-behavior-%% +* since: v1.41 + ## async method: Page.unroute * since: v1.8 diff --git a/docs/src/api/params.md b/docs/src/api/params.md index c2c9e33c0dc13..38aa703177b92 100644 --- a/docs/src/api/params.md +++ b/docs/src/api/params.md @@ -734,6 +734,14 @@ Whether to allow sites to register Service workers. Defaults to `'allow'`. * `'allow'`: [Service Workers](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API) can be registered. * `'block'`: Playwright will block all registration of Service Workers. +## unroute-all-options-behavior +* since: v1.41 +- `behavior` <[UnrouteAllBehavior]<"wait"|"ignoreErrors"|"default">> + +Specifies wether to wait for already running handlers and what to do if they throw errors: +* `'default'` - do not wait for current handler calls (if any) to finish, if unrouted handler throws, it may result in unhandled error +* `'wait'` - wait for current handler calls (if any) to finish +* `'ignoreErrors'` - do not wait for current handler calls (if any) to finish, all errors thrown by the handlers after unrouting are silently caught ## select-options-values * langs: java, js, csharp diff --git a/packages/playwright-core/src/client/browserContext.ts b/packages/playwright-core/src/client/browserContext.ts index c7eb89c5311df..09b7e5889cd17 100644 --- a/packages/playwright-core/src/client/browserContext.ts +++ b/packages/playwright-core/src/client/browserContext.ts @@ -334,6 +334,10 @@ export class BrowserContext extends ChannelOwner harRouter.addContextRoute(this); } + async unrouteAll(options?: { behavior?: 'wait'|'ignoreErrors'|'default' }): Promise { + await this._unrouteInternal(this._routes, [], options); + } + async unroute(url: URLMatch, handler?: network.RouteHandlerCallback, options?: { noWaitForActive?: boolean }): Promise { const removed = []; const remaining = []; @@ -343,11 +347,17 @@ export class BrowserContext extends ChannelOwner else remaining.push(route); } + const behavior = options?.noWaitForActive ? 'ignoreErrors' : 'wait' + await this._unrouteInternal(removed, remaining, { behavior }); +} + + private async _unrouteInternal(removed: network.RouteHandler[], remaining: network.RouteHandler[], options?: { behavior?: 'wait'|'ignoreErrors'|'default' }): Promise { this._routes = remaining; await this._updateInterceptionPatterns(); - const promises = removed.map(routeHandler => routeHandler.stopAndWaitForRunningHandlers(null, options?.noWaitForActive)); + if (!options || options?.behavior === 'default') + return; + const promises = removed.map(routeHandler => routeHandler.stopAndWaitForRunningHandlers(null, options?.behavior === 'ignoreErrors')); await Promise.all(promises); - } private async _updateInterceptionPatterns() { diff --git a/packages/playwright-core/src/client/page.ts b/packages/playwright-core/src/client/page.ts index 17b63636f3784..aa81e2a344596 100644 --- a/packages/playwright-core/src/client/page.ts +++ b/packages/playwright-core/src/client/page.ts @@ -470,6 +470,10 @@ export class Page extends ChannelOwner implements api.Page harRouter.addPageRoute(this); } + async unrouteAll(options?: { behavior?: 'wait'|'ignoreErrors'|'default' }): Promise { + await this._unrouteInternal(this._routes, [], options); + } + async unroute(url: URLMatch, handler?: RouteHandlerCallback, options?: { noWaitForActive?: boolean }): Promise { const removed = []; const remaining = []; @@ -479,9 +483,16 @@ export class Page extends ChannelOwner implements api.Page else remaining.push(route); } + const behavior = options?.noWaitForActive ? 'ignoreErrors' : 'wait' + await this._unrouteInternal(removed, remaining, { behavior }); + } + + private async _unrouteInternal(removed: RouteHandler[], remaining: RouteHandler[], options?: { behavior?: 'wait'|'ignoreErrors'|'default' }): Promise { this._routes = remaining; await this._updateInterceptionPatterns(); - const promises = removed.map(routeHandler => routeHandler.stopAndWaitForRunningHandlers(this, options?.noWaitForActive)); + if (!options || options?.behavior === 'default') + return; + const promises = removed.map(routeHandler => routeHandler.stopAndWaitForRunningHandlers(this, options?.behavior === 'ignoreErrors')); await Promise.all(promises); } diff --git a/packages/playwright-core/types/types.d.ts b/packages/playwright-core/types/types.d.ts index 24a6e700888a3..5a3074327b4a0 100644 --- a/packages/playwright-core/types/types.d.ts +++ b/packages/playwright-core/types/types.d.ts @@ -4253,6 +4253,23 @@ export interface Page { noWaitForActive?: boolean; }): Promise; + /** + * Removes all routes created with + * [page.route(url, handler[, options])](https://playwright.dev/docs/api/class-page#page-route). + * @param options + */ + unrouteAll(options?: { + /** + * Specifies wether to wait for already running handlers and what to do if they throw errors: + * - `'default'` - do not wait for current handler calls (if any) to finish, if unrouted handler throws, it may + * result in unhandled error + * - `'wait'` - wait for current handler calls (if any) to finish + * - `'ignoreErrors'` - do not wait for current handler calls (if any) to finish, all errors thrown by the handlers + * after unrouting are silently caught + */ + behavior?: "wait"|"ignoreErrors"|"default"; + }): Promise; + url(): string; /** @@ -8636,6 +8653,23 @@ export interface BrowserContext { noWaitForActive?: boolean; }): Promise; + /** + * Removes all routes created with + * [browserContext.route(url, handler[, options])](https://playwright.dev/docs/api/class-browsercontext#browser-context-route). + * @param options + */ + unrouteAll(options?: { + /** + * Specifies wether to wait for already running handlers and what to do if they throw errors: + * - `'default'` - do not wait for current handler calls (if any) to finish, if unrouted handler throws, it may + * result in unhandled error + * - `'wait'` - wait for current handler calls (if any) to finish + * - `'ignoreErrors'` - do not wait for current handler calls (if any) to finish, all errors thrown by the handlers + * after unrouting are silently caught + */ + behavior?: "wait"|"ignoreErrors"|"default"; + }): Promise; + /** * **NOTE** Only works with Chromium browser's persistent context. * diff --git a/tests/library/browsercontext-route.spec.ts b/tests/library/browsercontext-route.spec.ts index 8cf13254bd215..8f2cd92e91756 100644 --- a/tests/library/browsercontext-route.spec.ts +++ b/tests/library/browsercontext-route.spec.ts @@ -139,6 +139,77 @@ it('unroute should not wait for pending handlers to complete if noWaitForActive expect(secondHandlerCalled).toBe(true); }); +it('unrouteAll removes all handlers', async ({ page, context, server }) => { + await context.route('**/*', route => { + void route.abort(); + }); + await context.route('**/empty.html', route => { + void route.abort(); + }); + await context.unrouteAll(); + await page.goto(server.EMPTY_PAGE); +}); + +it('unrouteAll should wait for pending handlers to complete', async ({ page, context, server }) => { + it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/23781' }); + let secondHandlerCalled = false; + await context.route(/.*/, async route => { + secondHandlerCalled = true; + await route.abort(); + }); + let routeCallback; + const routePromise = new Promise(f => routeCallback = f); + let continueRouteCallback; + const routeBarrier = new Promise(f => continueRouteCallback = f); + const handler = async route => { + routeCallback(); + await routeBarrier; + await route.fallback(); + }; + await context.route(/.*/, handler); + const navigationPromise = page.goto(server.EMPTY_PAGE); + await routePromise; + let didUnroute = false; + const unroutePromise = context.unrouteAll({ behavior: 'wait' }).then(() => didUnroute = true); + await new Promise(f => setTimeout(f, 500)); + expect(didUnroute).toBe(false); + continueRouteCallback(); + await unroutePromise; + expect(didUnroute).toBe(true); + await navigationPromise; + expect(secondHandlerCalled).toBe(false); +}); + +it('unrouteAll should not wait for pending handlers to complete if behavior is ignoreErrors', async ({ page, context, server }) => { + it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/23781' }); + let secondHandlerCalled = false; + await context.route(/.*/, async route => { + secondHandlerCalled = true; + await route.abort(); + }); + let routeCallback; + const routePromise = new Promise(f => routeCallback = f); + let continueRouteCallback; + const routeBarrier = new Promise(f => continueRouteCallback = f); + const handler = async route => { + routeCallback(); + await routeBarrier; + throw new Error('Handler error'); + }; + await context.route(/.*/, handler); + const navigationPromise = page.goto(server.EMPTY_PAGE); + await routePromise; + let didUnroute = false; + const unroutePromise = context.unrouteAll({ behavior: 'ignoreErrors' }).then(() => didUnroute = true); + await new Promise(f => setTimeout(f, 500)); + await unroutePromise; + expect(didUnroute).toBe(true); + continueRouteCallback(); + await navigationPromise.catch(e => void e); + // The error in the unrouted handler should be silently caught and remaining handler called. + expect(secondHandlerCalled).toBe(false); +}); + it('should yield to page.route', async ({ browser, server }) => { const context = await browser.newContext(); await context.route('**/empty.html', route => { diff --git a/tests/page/page-route.spec.ts b/tests/page/page-route.spec.ts index 578d763914173..df7ab719069e8 100644 --- a/tests/page/page-route.spec.ts +++ b/tests/page/page-route.spec.ts @@ -132,6 +132,78 @@ it('unroute should not wait for pending handlers to complete if noWaitForActive expect(secondHandlerCalled).toBe(true); }); +it('unrouteAll removes all routes', async ({ page, server }) => { + await page.route('**/*', route => { + void route.abort(); + }); + await page.route('**/empty.html', route => { + void route.abort(); + }); + await page.unrouteAll(); + const response = await page.goto(server.EMPTY_PAGE); + expect(response.ok()).toBe(true); +}); + +it('unrouteAll should wait for pending handlers to complete', async ({ page, server }) => { + it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/23781' }); + let secondHandlerCalled = false; + await page.route(/.*/, async route => { + secondHandlerCalled = true; + await route.abort(); + }); + let routeCallback; + const routePromise = new Promise(f => routeCallback = f); + let continueRouteCallback; + const routeBarrier = new Promise(f => continueRouteCallback = f); + const handler = async route => { + routeCallback(); + await routeBarrier; + await route.fallback(); + }; + await page.route(/.*/, handler); + const navigationPromise = page.goto(server.EMPTY_PAGE); + await routePromise; + let didUnroute = false; + const unroutePromise = page.unrouteAll({ behavior: 'wait' }).then(() => didUnroute = true); + await new Promise(f => setTimeout(f, 500)); + expect(didUnroute).toBe(false); + continueRouteCallback(); + await unroutePromise; + expect(didUnroute).toBe(true); + await navigationPromise; + expect(secondHandlerCalled).toBe(false); +}); + +it('unrouteAll should not wait for pending handlers to complete if behavior is ignoreErrors', async ({ page, server }) => { + it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/23781' }); + let secondHandlerCalled = false; + await page.route(/.*/, async route => { + secondHandlerCalled = true; + await route.abort(); + }); + let routeCallback; + const routePromise = new Promise(f => routeCallback = f); + let continueRouteCallback; + const routeBarrier = new Promise(f => continueRouteCallback = f); + const handler = async route => { + routeCallback(); + await routeBarrier; + throw new Error('Handler error'); + }; + await page.route(/.*/, handler); + const navigationPromise = page.goto(server.EMPTY_PAGE); + await routePromise; + let didUnroute = false; + const unroutePromise = page.unrouteAll({ behavior: 'ignoreErrors' }).then(() => didUnroute = true); + await new Promise(f => setTimeout(f, 500)); + await unroutePromise; + expect(didUnroute).toBe(true); + continueRouteCallback(); + await navigationPromise.catch(e => void e); + // The error in the unrouted handler should be silently caught. + expect(secondHandlerCalled).toBe(false); +}); + it('should support ? in glob pattern', async ({ page, server }) => { server.setRoute('/index', (req, res) => res.end('index-no-hello')); server.setRoute('/index123hello', (req, res) => res.end('index123hello'));