Skip to content

Lesson 2 of 9 · 22m

Locators That Survive Refactors

Choose role, label, text, and test-id locators based on user-visible contracts instead of brittle DOM structure.

Most selector pain is self-inflicted. Teams bind tests to implementation artifacts — generated class names, DOM depth, nth-child positions — and then act surprised when a harmless UI refactor breaks forty tests. The correct question is not 'what selector can find this element today?' It is 'what stable contract does this element have with the user?'

Prefer accessibility-shaped locators

Playwright's role locators encode how users and assistive technology perceive the page:

await page.getByRole('button', { name: 'Create project' }).click();
await page.getByRole('textbox', { name: 'Project name' }).fill('Billing migration');
await expect(page.getByRole('heading', { name: 'Billing migration' })).toBeVisible();

These locators survive class changes, layout shifts, and component rewrites as long as the user-facing semantics remain intact. They also expose accessibility mistakes early. If a button cannot be found by role and accessible name, that is often a product bug rather than test inconvenience.

Use labels for form controls

await page.getByLabel('Email address').fill('qa+billing@example.test');
await page.getByLabel('Remember this device').check();

Label association is a real HTML contract. getByLabel rewards properly wired label elements and punishes placeholder-only inputs. Placeholders are not labels; they disappear during input and are unreliable accessibility names.

Test IDs are a controlled escape hatch

Not every element has a meaningful role or unique text. Charts, canvas controls, row-level actions, and repeated icons may need explicit test IDs:

await page.getByTestId('invoice-row-actions').click();

A test ID is an API. Keep it boring, stable, and detached from styling. Do not encode page position, feature experiments, or copy text in it. data-testid="submit" is weak because the behavior is vague; data-testid="invoice-send-reminder" is a contract.

Avoid structural selectors as the primary contract

This is a liability:

page.locator('main > div:nth-child(3) > div.card div.actions button:first-of-type')

It encodes unrelated layout decisions. Structural selectors are acceptable for narrowing inside a stable container, not as the primary identity of the element:

const row = page.getByRole('row', { name: /INV-1042/ });
await row.getByRole('button', { name: 'Send reminder' }).click();

Ambiguity is a test design signal

If Strict mode violation says a locator resolved to three elements, do not silence it with .first() reflexively. Ask why three indistinguishable controls exist. Sometimes the page needs better labels. Sometimes the test needs to scope to the right region. Sometimes duplicate render means the application is double-mounting during hydration.

Field notes

  • Accessible names come from labels, aria attributes, text content, and browser algorithms — not merely the visible text you expect.
  • Never use generated CSS module classes as the main selector; they are compiler output, not product contract.
  • Use .first() only when repetition is genuinely legitimate and document which instance matters.
  • Locator audits are cheap accessibility audits. If everything needs data-testid, your semantic HTML is probably thin.
  • When copy is localized, prefer roles, labels, and test IDs over exact language strings.

Was this lesson helpful?

Sign in to rate