Playwright 测试库 - 复选框状态

Playwright test library - checkbox state

请问如何在 Playwright E2E 测试库中获取复选框元素的状态。

您可以使用 jsHandle.evaluate():

const checkbox = await page.$('[type=checkbox]');
const checkboxState = await checkbox.evaluate(node => node.checked);

page.$eval():

const checkboxState = await page.$eval('[type=checkbox]', node => node.checked);

page.evaluate():

const checkboxState = await page.evaluate(() => {
    const checkbox = document.querySelector('[type=checkbox]');
    return checkbox.checked;
});

如果你有更多的,你也可以一次完成:

const checkboxStates = await page.evaluate(() => {
    const checkboxes = document.querySelectorAll('[type=checkbox]');
    return [...checkboxes].map(c => c.checked);
});
page.isChecked()

请查看文档:

https://playwright.dev/docs/api/class-page#page-is-checked