playwright - 如何检查是否打开了新的浏览器选项卡?

标签 playwright

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

最佳答案

你可以这样实现它

// @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

关于playwright - 如何检查是否打开了新的浏览器选项卡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64889036/

相关文章:

javascript - Playwright 中有没有办法在动态表中选择特定按钮?

typescript - 如何断言 Playwright 中的错误?

python - 具有多个 start_url 的 Scrapy-playwright

post - 如何使用 PlayWright 执行 POST 请求

playwright - 运行时出错 "npx folio": First argument must use the object destructuring pattern: _x

Python Playwrite 内存过载

playwright - 如何使用 Playwright Java 在 Windows 上设置 Chromium 代理?

azure-devops - 如何在 Azure DevOps 测试计划中执行剧作家测试

python - 有没有办法在 Playwright 中返回响应正文?

node.js - 使用 Playwright 制作 PDF 时控制页边距