Skip to main content

Locator Strategy

A locator strategy is the method used to find and target a specific UI element for automation — by ID, CSS selector, XPath, accessible role, or test-specific attribute. Choosing a resilient locator strategy is one of the biggest factors in whether an automated test suite stays stable or becomes constantly flaky.

Locator strategies form a rough stability hierarchy: dedicated test attributes (`data-testid`) are most stable, since they exist purely for testing and rarely change for unrelated reasons; IDs and semantic roles are next; CSS classes tied to styling and deep, position-based XPath expressions are least stable, since they break the moment a designer restyles the page or reorders elements.

Teams that invest in a consistent locator strategy upfront — agreeing to add `data-testid` attributes to key interactive elements as a development convention — spend dramatically less time fixing broken selectors later than teams that let automation reach for whatever CSS class happens to be nearby.

Locator Strategy — Definition, Example & How It's Used | QA Bash Glossary | QA Bash