Every Playwright guide says "use getByRole". I wanted to see what that advice is worth on the day a developer renames a button, so I wrote nine locators for the same "Add to cart" button and ran them against a refactor.

The target is our own practice shop. It has a switch, ?rename=1, that changes the button the way a real refactor does: the text becomes "Add to basket", the id goes from add-to-cart-6 to basket-add-6, and the data-testid goes from add-to-cart to basket-add. Same button, same place, same job.

The nine locators

All of them are scoped to one product card, except the CSS id and the XPath, which are written the way they usually appear in real suites:

const card = (page: Page) => page.getByTestId('product-card').filter({ hasText: 'Wireless mouse' }); const strategies: Record<string, (page: Page) => Locator> = { 'getByRole exact name': p => card(p).getByRole('button', { name: 'Add to cart' }), 'getByText': p => card(p).getByText('Add to cart'), 'getByTestId': p => card(p).getByTestId('add-to-cart'), 'CSS id': p => p.locator('#add-to-cart-6'), 'XPath with text': p => p.locator("//article[.//a[text()='Wireless mouse']]//button[text()='Add to cart']"), 'CSS class': p => card(p).locator('button.add'), 'getByRole regex name': p => card(p).getByRole('button', { name: /^add to (cart|basket)$/i }), 'getByRole, any button': p => card(p).getByRole('button'), 'or() fallback': p => card(p).getByTestId('add-to-cart').or(card(p).getByTestId('basket-add')), }; for (const [name, find] of Object.entries(strategies)) { test(name, async ({ page }, info) => { await page.goto('index.html' + (info.project.metadata.query as string)); await find(page).click(); await expect(page.getByTestId('cart-count')).toHaveText('1'); }); } 

Each test clicks the button and checks that the cart badge shows 1. The config runs the same file twice: once on the normal shop, once with ?rename=1.

What broke

On the normal shop, all nine passed. After the rename, five failed with the same error, a 3-second click timeout because nothing matched:

Locator Normal Renamed
getByRole with exact name pass fail
getByText pass fail
getByTestId pass fail
CSS id pass fail
XPath with text pass fail
CSS class (button.add) pass pass
getByRole with a regex name pass pass
getByRole, any button in the card pass pass
getByTestId(...).or(...) pass pass

I ran it with Playwright 1.63.0 on Chromium, first on 23 November and again on 30 November with --repeat-each=3. Same five failures every time, no flaky results.

The survivors are not the winners

It is tempting to read the table as "use regex names and .or()". That would be the wrong lesson.

  • The regex and the .or() fallback only survived because I knew the new name. I wrote "basket" into them. In real life you write the locator before the refactor, so they would have failed too.
  • The CSS class survived because the developer did not touch the class. Next sprint they might, and a class like add is also the kind of thing that gets reused across components.
  • "Any button in the card" survived because the card has one button. Add a "Save for later" button and it clicks whichever comes first. That is worse than a failure: the test can pass while clicking the wrong thing.

So the four survivors are either loose (they will match the wrong element one day) or prescient (they needed knowledge you do not have yet). Neither is a strategy.

What actually protects you

No locator survives every refactor. The useful question is how fast a broken locator gets noticed and fixed, and how rarely it breaks for reasons nobody intended.

  1. Scope to the part of the page you mean. Most locators above start from one product card. That is why even the loose survivors stayed inside the right product instead of clicking the first matching button on the page.
  2. Treat test ids as a contract. getByTestId broke here because the switch renamed it on purpose. In a real team, test ids are the one attribute developers can agree not to change casually. Write that down, and review renames like an API change.
  3. Prefer role and accessible name for what users see. getByRole failing on a text change is arguably correct: the label the user reads changed, and a human should confirm that was intended.
  4. Make failures cheap to fix. A clear timeout on one locator, in one helper, is a two-minute fix. A suite where the same button is found five different ways is not.

This is also why I am careful with "self-healing" claims in testing tools. A tool that quietly switches to "any button in the card" would have turned this rename into a green build, with no guarantee it clicked the right thing.

Run it yourself

The kit is public: zainbasra/aitg-test-kits. It runs against the live practice shop by default, or a local copy with BASE_URL. Expected result: 25 passed, 5 failed, and the 5 failures are the point.

What I did not test: Firefox and WebKit, closed shadow roots, other language bindings, and AI self-healing tools.

The full article covers all nine locators plus 12 harder cases (iframes, shadow DOM, changing ids, late-loading elements, strict mode): Playwright Locators: 9 Types Tested Against a UI Change.