-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature] Allow ElementHandle as an input to DOM expect matchers #11145
Comments
Making assertions work against const buyButton = await queries.findByText($document, "Buy");
// React re-render happens here, and the following line no longer makes sense.
await expect(buyButton).toBeDisabled() Using locator objects solves this and other races. Could you share why you are using testing library to select elements? I would imagine that Playwright's selectors are much more powerful at this point. |
Main reason is that it enforces best practices and prevents bad selectors. I'm aware Playwright's builtin selectors are quite strong, but there are concepts that testing library abstracts away so you don't have to know the underlying selectors implementation/engine. I would love if those selectors returned I guess, It would be fairly trivial to implements some of the testing library selectors to return locators behind the scenes, but 2 that I'm particularly interested in are |
I'm not familiar with how testing library enforces best practices, but your
page.locator(`label:has-text('Username')`) starting with 1.18 it will also be available as page.locator('label').hasText('Username')) Although testing library claims that page.locator('[role=button][aria-label=Username]') |
It's not that simple. I assume the element selected by this would be the
Yeah, I'm fairly certain they have heuristics for this. This for example wouldn't select queries.findByRole("button", { name: "Save" }) The following elements would be selected: <button>Save</button>
<div role="button">Save</div>
<div role="button" aria-label="Save" /> |
It'll resolve to a label, but input methods such as
Got it. We don't have anything like this. Let me treat this issue as a feature request for the role selectors. |
@pavelfeldman thanks for considering this as a feature request. Personally, given the popularity of testing library in React (and wider) ecosystem, I think having those builtin would be very compelling for a lot of people. TBH, this was the first thing I checked for when we were evaluating Playwright. If you think the initial feature request |
@petetnt and I worked on a simple script to use @pavelfeldman The way testing library works is more complex than you suggest, so a ByLabelText selector is not the same as Similarly, it gets implicit roles from the DOM (see implementation) so it doesn't only match when you have explicitly set There's a lot that it abstracts away from you, but you're correct that it's not doing this via the browser (so has to re-invent the wheel a bit). A better Playwright implementation would be very helpful. I wonder if @kentcdodds or @eps1lon might be able to fill you in on the feature set better than I can? |
The original issue makes it sound like this issue is about assertions which I don't have much insight on. Looks like a Playwright issue. |
@eps1lon Sorry, to be clear, this issue is about making a better integration between Playwright and Testing Library. However, I @pavelfeldman created an issue #11182 to track the possibility of implementing something like Sorry for the out of context |
The documentation for |
Thanks for all the pointers! Closing in favor of #11182. |
We're using https://github.com/testing-library/playwright-testing-library for selecting elements on our page. Those return
ElementHandle
, which doesn't seem to be compatible with someexpect
matchers.Simple example:
Throws:
There might be a way to get a
Locator
fromElementHandle
(not sure), but it would be great if those also worked withElementHandle
itself.The text was updated successfully, but these errors were encountered: