Custom selector engines and test id attributes
Change which attribute getByTestId reads, and learn how a custom selector engine is built and registered, and why it is the last tool in the box.
L1–L4 are part of the Pro pass
L0 Explain simply
Analogy: teaching the robot a new word for finding things. A helper robot knows words such as door, window and lamp. You ask for a lamp and it brings one. One day you need the blue lamp your aunt gave you. You teach the robot a new word, aunt-lamp, and the rule that goes with it.
Tom tests Daily List, a to-do list app. Playwright already finds things by role, by label and by text. Most of the time those words are enough.
The picture has limits. A robot needs one word for a lamp. A test needs the new word to keep working on every page. A home-made word is also code that someone must maintain.
What it is: Playwright is a free library from Microsoft that drives a real browser from code. This topic is one slice of that library: changing how a test finds an element, and the rare case of teaching a new finder. getByTestId reads the attribute data-testid unless you set testIdAttribute. A is a small finder you write, with query and queryAll. is selectors.register, and it must happen before any page is created. Content script mode is { contentScript: true }. It runs the engine apart from the page's own scripts, so the page cannot rewrite built-in objects such as Node.prototype. The nearest neighbour is getByRole. Role locators match what a person meets. A custom engine is the last tool, after role, label, text and a test id.
Why it matters: Leo's team already marks elements with data-qa. If Tom asks them to rename every attribute, the app change is larger than the test change. Setting testIdAttribute: 'data-qa' keeps getByTestId('add-button') working. A custom engine is rarer. It costs a new word the whole team must learn. Tom writes one only when role, label, text, test id and CSS (Cascading Style Sheets) all fail.
(SYNTHETIC / TRAINING ONLY: Daily List and every person named in this lesson are invented.)
PLAIN WORDS · 2 in this section
- playwright
- a free Microsoft tool that drives a browser for tests
- rename
- give a new name to
PICTURE IT
The picture is the one the lesson above opens with. The table below shows how each part of it matches the software.
THE REAL JOB Telling Playwright another attribute name for test ids, or teaching it a new way of finding elements.
- Try built-in
- Rename the id
- Teach the word
- Keep it small
HOW THE PICTURE MAPS
| In the picture | In the software |
|---|---|
| A new word for the robot | A custom selector engine |
| The rule that goes with the word | The query and queryAll methods |
| Teaching the word before the day begins | Registering the engine before any page is created |
| Calling an existing thing by your family name | Changing the test id attribute |
WHERE THIS PICTURE STOPS
- A robot learns a word once, but a test must register the engine in every run.
- A word spoken at home may not work at a friend's house, and an engine works only in the project that registers it.
- The robot does not mind if the word is odd, but a team does mind an odd word.
HOOK
Rename first, teach last.
REMEMBER IT THIS WAY
A contrast: two neighbours told apart by one test.
Two small jobs call for a custom engine: a new word for finding things, or another name for the . Rename first, teach last.
| In the trick | What it stands for |
|---|---|
| new word | a custom selector engine |
| other name | data-qa instead of data-testid |
An odd word is a cost for the whole team, so use the built-in ways when they are enough.
TELL A FRIEND
Changing the is a small rename. A is a new word for the robot, used last.
OFTEN MIXED UP WITH Locators: finding an element with CSS selectors and XPath. The earlier lesson ranks ids, Cascading Style Sheets (CSS) and XPath. This lesson shows how to extend the finders when none of them fit.
Sources & standards — 7 sources
- Market practice
Selectors class reference (Playwright documentation) ↗ — Microsoft (Playwright) · Reviewed 6 November 2026: register and setTestIdAttribute. See v45 source analysis for version scope and documented source ambiguities; no upstream run claimed.
Primary source opened and operative text inspected on 6 November 2026. Scope: register and setTestIdAttribute. Current live Playwright documentation reflects 1.65 where stated; explicit older examples retain their version. Source inspection is not an execution or compatibility-test claim. See the completed v45 Playwright analysis for source discrepancies and exact reading boundaries.
- Market practice
Locators (Playwright documentation) ↗ — Microsoft (Playwright) · Reviewed 6 November 2026: Complete extracted primary article; examples inspected, not executed. See v45 source analysis for version scope and documented source ambiguities; no upstream run claimed.
Primary source opened and operative text inspected on 6 November 2026. Scope: Complete extracted primary article; examples inspected, not executed. Current live Playwright documentation reflects 1.65 where stated; explicit older examples retain their version. Source inspection is not an execution or compatibility-test claim. See the completed v45 Playwright analysis for source discrepancies and exact reading boundaries.
- Market practice
Selectors class reference (Playwright documentation) ↗ — Microsoft (Playwright) · Reviewed 6 November 2026: register and setTestIdAttribute. See v45 source analysis for version scope and documented source ambiguities; no upstream run claimed.
Primary source opened and operative text inspected on 6 November 2026. Scope: register and setTestIdAttribute. Current live Playwright documentation reflects 1.65 where stated; explicit older examples retain their version. Source inspection is not an execution or compatibility-test claim. See the completed v45 Playwright analysis for source discrepancies and exact reading boundaries.
- Market practice
Best practices (Playwright documentation) ↗ — Microsoft (Playwright) · Prefer user-facing locators over CSS or XPath (search excerpt, page not opened)
Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail. A second search on 2026-10-20 returned this page for its title and matched the described content; the page was still not opened (search excerpt, page not opened).
- Market practice
Playwright 1.65.0 selector registration implementation ↗ — Microsoft (Playwright) · Playwright v1.65.0 pinned source: register validation /^[a-zA-Z_0-9-]+$/ accepts hyphens; descriptive error text still omits them. Inspected 6 November 2026; source contract, not an upstream execution claim.
Actual pinned source section inspected during the completed v45 analysis. Framework source is Apache-2.0, distinct from the documentation CC BY 4.0 licence. Upstream tests were not executed.
- Market practice
Playwright 1.65.0 selector parser ↗ — Microsoft (Playwright) · Playwright v1.65.0 pinned source: parseSelectorString engine-name recognition accepts a hyphen. Inspected 6 November 2026; source contract, not an upstream execution claim.
Actual pinned source section inspected during the completed v45 analysis. Framework source is Apache-2.0, distinct from the documentation CC BY 4.0 licence. Upstream tests were not executed.
- Market practice
Playwright 1.65.0 selector client ↗ — Microsoft (Playwright) · Playwright v1.65.0 pinned source: register propagates an engine registration to known contexts; early registration remains the documented predictable setup. Inspected 6 November 2026; source contract, not an upstream execution claim.
Actual pinned source section inspected during the completed v45 analysis. Framework source is Apache-2.0, distinct from the documentation CC BY 4.0 licence. Upstream tests were not executed.
PRACTISE AND KEEP
Sources for this topic — 7 sources
- Market practice
Selectors class reference (Playwright documentation) ↗ — Microsoft (Playwright) · Reviewed 6 November 2026: register and setTestIdAttribute. See v45 source analysis for version scope and documented source ambiguities; no upstream run claimed.
Primary source opened and operative text inspected on 6 November 2026. Scope: register and setTestIdAttribute. Current live Playwright documentation reflects 1.65 where stated; explicit older examples retain their version. Source inspection is not an execution or compatibility-test claim. See the completed v45 Playwright analysis for source discrepancies and exact reading boundaries.
- Market practice
Locators (Playwright documentation) ↗ — Microsoft (Playwright) · Reviewed 6 November 2026: Complete extracted primary article; examples inspected, not executed. See v45 source analysis for version scope and documented source ambiguities; no upstream run claimed.
Primary source opened and operative text inspected on 6 November 2026. Scope: Complete extracted primary article; examples inspected, not executed. Current live Playwright documentation reflects 1.65 where stated; explicit older examples retain their version. Source inspection is not an execution or compatibility-test claim. See the completed v45 Playwright analysis for source discrepancies and exact reading boundaries.
- Market practice
Selectors class reference (Playwright documentation) ↗ — Microsoft (Playwright) · Reviewed 6 November 2026: register and setTestIdAttribute. See v45 source analysis for version scope and documented source ambiguities; no upstream run claimed.
Primary source opened and operative text inspected on 6 November 2026. Scope: register and setTestIdAttribute. Current live Playwright documentation reflects 1.65 where stated; explicit older examples retain their version. Source inspection is not an execution or compatibility-test claim. See the completed v45 Playwright analysis for source discrepancies and exact reading boundaries.
- Market practice
Best practices (Playwright documentation) ↗ — Microsoft (Playwright) · Prefer user-facing locators over CSS or XPath (search excerpt, page not opened)
Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail. A second search on 2026-10-20 returned this page for its title and matched the described content; the page was still not opened (search excerpt, page not opened).
- Market practice
Playwright 1.65.0 selector registration implementation ↗ — Microsoft (Playwright) · Playwright v1.65.0 pinned source: register validation /^[a-zA-Z_0-9-]+$/ accepts hyphens; descriptive error text still omits them. Inspected 6 November 2026; source contract, not an upstream execution claim.
Actual pinned source section inspected during the completed v45 analysis. Framework source is Apache-2.0, distinct from the documentation CC BY 4.0 licence. Upstream tests were not executed.
- Market practice
Playwright 1.65.0 selector parser ↗ — Microsoft (Playwright) · Playwright v1.65.0 pinned source: parseSelectorString engine-name recognition accepts a hyphen. Inspected 6 November 2026; source contract, not an upstream execution claim.
Actual pinned source section inspected during the completed v45 analysis. Framework source is Apache-2.0, distinct from the documentation CC BY 4.0 licence. Upstream tests were not executed.
- Market practice
Playwright 1.65.0 selector client ↗ — Microsoft (Playwright) · Playwright v1.65.0 pinned source: register propagates an engine registration to known contexts; early registration remains the documented predictable setup. Inspected 6 November 2026; source contract, not an upstream execution claim.
Actual pinned source section inspected during the completed v45 analysis. Framework source is Apache-2.0, distinct from the documentation CC BY 4.0 licence. Upstream tests were not executed.
Deepest material on this page: L4 — Standards & sources. Where a topic stops short of implementation depth, that is a deliberate coverage decision, not an oversight — see coverage.