microsoft / playwright

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
https://playwright.dev
Apache License 2.0
66.85k stars 3.66k forks source link

[Feature]: Extend toHaveAccessibleName() to accept an array of expected accessible names #32593

Open aahlenst opened 1 month ago

aahlenst commented 1 month ago

🚀 Feature Request

It would be great if toHaveAccessibleName() would work as toHaveText() and accept an array of expected accessible names, not only a single string or regular expression. In other words, expected: string | RegExp should be changed to expected: string | RegExp | (string | RegExp)[].

Example

With an Excel-like table where the columns are named A, B, ..., and the rows are named 1, 2, ..., asserting the entire table at once would look something like that:

await expect(page.getByRole("row")).toHaveAccessibleName([
    "Cell A1 Cell B1 Cell C1",
    "Cell A2 Cell B2 Cell C2",
    "Cell A3 Cell B3 Cell C3",
]);

Motivation

To assert the entire table with the current version of toHaveAccessibleName(), I have to write more lines that make the test harder to read:

const rows = [
    "Cell A1 Cell B1 Cell C1",
    "Cell A2 Cell B2 Cell C2",
    "Cell A3 Cell B3 Cell C3",
];
await expect(page.getByRole("row")).toHaveCount(rows.length);
for (let i = 0; i < rows.length; i++) {
    await expect(page.getByRole("row").nth(i)).toHaveAccessibleName(rows[i]);
}

Furthermore, I think it would align nicely with existing assertions that have that capability, for example, toHaveText().

aahlenst commented 1 month ago

Would a PR be considered at this stage?