test('Handle Dialogs', async function({ page })
{
await page.goto("https://testpages.herokuapp.com/styled/alerts/alert-test.html");
await page.waitForLoadState();
// alert popup and okay
const confirmStatus = await page.locator("#alertexamples").isVisible();
console.log("button status:- ", confirmStatus);
await page.locator("#alertexamples").click();
page.on("dialog", async (alert) => {
const text = alert.message();
console.log("text from alert box:- ", text);
await alert.accept();
});
});
问题是代码没有点击按钮。我没有收到任何错误消息。我尝试将点击操作移到监听器代码下方。但仍然没有执行点击操作。 请指教。
在单击之前设置对话框侦听器。可见性检查不是必需的 -
.click()
已经将其作为其可操作性测试的一部分。
import {expect, test} from "@playwright/test"; // ^1.46.1
test("Handle Dialogs", async ({page}) => {
await page.goto(
"https://testpages.herokuapp.com/styled/alerts/alert-test.html"
);
page.once("dialog", async alert => {
console.log(`text from alert box: ${alert.message()}`);
await alert.accept();
});
await page.locator("#alertexamples").click();
});
我认为测试没有义务等待对话框回调运行,因此如果您想进行断言,请查看 寻找用于在 TS 中断言对话框消息的 Web-First 断言和 是吗可以在单独的 AlertHelper 文件上编写“isAlertDisplayed”方法吗?.