Don’t flood Playwright traces with route.continue()

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 <geigerzaehler@axiom.fm>
This commit is contained in:
Thomas Scholtes
2023-07-14 09:29:19 +02:00
committed by Rūdolfs Ošiņš
parent f80b193bbd
commit 684700a24d
2 changed files with 27 additions and 29 deletions
+5 -8
View File
@@ -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");
});
+22 -21
View File
@@ -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();
},