Skip to content

[Feature] Aria methods and assertions #13517

@kevin940726

Description

@kevin940726

Originally posted in #12999 (comment).

Now that we have all these accessible attributes helpers, is it worth making them available under locator and assertions as well?

For instance, we want to know whether an element is expanded or not:

const button = page.locator('role=button[name=Expand me]');
const isExpanded = await button.expanded(); // `true` or `false`
await expect(button).toBeExpanded(); // Error if the button is not expanded

I think it's only natural to make them available both via querying and accessing.

Currently, we only have page.accessibility, but that uses browsers' API and it's platform-specific, different from the JS implementation injected into the browsers.

It'll be great to add support for computing aria attributes on Locator and ElementHandle to make them more feature-complete.

Proposed methods on Locator (and ElementHandle):

  • locator.ariaRole(): string: Get the aria role of the element.
  • locator.ariaName(): string: Get the aria name of the element.
  • locator.isChecked(): boolean: Already available.
  • locator.isSelected(): boolean: Returns whether the option is selected.
  • locator.isExpanded(): boolean: Returns whether the element is expanded.
  • locator.isPressed(): boolean: Returns whether the button is being pressed.
  • locator.isDisabled(): boolean: Already available.
  • locator.level(): number: Get the level of the heading element.

And for assertions:

  • expect(locator).toHaveRole(role: string): Test whether the element has the aria role.
  • expect(locator).toHaveName(name: string | RegExp): Test whether the element has the aria name.
  • expect(locator).toBeChecked(): Already available.
  • expect(locator).toBeSelected(): Expect the option to be selected.
  • expect(locator).toBeExpanded(): Expect the element to be expanded.
  • expect(locator).toBePressed(): Expect the button to be pressed.
  • expect(locator).toBeDisabled(): Already available.
  • expect(locator).toHaveLevel(level: number): Expect the heading element to have the level.

c.c. @dgozman

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions