question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ByRole and aria-modal

See original GitHub issue
  • @testing-library/dom version: 7.29
  • Testing Framework and version: Cypress 6.1.0
  • DOM Environment: Chrome 87.0.4280.88

Relevant code or config:

  it.only('no aria-modal support', () => {
    cy.visit('https://ns2hp.csb.app/');

    cy.findByRole('button', { name: 'Go' }).click();
    cy.findByText('Modal title').should('be.visible');
    cy.wait(500);

    cy.findByRole('button', { name: 'Go' }).click();
    
  // Note: This does produce the expected result
  // cy.get('[aria-modal=true]').findByRole('button', { name: 'Go' }).click();
  });

What you did:

In a Cypress test a Bootstrap modal dialog is opened. Once the modal is opened findByRole() is used to search for a second button inside the modal with the same label as a button outside of the modal.

What happened:

Received an error when running the test: Timed out retrying: Found multiple elements with the role “button” and name “Go”

Reproduction:

See for the code under test: https://codesandbox.io/s/epic-hooks-ns2hp?file=/index.html

The Cypress test is above

Problem description:

According to the specification using the aria-modal=true attribute means the windows underneath the current dialog are not available for interaction (inert). Therefor the button outside the modal should not have been selected by findByRole().

Note that explicitly searching for the modal root and doing the find on the subtree work’s as expected.

Suggested solution:

Check if aria-modal=true is present in the current subtree and if so limit the search to there.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
eps1loncommented, Dec 21, 2020

The second time it should only search in the aria-modal=true subtree and only find that button.

I think what we should rather add a filter for modal: true. Right now we’re including all elements of the a11y tree which includes elements outside of aria-modal. I don’t think we should filter by default because aria-modal does not necessarily have a direct effect on the UX. Without JS you could still tab to elements outside of it as far as I know.

So if we decide to ignore elements that are not part of [aria-modal="true"] (if such an element exists) then we should only do so if assistive technology has widespread support. We should be very careful with supporting ARIA semantics if these don’t work in actuality.

Also note that by spec aria-modal does not necessarily limit navigation:

Assistive technologies MAY limit navigation to the modal element’s contents.

https://www.w3.org/TR/wai-aria-1.2/#aria-modal

In summary:

  • add modal: boolean to the filter options

Bonus:

  1. prepare a minimal page with <section data-testid="page">...some...content...her</section><div role="dialog" aria-modal="true" data-testid="modal">...other...content...here</div>
    • no framework e.g. no bootstrap
    • no JS
    • no CSS
  2. Check if content outside of [data-testid="modal"] is accessible i.e. can we navigate to content inside [data-testid="page"]
    • NVDA + firefox
    • VoiceOver + safari (mobile and desktop)
    • JAWS + IE 11
  3. If all of these exclude content within [data-testid="modal"] set default for modal to true, otherwise false
0reactions
MatanBobicommented, Feb 9, 2021

Thanks @eps1lon… So in the aria-modal spec it states this:

Assistive technologies MAY limit navigation to the modal element’s contents. If focus moves to an element outside the modal element, assistive technologies SHOULD NOT limit navigation to the modal element.

So I’m not sure this is something we want to enforce…

Read more comments on GitHub >

github_iconTop Results From Across the Web

ByRole | Testing Library
Roles are matched literally by string equality, without inheriting from the ARIA role hierarchy. As a result, querying a superclass role like ...
Read more >
Using ARIA: Roles, states, and properties - MDN Web Docs
ARIA defines semantics that can be applied to elements, with these divided into roles (defining a type of user interface element) and states ......
Read more >
ARIA 2 Requirements - Protocols and Formats Working Group ...
Any element with role=dialog or role=alertdialog should have another optional attribute aria-modal. If true, this should be a strong signal to assistive ...
Read more >
ARIA Widget Checklist: For Screen Reader Testing
Includes role=”dialog”, aria-label, aria-describedby, aria-modal, and aria-hidden. Expected behavior: Activating the Login button should generate a modal ...
Read more >
How using Testing Library will help you improve the ...
Instead, you should make the modal accessible by adding role , aria-modal , and aria-labelledby attributes. role identifies the element as a ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found