Which locator strategy makes UI tests both stable and meaningful?
Answer
Prefer user-visible roles, labels and stable product contracts; avoid styling and deep DOM structure. A locator should identify what the user interacts with, remain unique, and fail clearly when accessibility or product behaviour changes.
Strong answer includes
- user-facing locators
- resists DOM coupling
- uniqueness and useful failure
Practical example
A test using css .btn-primary > span:nth-child(2) breaks every time a designer tweaks the button's markup, while a rewrite using getByRole('button', { name: 'Submit order' }) survives a full visual redesign because it targets what the user actually sees and clicks, not the implementation detail.