Skip to content

[Feature] Align getByRole behavior with testing librrary #19046

Closed as not planned

Description

@Fox32

I really like the approach of getByRole and similar selectors in testing library and therefore I'm happy to use the in playwright too!

There is one difference that makes problems for me.
Having a page like:

<button>Add</button>
<button>Add post</button>

Using the getByRole selector in testing library would match a single result:

screen.getByRole('button', {name: 'Add'}) // Returns only the first button

While the same selector in playwright matches both and therefore fail:

page.getByRole('button', {name: 'Add'}) // Fails because it matches both buttons.

The difference between Playwright and testing library is, that Playwright uses a contains query if a string is passed as the name, while testing library is using an exact match. In testing library there is an exact option to toggle this behavior, which defaults to true. In both libraries you can use RegEx to perform contains or other matches.

There is currently a workaround that I'm using for an exact match:

page.getByRole('button', { name: /^Add$/ })

Even though it is a behavior change, I would vote for aligning the behavior of the selectors, especially as they claim to be like testing library. That makes it a lot easier for devs to switch from one library to another.

Activity

  1. debs-obrien commented on Nov 24, 2022

    @debs-obrien
    Contributor

    Hi Oliver Sand (@Fox32) can you try adding exact true as the default for exact is in fact false

    await page.getByRole('button', { name: 'Add', exact: true }).click();

    See the API docs for more details and make sure to update to latest version - v1.28

  2. Fox32 commented on Nov 24, 2022

    @Fox32
    ContributorAuthor

    Ah, I'm still on 1.27, but yeah that is the behavior I mean. The problem is that the default for exact is different between testing library and playwright.

  3. debs-obrien commented on Nov 24, 2022

    @debs-obrien
    Contributor

    Yes it is different. I will let Dmitry Gozman (@dgozman) give a better explanation to why we chose the exact default to be false

  4. dgozman commented on Nov 25, 2022

    @dgozman
    Collaborator

    We made exact: false the default to align with all other locators. Overall, it is usually more convenient to write lax locators, especially when you do that manually.

  5. Fox32 commented on Nov 26, 2022

    @Fox32
    ContributorAuthor

    Ok, that's a good reasoning. Selectors like text= are also contains queries by default.

    I'm fine with keeping it, now I know that exact was introduced recently.

  6. ZooDoo4U commented on Dec 12, 2022

    @ZooDoo4U

    Would assert if, or as Playwright is a test library, being exact is more important and should be the default behavior. Or better? Provide both by doing:

    page.getByRole('button', {nameExact: 'Add'})
    page.getByRole('button', {nameContains: 'Add'})
    page.getByRole('button', {nameExactIgnoreCase: 'Add'})
    page.getByRole('button', {nameContainsIgnoreCase: 'Add'})

    Remove 'name' and only have the fully qualified match behavior...?

    Also would assert "name == name", and "name != name*". Or again why is it most every library out there has Equals and/or Contains/ContainsIgnoreCase... Seems strange lax/contains would be the default and add confusion... If one wants a lax match, easy enough with a regex too?

    Also locators are strict by default. Just saying the precedence in the library is more of a strict nature. Not a bad thing, if users know they must be exact, would argue that increases the quality of the testing?

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions