0

在 Web 应用程序(在 React 中实现)中,当我按下特定按钮时,会打开一个新的浏览器选项卡。我想检查是否发生了这种情况以及新标签的 URL 是否正确。

4

1 回答 1

1

你可以这样实现它

// @ts-check
const playwright = require("playwright");

(async () => {
  const browser = await playwright.chromium.launch();
  const context = await browser.newContext();
  const page = await context.newPage();
  await page.goto('https://example.org/');

  // Important to "start" this promise before the window.open() could happen
  const newPagePromise = new Promise(resolve => context.once("page", resolve))

  // Imagine some internal window.open() logic
  await page.evaluate(() => {
    setTimeout(() => {
      window.open("https://github.com/microsoft/playwright", "_blank")
    }, 3 * 1000)
  })

  // Here we are waiting until the page has been opened
  const newPage = await newPagePromise

  // Since its a normal Page instance, we can now assert the URL of it
  console.log(newPage.url(), await newPage.title())

  await browser.close();
})();

在 Try Playwright 上以交互方式查看它:https ://try.playwright.tech/?s=g8vb1si

于 2020-11-18T10:13:28.183 回答