[Feature]: Resolve Elements with aria-owns and aria-controls as Children in getByRole Method #34348
Description
🚀 Feature Request
We propose adding a feature to the getByRole
method in Playwright to include an ariaChildren
option. This option would allow developers to locate elements referenced by ARIA attributes like aria-owns
and aria-controls
, even when these elements are not direct children in the DOM.
Proposed Solution:
Introduce an ariaChildren
option to the getByRole
method. When enabled, this option will include elements referenced by ARIA attributes in the search scope.
Example
Consider the following DOM structure:
<div role="navigation" aria-owns="menu1 menu2">
<div id="menu1" role="menu">
<div role="menuitem">Home</div>
<div role="menuitem">About</div>
</div>
</div>
<div id="menu2" role="menu">
<div role="menuitem">Services</div>
<div role="menuitem">Contact</div>
</div>
Using the ariaChildren
option, developers can locate elements like this:
import { test as it, expect } from './pageTest';
it('should handle aria-children with elements outside the parent tree', async ({ page }) => {
await page.setContent(`
<div role="navigation" aria-owns="menu1 menu2">
<div id="menu1" role="menu">
<div role="menuitem">Home</div>
<div role="menuitem">About</div>
</div>
</div>
<div id="menu2" role="menu">
<div role="menuitem">Services</div>
<div role="menuitem">Contact</div>
</div>
`);
const menuItem = page.getByRole('navigation').getByRole('menuitem', { name: 'Services', ariaChildren: true });
await expect(menuItem).toHaveText('Services');
});
Motivation
Currently, elements referenced by aria-owns
or aria-controls
attributes are not included in the search scope by default. Without this functionality, it becomes challenging to test and validate these relationships effectively.