feat(getByLabel): support aria-label (#19726)

References #19284.
This commit is contained in:
Dmitry Gozman 2022-12-27 12:43:55 -08:00 committed by GitHub
parent 3ad65e7ce8
commit 24f2ccb4ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 0 deletions

View File

@ -298,6 +298,11 @@ export class InjectedScript {
const allElements = this._evaluator._queryCSS({ scope: root as Document | Element, pierceShadow: true }, '*');
return allElements.filter(element => {
let labels: Element[] | NodeListOf<Element> | null | undefined = getAriaLabelledByElements(element);
if (labels === null) {
const ariaLabel = element.getAttribute('aria-label');
if (ariaLabel !== null)
return matcher({ full: ariaLabel, immediate: [ariaLabel] });
}
if (labels === null)
labels = (element as HTMLInputElement).labels;
return !!labels && [...labels].some(label => matcher(elementText(this._evaluator._cacheText, label)));

View File

@ -112,6 +112,16 @@ it('getByLabel should prioritize aria-labelledby over native label', async ({ pa
expect(await page.getByLabel('Name').evaluate(e => e.textContent)).toBe('Click me');
});
it('getByLabel should work with aria-label', async ({ page }) => {
await page.setContent(`<input id=target aria-label="Name">`);
expect(await page.getByLabel('Name').evaluate(e => e.id)).toBe('target');
});
it('getByLabel should prioritize aria-labelledby over aria-label', async ({ page }) => {
await page.setContent(`<label id=other-label>Other</label><input id=target aria-label="Name" aria-labelledby=other-label>`);
expect(await page.getByLabel('Other').evaluate(e => e.id)).toBe('target');
});
it('getByPlaceholder should work', async ({ page }) => {
await page.setContent(`<div>
<input placeholder='Hello'>