在 Playwright 中为请求使用另一个 URI 或路由

问题描述

下面是我的代码。当应用程序被提供并且测试开始时,它使用我在 await page.goto(BASE_URL) 中传递的 BASE_URL。我想为我的所有请求使用另一条路线。

describe("Screenshots - PLAYWRIGHT",() => {
  it("Takes screenshots for multiple browsers",async () => {
    for (const browserType of ["chromium","firefox","webkit"]) {
      const browser = await playwright[browserType].launch();
      const context = await browser.newContext();
      const page = await context.newPage();
      await page.goto(BASE_URL);
      await page.waitForLoadState("networkidle");
      await page.screenshot({ path: `screenshots/example-${browserType}.png` });
      await browser.close();
    }
  });
});

解决方法

我怕不明白。 如果您需要更改 URL,您可能会这样做。 你想路由什么? 我正在使用 page.route 来阻止,例如:

   await page.route(
        re.compile(r"((facebook)|(twitter)|(google))"),lambda route: route.fulfill(status=404,content_type="text/plain"))

当然你也可以做一个 301 重定向到另一个 URL,

  await page.route("**/*",lambda route: route.fulfill(status= 301,location=NEW_URL)

但对我来说,更改 page.goto() 的 URL 似乎更容易