Selectors and Assertions
Target elements and assert on state.
Good Selectors Make Stable Tests
How you find elements determines how robust your tests are. Selectors tied to CSS classes or DOM structure break on every redesign. Semantic, user-facing locators survive refactors because they reflect what users actually perceive.
getByRole: The Preferred Locator
getByRole finds elements by their accessibility role and accessible name. It is the most resilient locator and doubles as an accessibility check.
page.getByRole('button', { name: 'Submit' });
page.getByRole('heading', { name: 'Dashboard' });
page.getByRole('link', { name: 'Profile' });All lessons in this course
- E2E Testing Overview
- Writing Playwright Tests
- Selectors and Assertions
- CI Integration