[Feature] Testing Library query helper for multiple elements as Locator API (e.g. `getAllBy`)
See original GitHub issueI used Testing Library for years and also the Playwright plugin to be able to use queries in my Playwright tests, so I am really happy that with v1.27.0
they’re now part of the official API 🥳
What is missing right now is the support for queries that are used to access multiple elements:
https://testing-library.com/docs/queries/about#types-of-queries
These “all” queries (e.g. getAllBy...
) are essential to use in some circumstances, so I am still bound to use the Playwright Testing Library right now.
It would be nice if the queries for multiple elements would also be part of the official Locator API.
Issue Analytics
- State:
- Created a year ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
About Queries
If you need to find more than one element, use findAllBy . Multiple Elements. getAllBy... : Returns an array of all matching nodes...
Read more >React Testing Library Cheat Sheet
This React Testing Library Cheat Sheet will answer all your questions! React Testing Library is a set of helpers built on top of...
Read more >React Testing Tutorial - 28 - Query Multiple Elements - YouTube
Sign up for free here - https://studio.harperdb.io/sign-up?utm_source=codevolution2022 Courses - https://learn.codevolution.dev/ Support ...
Read more >Separation of concerns in getByX / queryByX / etc. API #266
Describe the feature you'd like: · APIs such as react-testing-library 's render would no-longer need to take a queries option; it would always ......
Read more >How to query by text string which contains html tags using ...
Update 2. Having used this many times, I've created a helper. Below is an example test using this helper. Test helper:.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@bennettdams Playwright’s locator is a “way to find the element”. It does not actually look inside the page at the time of creation, it only matches element when you do something with it. This means
page.getByText('My text')
can never fail. It will only be resolved to the element upon the action, assertion or something else. I’d encourage you to read the locators guide that explains this, auto-waiting and more.I don’t think we want to change how locators work.
Ahh, so there is no “API feature parity” right now. Testing Library queries already fail when there are multiple elements as a result, Playwright will not. But Playwright will fail when you try to do something with the result.
Basic example: Imagine we render three buttons, but we expect them to all have a different text each. But because of a bug, they now all show the same text:
Testing Library behavior:
vs.
Playwright behavior:
I’m so used to the Testing Library queries being the ones failing that I didn’t think about Playwright’s locator strictness for the new locator helpers.
Not sure if there is a way to bring them closer together or if this is even planned?