Skip to content

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

Open
@Oscaruzzo

Description

@Oscaruzzo

🚀 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions