🔥 0
0
Lesson 3 of 10 25 min +200 XP

Locators That Don't Break

The #1 reason tests break after deployments? Brittle locators. A developer changes a CSS class, and suddenly 50 tests fail. Let's fix that.

The Locator Hierarchy

Playwright recommends locators in this priority order:

1. getByRole()     → Best: matches how users see the page
2. getByText()     → Good: visible text content
3. getByLabel()    → Good: form accessibility
4. getByTestId()   → Good: explicit test contracts
5. CSS selectors   → Okay: when nothing else works
6. XPath           → Last resort: complex DOM traversal

Why this order? User-facing locators are stable. If your "Add to Cart" button changes from