Locator strategy
Choosing the right locator strategy is crucial for building reliable and maintainable end-to-end tests. This section outlines the recommended locator strategies, ordered by priority and aligned with best practices, to ensure your tests remain robust and expressive as your application evolves.
- Using
data-testid
(or any dedicated test attribute) as the primary locator strategy makes the tests more stable, easier to maintain and focused on test behavior rather that UI structure. - Playwright encourages using semantic, resilient selectors that mirror how users interact with the UI, rather than relying on fragile or implementation-specific details like CSS classes or XPath.
In order of priority:
data-testid
attributes (getByTestId)- User-facing attributes recommended by Playwright:
- Other locators (page.locator)