Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(route): update patterns before awaiting interceptor removal (#43551)

Devin Rousso66070a1eaa0b
diff --git a/packages/playwright-core/src/server/dispatchers/browserContextDispatcher.ts b/packages/playwright-core/src/server/dispatchers/browserContextDispatcher.ts
index 35b19a550f..09207e26e5 100644
--- a/packages/playwright-core/src/server/dispatchers/browserContextDispatcher.ts
+++ b/packages/playwright-core/src/server/dispatchers/browserContextDispatcher.ts
@@ -314,14 +314,13 @@ export class BrowserContextDispatcher extends Dispatcher<BrowserContext, channel
async setNetworkInterceptionPatterns(params: channels.BrowserContextSetNetworkInterceptionPatternsParams, progress: Progress): Promise<void> {
const hadMatchers = this._interceptionUrlMatchers.length > 0;
+ this._interceptionUrlMatchers = params.patterns.map(deserializeURLMatch);
if (!params.patterns.length) {
// Note: it is important to remove the interceptor when there are no patterns,
// because that disables the slow-path interception in the browser itself.
if (hadMatchers)
await progress.race(this._context.removeRequestInterceptor(this._requestInterceptor));
- this._interceptionUrlMatchers = [];
} else {
- this._interceptionUrlMatchers = params.patterns.map(deserializeURLMatch);
if (!hadMatchers)
await this._context.addRequestInterceptor(progress, this._requestInterceptor);
}
diff --git a/packages/playwright-core/src/server/dispatchers/pageDispatcher.ts b/packages/playwright-core/src/server/dispatchers/pageDispatcher.ts
index 766da99153..790ffdca7f 100644
--- a/packages/playwright-core/src/server/dispatchers/pageDispatcher.ts
+++ b/packages/playwright-core/src/server/dispatchers/pageDispatcher.ts
@@ -208,14 +208,13 @@ export class PageDispatcher extends Dispatcher<Page, channels.PageChannel, Brows
async setNetworkInterceptionPatterns(params: channels.PageSetNetworkInterceptionPatternsParams, progress: Progress): Promise<void> {
const hadMatchers = this._interceptionUrlMatchers.length > 0;
+ this._interceptionUrlMatchers = params.patterns.map(deserializeURLMatch);
if (!params.patterns.length) {
// Note: it is important to remove the interceptor when there are no patterns,
// because that disables the slow-path interception in the browser itself.
if (hadMatchers)
await progress.race(this._page.removeRequestInterceptor(this._requestInterceptor));
- this._interceptionUrlMatchers = [];
} else {
- this._interceptionUrlMatchers = params.patterns.map(deserializeURLMatch);
if (!hadMatchers)
await this._page.addRequestInterceptor(progress, this._requestInterceptor);
}
diff --git a/tests/page/page-route.spec.ts b/tests/page/page-route.spec.ts
index e7887a52ce..9087afd784 100644
--- a/tests/page/page-route.spec.ts
+++ b/tests/page/page-route.spec.ts
@@ -977,15 +977,24 @@ it('RO-044 should support cors for different methods', async ({ page, server })
}
});
-it('RO-045 should support the times parameter with route matching', async ({ page, server }) => {
+it('RO-045 should support the times parameter with route matching', {
+ annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/43507' },
+}, async ({ page, server }) => {
+ await page.goto(server.EMPTY_PAGE);
+ function fetchBody() {
+ return page.evaluate(() => fetch('/empty.html').then(response => response.text()));
+ }
+
const intercepted = [];
await page.route('**/empty.html', route => {
intercepted.push(1);
- void route.continue();
+ void route.fulfill({ body: 'first' });
}, { times: 1 });
- await page.goto(server.EMPTY_PAGE);
- await page.goto(server.EMPTY_PAGE);
- await page.goto(server.EMPTY_PAGE);
+ expect(await fetchBody()).toBe('first');
+ await page.route('**/empty.html', route => route.fulfill({ body: 'second' }), { times: 1 });
+ expect(await fetchBody()).toBe('second');
+ await fetchBody();
+ await fetchBody();
expect(intercepted).toHaveLength(1);
});