Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: have toHaveAttribute(name, value) behave like toHaveText(value) when parameter is an array #34132

Open
Oscaruzzo opened this issue Dec 23, 2024 · 2 comments

Comments

@Oscaruzzo
Copy link

🚀 Feature Request

The assertion expect(locator).toHaveText(value) accepts arrays as parameter and will check that the locator matches n elements and check that the nth element has the nth text value.

A similar behavior for other assertions would be very useful. In particular I'm in need of said behavior for the toHaveAttribute assertion. See example below.

Example

Given this document

<div >
   <div class="theClass" someAttr="a"></div>
   <div class="theClass" someAttr="b"></div>
   <div class="theClass" someAttr="c"></div>
</div>

I'd like this code to succeed

await expect(page.locator(".theClass")).toHaveAttribute("someAttr", ["a", "b", "c"])

and this code to fail

// unexpected "b" in DOM, "d" not found, "a" found in wrong position
await expect(page.locator(".theClass")).toHaveAttribute("someAttr", ["c", "d", "a"])

Motivation

I can write a function that does what I described, using a combination of expect.poll() and locator.getAttribute but I think this is a general enough case that deserves support out of the box. Motivation is basically ease of use.

@Oscaruzzo Oscaruzzo changed the title [Feature]: have toHaveAttribute(name, value) behave like toHaveText assert when parameter is an array [Feature]: have toHaveAttribute(name, value) behave like toHaveText() when parameter is an array Dec 23, 2024
@Oscaruzzo Oscaruzzo changed the title [Feature]: have toHaveAttribute(name, value) behave like toHaveText() when parameter is an array [Feature]: have toHaveAttribute(name, value) behave like toHaveText(value) when parameter is an array Dec 23, 2024
@Oscaruzzo
Copy link
Author

Oscaruzzo commented Dec 23, 2024

Addendum: at the moment I'm doing this, and it works, but I feel it's a bit clunky

await expect.poll(async () => {
  const headers = await page.getByRole('columnheader').all();
  const headersIds = await Promise.all(headers.map(l => l.getAttribute("col-id")));
  return headersIds;
}).toEqual(['FirstName', 'LastName', 'Age']);

@Skn0tt
Copy link
Member

Skn0tt commented Dec 23, 2024

Another thing that should work is this:

await expect.soft(page.locator(".theClass").nth(0)).toHaveAttribute("someAttr", "a")
await expect.soft(page.locator(".theClass").nth(1)).toHaveAttribute("someAttr", "b")
await expect.soft(page.locator(".theClass").nth(2)).toHaveAttribute("someAttr", "c")
await expect(page.locator(".theClass")).toHaveCount(3)

Note the use of soft assertions, so that you'll see the values of all elements if one of them has the wrong attribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants