-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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] locator for getting an element's description #18332
Comments
Is there something similar in testing library? I like the feature request to get the aria describedby element! |
Unfortunately we don't have anything like this in Testing Library either. Definitely would be a good one to have though. |
Why was this issue closed?Thank you for your involvement. This issue was closed due to limited engagement (upvotes/activity), lack of recent activity, and insufficient actionability. To maintain a manageable database, we prioritize issues based on these factors. If you disagree with this closure, please open a new issue and reference this one. More support or clarity on its necessity may prompt a review. Your understanding and cooperation are appreciated. |
This is one of those things people don't know they need (hence the lower up-votes) but will use once they have. |
i think it would be useful to get the element's description, but also to locate by description. similar to |
@testing-library/jest-dom has three matchers specifically for accessibility testing:
We've been using this API extensively, and would be happy to have this or something similar with Playwright, too! |
This would be very nice to have in playwright! |
Upvoting this since I'm running into a situation where having this feature would save me some time. |
I would also find this quite useful. We are using Playwright for testing our UI pretty extensively, and this is very helpful for ensuring that the screen reader experience is tested automatically (to some extent). Having locators for getting an element's accessible description, as well as selecting elements by an accessible description would be great. In addition to what Kent proposed above, something like: // Test that there is an element describing field in the page
const description = await page.getByDescription(/required/i)
await expect(description).toBeVisible() |
Hello @dgozman . I've taken a look and started thinking what could be a possible solution. Locators “locate” with selector strings. There are util functions to construct the selector string that can be found in locatorUtils.ts file. These selectors seem that they are some internal selectors because they are prefixed with If the usage of internal selector is not obligatory, an xpath selector like the following can work:
It looks for elements that are the accessible descriptions of an other element. I would be happy ro receive some help |
I think it's worth pointing out that <h1 aria-describedby="description">ZA WARUDO</h1>
<div id="description">
<div aria-hidden="true">Bro... you're a weeb.</div>
<div>Apparently, some entity that can stop time.</div>
</div> My understanding is that the accessible description for the
not
The Google Chrome Devtools seem to agree with this, as does MacOS VoiceOver (again, used on a webpage in Chrome). How would a I'm assuming that we're all here because what we really want is to write tests which verify that our elements have accessible descriptions. To that end, I think it would be better to have an In the meantime, for those of you who absolutely need a way to have Jest DOM Testing Library's /* ---------- Utils File ---------- */
/** Retrieves the [accessible description](https://developer.mozilla.org/docs/Web/Accessibility/ARIA/Attributes/aria-describedby) for an element. */
async function getAccessibleDescriptionFor(element: Locator): Promise<string> {
const page = element.page();
const a11yHelperLoaded = await page.evaluate(() => "computeAccessibleDescription" in window);
if (!a11yHelperLoaded) {
await page.addScriptTag({
type: "module",
content: `
import { computeAccessibleDescription } from "https://cdn.jsdelivr.net/npm/[email protected]/+esm";
window.computeAccessibleDescription = computeAccessibleDescription;
`,
});
await page.waitForFunction("'computeAccessibleDescription' in window");
}
// Note: TS users will need a Type Cast here
return element.evaluate((node) => window.computeAccessibleDescription(node));
}
/* ---------- Within a Test File ---------- */
const email = page.getByRole("textbox", { name: /email/i });
const button = page.getByRole("button", { name: /submit/i });
// Ban invalid emails
await expect(email).toHaveValue("");
await button.click();
await expect(email).toHaveAttribute("aria-invalid", String(true));
expect(await getAccessibleDescriptionFor(email)).toBe("Email is invalid");
// Allow valid emails
await email.fill("[email protected]");
await button.click();
await expect(email).toHaveAttribute("aria-invalid", String(false));
expect(await getAccessibleDescriptionFor(email)).toBe(""); I'm doing things this way because Obviously, there are limitations to this approach. For instance, if you're using CSPs, you'll need a workound. I'm sure there are ways to improve this approach; so feel free to make improvements if possible. But if this is feasible for you for now... that's great. For the Playwright maintainers: The |
Hooray! Thank you 👏👏 |
Looking at the comments in this GitHub Issue, it looks like we have a lot of exciting assertions incoming!
Thanks so much for this! And thanks for exposing an assertion instead of a complex locator. 🙇🏾♂️ |
Really looking forward to this. |
I'm testing an error message.
Here's what I have to do now:
Here's what I'd like to be able to do:
Thanks!
The text was updated successfully, but these errors were encountered: