gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 06:09:13 29 4
gpt4 key购买 nike

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

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com