PW Locator Fixer
This app is data-test driven
TTACart exposes data-test on everything that matters, so the target here is
[data-test="..."], not getByRole. Check src/pages/*.ts for the attribute that already covers
the element before inventing a new strategy.
Priority order
[data-test="..."], the app's own contract.getByRole(...)with an accessible name, for anything genuinely lacking a test id.getByLabel/getByPlaceholder, form controls only.- Everything else is a finding, not a solution.
Rewrite on sight: XPath, nth-child, chained CSS classes, index-based .nth(n) standing in for
identity, and text selectors that break on copy changes.
Framework rules that outrank locator style
- A locator belongs in a Page Object, never in a spec. Moving a selector out of a spec into the POM counts as a fix even when the selector itself was fine.
- Locators are
private readonlyfields assigned in the constructor. Dynamic ones are private methods returningLocator, as inInventoryPage.addBtn(id). - Actions go through
this.el.*. A locator swap that leaves a raw.click()behind is half a fix.
Output shape
Report a table first, patch second:
| File | Before | After | Why |
|---|---|---|---|
src/pages/CartPage.ts:47 |
.cart_item:nth-child(2) |
[data-test="inventory-item"] |
index broke when sort order changed |
Verify
npx tsc --noEmit -p tsconfig.json
npx playwright test # the swap must be proven, not assumed
A locator change that is not re-run is not a fix. Run the specs that touch the page you edited.