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:
- Created 3 years ago
- Comments:6 (5 by maintainers)
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 ofaria-modal
. I don’t think we should filter by default becausearia-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:– https://www.w3.org/TR/wai-aria-1.2/#aria-modal
In summary:
modal: boolean
to the filter optionsBonus:
<section data-testid="page">...some...content...her</section><div role="dialog" aria-modal="true" data-testid="modal">...other...content...here</div>
[data-testid="modal"]
is accessible i.e. can we navigate to content inside[data-testid="page"]
[data-testid="modal"]
set default formodal
totrue
, otherwisefalse
Thanks @eps1lon… So in the
aria-modal
spec it states this:So I’m not sure this is something we want to enforce…