What are your best practices for writing stable CSS/XPath selectors?
- selectors
- locators
- stability
Answer it first, then compare. Nothing here is saved or scored.
Model answer
Priority order: (1) data-testid attributes, most stable, decoupled from styling. (2) ARIA roles and labels (getByRole, getByLabel), user-centric and accessible. (3) Text content (getByText), natural but fragile to copy changes. (4) CSS classes, avoid unless stable and semantic. (5) Never use: auto-generated IDs, position-based selectors (nth-child), deeply nested paths. Work with developers to add test attributes during development. The best selector is one that only changes when the feature changes.
What interviewers listen for
- Test architecture instinctsYou can defend why your suite is shaped like a pyramid, a trophy, or a honeycomb.
- Locator disciplineYou rank selector strategies by stability and explain when to break the rule.
- Flaky test ownershipYou debug to a structural fix instead of retry-until-green.
- Parallelisation literacyYou know how your framework isolates state, fixtures, and database access across workers.