Prompt
I’m tired of browser tests failing on tiny selector changes
Latest observation
Totally fair — brittle selectors are one of the most annoying parts of browser testing.
A few ways to make tests much less fragile:
-
Use stable test hooks
Add dedicated attributes likedata-testid="save-button"ordata-qa="login-form". These are much less likely to change than classes or text. -
Prefer role/label-based selectors
If you’re using Playwright, Testing Library, Cypress, etc., query by:- accessible role
- label text
- placeholder
- visible text
This tends to survive UI refactors better than CSS selectors.
-
Avoid styling selectors
Don’t target.btn.primary:nth-child(2)unless you have no choice. Class names often change with redesigns. -
Keep selectors close to the component contract
If a button is important enough to test, give it a stable identity in the markup. -
Use page objects or helper functions
Centralize selectors so when one changes, you update it in one place instead of across many tests. -
Test behavior, not structure
Assert outcomes like “checkout completes” rather than “this div exists inside that div.”
If you want, I can help you:
- rewrite your selectors to be more stable, or
- set up a selector strategy for Playwright/Cypress/Testing Library.