From 684700a24d8f799797c270ed84169fde07b319ab Mon Sep 17 00:00:00 2001 From: Thomas Scholtes Date: Tue, 11 Jul 2023 15:41:55 +0200 Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20flood=20Playwright=20traces=20w?= =?UTF-8?q?ith=20`route.continue()`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We determine URLs to mock with a URL matcher instead of mocking every URL and determining the behavior in the route handler. This should be slightly faster and should result in better debugging experience because we don’t come across the `route.continue()` event that often. Signed-off-by: Thomas Scholtes --- tests/e2e/project.spec.ts | 13 +++++------- tests/support/fixtures.ts | 43 ++++++++++++++++++++------------------- 2 files changed, 27 insertions(+), 29 deletions(-) diff --git a/tests/e2e/project.spec.ts b/tests/e2e/project.spec.ts index 34f107e72..4d236f02c 100644 --- a/tests/e2e/project.spec.ts +++ b/tests/e2e/project.spec.ts @@ -438,14 +438,11 @@ test.describe("browser error handling", () => { }); test("external markdown link", async ({ page }) => { - await page.route("http://github.github.com/**", route => { + await page.route("https://example.com/**", route => { return route.fulfill({ body: "hello", contentType: "text/plain" }); }); - await page.goto(`${markdownUrl}/tree/main/cheatsheet.md`); - await page - .getByRole("link", { name: "Github-flavored Markdown info page" }) - .click(); - await expect(page).toHaveURL( - "http://github.github.com/github-flavored-markdown/", - ); + await page.goto(`${markdownUrl}/tree/main/footnotes.md`); + await page.getByRole("link", { name: "https://example.com" }).click(); + await page.pause(); + await expect(page).toHaveURL("https://example.com"); }); diff --git a/tests/support/fixtures.ts b/tests/support/fixtures.ts index 5b6f0b677..e38379cd6 100644 --- a/tests/support/fixtures.ts +++ b/tests/support/fixtures.ts @@ -112,33 +112,34 @@ export const test = base.extend<{ } const playwrightLabel = logLabel.make("playwright"); - await page.route("**/*", route => { - if ( - route.request().url().startsWith("http://127.0.0.1") || - route.request().url().startsWith("http://localhost") || - route.request().url().startsWith("http://0.0.0.0") - ) { - return route.continue(); - } else if ( - route - .request() - .url() - .startsWith("https://www.gravatar.com/avatar/") || - route.request().url().endsWith(".png") - ) { - return route.fulfill({ - status: 200, - path: "./public/radicle.svg", - }); - } else { + + function isLocalhost(url: URL) { + return url.hostname === "localhost" || url.hostname === "127.0.0.1"; + } + + await page.route( + url => !isLocalhost(url), + route => { log( `Aborted remote request: ${route.request().url()}`, playwrightLabel, outputLog, ); return route.abort(); - } - }); + }, + ); + + await page.route( + url => + url.href.startsWith("https://www.gravatar.com/avatar/") || + (url.href.endsWith(".png") && !isLocalhost(url)), + route => { + return route.fulfill({ + status: 200, + path: "./public/radicle.svg", + }); + }, + ); await use(); },