Skip to content
RED SAGSkills
easyAutomation Test Engineer interview question

How do you choose locators for UI tests, and what makes a locator fragile?

What the interviewer wants to hear

  • Prefer user-facing locators: role, label, text
  • Use stable test ids when roles are not enough
  • Avoid absolute XPath, generated classes and indexes
  • Locators should reflect what the user sees and does
  • Keep locators in one place such as page objects

Model answer

I start with locators that match how a user finds the element: its role and accessible name, its label or its visible text. In Playwright that means getByRole or getByLabel. These survive layout and styling changes and also push the team toward accessible markup. When there is no good accessible name, for example in a dense table, I ask developers for a data-testid attribute. Fragile locators are ones tied to page structure or build output: absolute XPath, nth-child indexes, auto-generated CSS classes or long chains of divs. They break when someone wraps a button in a new container. I keep locators inside page objects or component objects so a change is fixed in one place, and I review new locators in pull requests just like any other code.

Can you answer it under time pressure?

Take a timed easy round and get scored on every answer.

Start mock interview

More easy Automation Test Engineer questions