You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Feature] Align getByRole behavior with testing librrary #19046
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>Addpost</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.
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.
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.
Reacted by Felix Leupold, Andrei Botalov and Taylor Gautier
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:
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?
Activity
debs-obrien commentedon Nov 24, 2022
Hi Oliver Sand (@Fox32) can you try adding exact true as the default for exact is in fact false
See the API docs for more details and make sure to update to latest version - v1.28
Fox32 commentedon Nov 24, 2022
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.
debs-obrien commentedon Nov 24, 2022
Yes it is different. I will let Dmitry Gozman (@dgozman) give a better explanation to why we chose the exact default to be false
dgozman commentedon Nov 25, 2022
We made
exact: falsethe default to align with all other locators. Overall, it is usually more convenient to write lax locators, especially when you do that manually.Fox32 commentedon Nov 26, 2022
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
exactwas introduced recently.ZooDoo4U commentedon Dec 12, 2022
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?