Accessibility-Tree Queries vs DOM Implementation Coupling
CSS and data-testid selectors pass on clickable divs that screen readers and keyboards cannot reach; querying by computed ARIA role and accessible name tests the user-facing contract.
About this page
DOM Selector Coupling vs Accessibility-Tree Queries
Compare how data-testid hides broken semantics while getByRole enforces ARIA roles and accessible names.
const btn = page.locator('[data-testid="pay-btn"]');
await btn.click(); // Passes even on a non-focusable <div>Passes when <button> is replaced with <div onClick> and no keyboard support.
const btn = page.getByRole('button', { name: 'Pay now' });
await btn.click(); // Fails if role or label is missingEnforces semantic role, visible label, and exclusion of inert/hidden subtrees.
Under the Hood#
Role-based locators in Playwright and Testing Library run a subset of the browser's WAI-ARIA role and AccName 1.2 algorithms on the DOM subtree before matching.
- Implicit & Explicit Role Resolution: The locator maps native elements (
<button>,<a href>,<input type="checkbox">) to implicit ARIA roles unless overridden by an explicitroleattribute. - AccName 1.2 Computation: The query walks
aria-labelledby,aria-label, associated<label>, inner text, andaltattributes in strict spec priority to compute the accessible name. - Subtree Pruning: Nodes inside
display: none,visibility: hidden,aria-hidden="true", or inert containers are excluded from the queryable tree.
// Fails if <div onClick> replaces <button> or label text disappears
await page.getByRole('button', { name: 'Confirm order' }).click();
// Fails if dialog lacks role="dialog" or accessible heading
const modal = page.getByRole('dialog', { name: 'Payment' });
await expect(modal).toBeVisible();Problem Genealogy#
- 2014 (CSS Selector Suites in Selenium/Enzyme): Tests queried
.header > div:nth-child(2) > span, breaking on every CSS refactor and passing when buttons were non-focusable<div>tags. - 2018 (
data-testidEverywhere): Eliminated CSS class coupling, yet still masked missing<label>associations and brokenaria-hiddenstates in 40%+ of audited forms. - 2020 (
getByRoleDefault): Standardized AccName 1.2 queries in Testing Library and Playwright, turning accessibility regressions into immediate test failures.
Production Pitfalls#
- Slow
getByRoleScans on Huge DOMs: RunninggetByRoleacross 5,000+ unvirtualized table cells in JSDOM executes JS AccName traversal synchronously and can add 150ms-400ms per query. - Overusing
nameRegexes on Dynamic Badges: Matching/save/iagainst a button containing an unread count badge or iconaria-labelbreaks when copy changes unless scoped cleanly.
Where this connects
Prerequisites
The Accessibility Tree and WAI-ARIA Semantics
Role and accessible-name queries resolve against WAI-ARIA role mappings and AccName computation.
The inert Attribute and Modal Focus Containment
Accessibility-tree queries respect aria-hidden and inert subtrees when filtering exposed controls.
Seen in Architecture Autopsies
Autopsy #07: 412 Green Tests and a Dead Menu Button
Tests query internal class names instead of ARIA roles, leaving aria-expanded and focus traps unverified.