0Pricing
Angular Academy · Lesson

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

  1. E2E Testing Overview
  2. Writing Playwright Tests
  3. Selectors and Assertions
  4. CI Integration
← Back to Angular Academy