# Frontend `data-testid` Conventions > **When to use**: When authoring or reviewing UI components, or whenever a test/AI agent needs a durable handle on an element. > **Audience**: frontend engineers writing the app (not test authors). > **Companion**: [examples.md](examples.md) for framework code; [../core/locators.md](../core/locators.md) for the consumer side. A `data-testid` is a **public API for your UI's automation layer**. Once a test or an agent depends on `data-testid="login-submit"`, renaming it is a breaking change. Treat these attributes with the same care as a function signature: deliberate, named well, unique, and stable across refactors. ## TL;DR 1. **Semantic HTML first.** Before adding a test ID, give the element a real role, label, or accessible name. An accessible button is locatable *and* usable by screen readers. (See [Locator priority](#locator-priority-build-this-in-order).) 2. **Add `data-testid` only where role, label and text fall short** — custom components with no semantic role, scope containers, list rows, and dynamic, localized or async regions. A control `getByRole` already finds uniquely needs none. (See [When to add](#when-to-add-a-data-testid).) 3. **Name for global uniqueness**: `kebab-case`, namespaced by feature/component: `feature-component-element[-variant]`. (See [Naming convention](#naming-convention).) 4. **In lists, suffix with a stable domain ID — never the array index.** `user-row-${user.id}` ✅ / `user-row-${index}` ❌. (See [Uniqueness in lists](#uniqueness-in-lists-and-loops).) 5. **Never tie the value to text, locale, copy, styling, or DOM position.** (See [Stability rules](#stability-rules).) 6. The consumer (Playwright) reads these via `getByTestId()`, configured by `testIdAttribute` (default `data-testid`). (See [Consumer config](#how-the-consumer-uses-them).) --- ## Locator priority (build this in order) Locate elements in this order (most → least preferred), the same order [core/locator-strategy.md](../core/locator-strategy.md) uses. Playwright itself recommends role locators first and CSS/XPath last. As the **author of the markup**, your goal is to make the *higher* tiers possible, and only fall to `data-testid` when they genuinely don't fit. | Priority | Strategy | What you provide in markup | Notes | |---|---|---|---| | 1 | **Role + accessible name** | semantic element (` ))} ``` Now an agent can target one exact row: `getByTestId('user-row-42-delete-button')`, or scope into it: `getByTestId('user-row-42').getByRole('button', { name: 'Delete' })`. - Use the **business/entity key** (`user.id`, `product.sku`, `order.number`, `slug`). It must be stable and unique within the page. - If the data genuinely has no stable key, the ID on the *container* plus a semantic locator inside (`.filter({ hasText })`) is better than an index-based test ID. - **Never** use `Math.random()` or a render-time counter — the value must be deterministic across renders and across test runs. ### Selecting the whole collection Unique row IDs leave no shared handle for "all rows", for example to assert a count. A prefix does not work either: `/^user-row-/` also matches `user-row-42-name` and `user-row-42-delete-button`. Use one of these instead of a second, non-unique attribute (such as `data-testclass`), which `getByTestId()` cannot read and would push tests back to CSS selectors: ```ts // Container test ID + role inside it (preferred) await expect(page.getByTestId('users-table').getByRole('row')).toHaveCount(5); // Or an anchored pattern that matches row IDs only await expect(page.getByTestId(/^user-row-\d+$/)).toHaveCount(5); ``` --- ## Stability rules A `data-testid` earns its keep only if it **does not change for the wrong reasons**. Decouple the value from everything volatile: - **Independent of copy/text** — `data-testid` stays `submit-button` whether the label says "Submit", "Save", or "Guardar". - **Independent of locale/i18n** — never interpolate translated strings into the value. - **Independent of styling** — no color, size, or CSS-class names in the value. - **Independent of DOM position** — no "first", "second", "top"; use a domain key instead. - **Stable across refactors** — moving a component or changing its tag should not change its test ID. This is the whole point: it's the anchor that *survives* refactors. - **Deterministic** — same inputs → same value, every render, every environment. No randomness, no timestamps. - **One element, one ID** — don't reuse the same `data-testid` on multiple elements (breaks strict-mode locators). Uniqueness comes from the namespacing above plus the entity key in lists. --- ## How the consumer uses them Test authors / AI agents locate your elements via Playwright's `getByTestId()`, which reads the attribute named by `testIdAttribute` (default `data-testid`): ```ts // playwright.config.ts — only change this if your team standardizes on // a different attribute (e.g. data-test, data-qa, data-pw). import { defineConfig } from '@playwright/test'; export default defineConfig({ use: { testIdAttribute: 'data-testid' }, // default }); ``` ```ts // Consuming your markup: await page.getByTestId('login-form-email-input').fill('user@example.com'); await page.getByTestId('login-form-submit-button').click(); // Scoping into a container you exposed: const row = page.getByTestId('user-row-42'); await row.getByRole('button', { name: 'Delete' }).click(); ``` **Agreement to make with your test team:** pick **one** attribute name (`data-testid` is the default and recommended) and use it everywhere. Mixing `data-test`, `data-qa`, and `data-testid` defeats `getByTestId` and forces brittle CSS. > **Production stripping (optional):** if you don't want test IDs shipped to production, strip them at build time with a Babel/SWC plugin (e.g. `babel-plugin-react-remove-properties` with `{ properties: ['data-testid'] }`) — but **keep them in every environment your tests/agents run against** (dev, CI, staging). Stripping in the environment under test removes the locators you just built. See [examples.md](examples.md#production-stripping-optional). --- ## Review checklist Use this when writing or reviewing a component: - [ ] Interactive elements use **semantic HTML / ARIA** first (so `getByRole`/`getByLabel` work). - [ ] `data-testid` added only where role/label/text fall short: no-role custom widgets, scope containers, list rows, dynamic or localized regions. - [ ] No `data-testid` on purely decorative/layout nodes. - [ ] Every value follows `feature-component-element[-variant]` in `kebab-case`. - [ ] List/loop IDs use a **stable domain key**, not the array index. - [ ] No value derives from text, locale, CSS, color, or DOM position. - [ ] No duplicate `data-testid` on the page (one element ↔ one ID). - [ ] Test-id strings are centralized/shared with the test suite where practical. - [ ] Attribute name matches the team's `testIdAttribute` (default `data-testid`). --- ## Anti-patterns | Don't | Why it breaks | Do instead | |---|---|---| | `data-testid="btn"` on 12 buttons | Not unique → strict-mode violations / wrong match | Namespace: `checkout-pay-button`, `cart-clear-button` | | ``data-testid={`row-${index}`}`` | Reorders/paginates → IDs shift | `row-${item.id}` | | `data-testid="blue-large-cta"` | Tied to styling → renamed on redesign | `signup-cta-button` | | `data-testid={t('submit')}` | Tied to locale → breaks per language | `submit-button` (constant) | | `
` | Skips accessibility; not keyboard-usable | `