Locators that survive a redesign
Why This Matters
A locator is a bet on what will not change. Bet on a CSS class and you lose the next time a designer restyles the page. Bet on what the user sees (a button called “Add to Cart”, a field labelled “Username”) and your test only breaks when the behaviour breaks, which is exactly when it should.
The order to try them in
Playwright recommends locators in roughly this order:
| Locator | Finds | Example on the practice site |
|---|---|---|
getByRole | An element by its role and accessible name | getByRole("button", { name: "Log in" }) |
getByLabel | A form field by its label | getByLabel("Username") |
getByPlaceholder, getByText | By placeholder or visible text | getByText("Desk Lamp") |
getByTestId | By a data-testid attribute | getByTestId("cart-button") |
locator(css) | Anything, by CSS or XPath | last resort |
Role and label come first because they test the page the way a user (and a screen reader) experiences it. If getByRole("button", { name: "Log in" }) cannot find the button, a screen-reader user cannot either: that is a real bug.
Test ids: a contract with the developers
practiceautomatedtesting.com puts data-testid attributes on everything you might want to test: search-input, cart-button, add-to-cart-5, checkout-modal. A test id is a promise from the developers that this handle stays stable, whatever the design does. Use it when there is no good role or label, for example for the cart button, whose visible text changes with every product you add (“0 items $0.00”, “1 items $59.99”).
Finding one product among many
Every product card has the same “Add to Cart” button. Ask for it on the whole page and the locator matches the button of every card that is in stock:
page.getByRole("button", { name: "Add to Cart" }); // matches several buttons
Narrow it down by chaining: first find the card, then the button inside it.
const headphones = page.getByTestId(/^product-\d+$/).filter({ hasText: "Bluetooth Headphones" });
await headphones.getByRole("button", { name: "Add to Cart" }).click();
Read it as a sentence: the product card that contains “Bluetooth Headphones”, and in it, the Add to Cart button. Note two details:
getByTestIdaccepts a regular expression./^product-\d+$/matchesproduct-5andproduct-9, but notproduct-grid, which also starts withproduct-.filter({ hasText })keeps only the cards with that text.
Strict mode
What if you click a locator that matches several elements? Playwright refuses. This is the real error from the debugging exercise in module 3:
Error: locator.click: Error: strict mode violation: getByRole('button', { name: 'Add to Cart' }) resolved to 5 elements:
1) <button data-testid="add-to-cart-5" …>Add to Cart</button> aka getByTestId('add-to-cart-5')
2) <button data-testid="add-to-cart-9" …>Add to Cart</button> aka getByTestId('add-to-cart-9')
…
Five, not ten: the shop shows six products per page, and one of them is out of stock. Note the aka hints: Playwright suggests a locator for each match.
That is a feature. Clicking “the first one” silently would make the test depend on the order of the products. When you really do mean any of them, say so with .first(); otherwise, make the locator more specific.
Why not XPath and layout classes
The same button by XPath and by CSS class:
page.locator("//div[3]/div/div[2]/button"); // breaks when a card is added
page.locator(".bg-primary-600.rounded-lg.px-4"); // breaks when the style changes
Both describe where the button is or how it looks, not what it is. The practice site uses Tailwind CSS, where class names are pure styling; they can change in any release without anything breaking for users.
Exercise
On https://practiceautomatedtesting.com/shopping, write locators for:
- The search box
- The “Add to Cart” button of the Desk Lamp
- The “Sort by Price” button
- The “In Stock Only” checkbox
Use a role or label where you can and a test id where you cannot. Check each one in UI mode (npm run test:ui, then pick a locator in the toolbar).
Show suggested solution
page.getByRole("textbox", { name: "Search for items" });
page.getByTestId(/^product-\d+$/).filter({ hasText: "Desk Lamp" }).getByRole("button", { name: "Add to Cart" });
page.getByRole("button", { name: "Sort by Price" });
page.getByRole("checkbox", { name: "In Stock Only" });
All four use what the user sees. getByTestId("in-stock-toggle") and getByTestId("add-to-cart-9") also work and are stable, but add-to-cart-9 hides which product you mean behind an id: the version with filter({ hasText: "Desk Lamp" }) explains itself.