Search GimmeJob

Type at least 2 characters. Enter also searches by meaning.
QA interview question
Automation & CIMiddleOccasionalTheory

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.

#locators#playwright#selenium

Sources

Playwright best practicesMicrosoft · Reliable modern browser automationSelenium documentationSelenium Project · WebDriver architecture, waits, Grid and browser automation