Day 66 of 100: Locators Part 1 — getByRole and Accessibility-First
Day 66 of 100 — 100 Days of Playwright with TypeScript Challenge. Phase: Playwright. JavaScript (Days 1-33) → TypeScript (Days 34-63) → Playwright (Days 64-96) → AI Framework Capstone (Days 97-100).
🎭 Want to master this with real projects? Join the Playwright Automation Mastery course at The Testing Academy.
Locators are HOW tests find elements. Playwright ranks them: user-facing first. getByRole is king.
Contents
The User-Facing Set
// getByRole — finds elements the way assistive tech does:
page.getByRole('button', { name: 'Sign in' });
page.getByRole('link', { name: 'Documentation' });
page.getByRole('heading', { name: 'Welcome', level: 1 });
page.getByRole('textbox', { name: 'Email' });
page.getByRole('checkbox', { name: 'Remember me' });
// Companions:
page.getByLabel('Password'); // form fields by label
page.getByPlaceholder('Search...'); // by placeholder
page.getByText('Order confirmed'); // by visible text
page.getByAltText('Company logo'); // images
page.getByTestId('checkout-summary'); // last resort escape hatch
🚀 Level Up Your Playwright
From locators to CI pipelines — build a production-grade Playwright + TypeScript framework step by step.
Why Role Beats CSS
- Survives refactors: class names change, roles rarely do
- Doubles as an accessibility check — unnamed button = a11y bug AND locator failure
- Reads like the user story: ‘click the Sign in button’
- CSS/XPath couple tests to DOM structure — the #1 maintenance tax in Selenium suites
Strictness — A Feature
// Locators must match EXACTLY ONE element when acted upon:
// page.getByRole('button') → error if 5 buttons exist
// Fix by being specific:
page.getByRole('button', { name: 'Delete', exact: true });
// Strict mode converts ambiguity into loud failures instead of
// silently clicking the wrong element.
Tomorrow (Day 67): Locators part 2 — chaining, filter, and handling lists.
🎓 Master Playwright End to End
Join hundreds of SDETs building real automation frameworks. Lifetime access, hands-on projects, and a job-ready portfolio.
