question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feature] Testing Library query helper for multiple elements as Locator API (e.g. `getAllBy`)

See original GitHub issue

I 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

image

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:closed
  • Created a year ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
dgozmancommented, Oct 20, 2022

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

Not sure if there is a way to bring them closer together or if this is even planned?

I don’t think we want to change how locators work.

0reactions
bennettdamscommented, Oct 20, 2022

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:

...
<button>My text</button>
<button>My text</button>
<button>My text</button>
....

Testing Library behavior:

// ❌ this already fails the test (multiple elements)
const button = screen.getByText("My text")

await userEvent.click(button)

vs.

Playwright behavior:

// this does not fail the test
const button = await page.getByText("My text")

// ❌ this fails the test (multiple elements)
await button.click()

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?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found