diff --git a/src/client/browserContext.ts b/src/client/browserContext.ts index 3990c0fa94..10322ebf25 100644 --- a/src/client/browserContext.ts +++ b/src/client/browserContext.ts @@ -141,14 +141,20 @@ export class BrowserContext extends ChannelOwner {}); + if (!handled) { + // it can race with BrowserContext.close() which then throws since its closed + route.continue().catch(() => {}); + } else { + this._routes = this._routes.filter(route => !route.expired()); + } } async _onBinding(bindingCall: BindingCall) { diff --git a/src/client/network.ts b/src/client/network.ts index 2b4713d3ce..9f31cfced1 100644 --- a/src/client/network.ts +++ b/src/client/network.ts @@ -626,15 +626,18 @@ export class RouteHandler { this.handler = handler; } + public expired(): boolean { + return !!this._times && this.handledCount >= this._times; + } + public matches(requestURL: string): boolean { - if (this._times && this.handledCount >= this._times) - return false; return urlMatches(this._baseURL, requestURL, this.url); } public handle(route: Route, request: Request) { this.handler(route, request); - this.handledCount++; + if (this._times) + this.handledCount++; } } diff --git a/src/client/page.ts b/src/client/page.ts index 20ee4c9326..fdedacc84f 100644 --- a/src/client/page.ts +++ b/src/client/page.ts @@ -169,13 +169,18 @@ export class Page extends ChannelOwner !route.expired()); } async _onBinding(bindingCall: BindingCall) {