TOPIC 40/6018 MINNOT STARTED

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.

  1. Try built-in
  2. Rename the id
  3. Teach the word
  4. Keep it small
Rename before you teach

HOW THE PICTURE MAPS

How each part of the picture matches the software
In the pictureIn the software
A new word for the robotA custom selector engine
The rule that goes with the wordThe query and queryAll methods
Teaching the word before the day beginsRegistering the engine before any page is created
Calling an existing thing by your family nameChanging 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.

What each part of the trick stands for
In the trickWhat it stands for
new worda custom selector engine
other namedata-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
  1. 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.

    Selectors API: register/query/queryAll, test ID attributes and contentScript. Documentation name vocabulary is narrower than inspected Playwright 1.65 registration/parser, which also accepts hyphens. Register early for predictable setup; inspected implementation propagates registration to existing contexts. · Checked 2026-11-06

    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.

  2. 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.

    Says role locators are the preferred choice because they match how users and assistive tools see the page, label locators suit form fields, text locators suit non-interactive elements, and test ids are the most resilient fallback when role or text cannot be used. Says the test id attribute is data-testid by default and can be changed with testIdAttribute. Says a matching locator that resolves to more than one element throws in strict mode for actions, and that first, last and nth are not recommended. Vendor documentation for one tool. Practitioner guidance, not a standard. · Checked 2026-11-06

    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.

  3. 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.

    Selectors API: register/query/queryAll, test ID attributes and contentScript. Documentation name vocabulary is narrower than inspected Playwright 1.65 registration/parser, which also accepts hyphens. Register early for predictable setup; inspected implementation propagates registration to existing contexts. · Checked 2026-11-06

    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.

  4. Market practice

    Best practices (Playwright documentation) ↗ — Microsoft (Playwright) · Prefer user-facing locators over CSS or XPath (search excerpt, page not opened)

    Recommends user-facing locators such as role-based ones over Cascading Style Sheets (CSS) or XPath, web-first assertions that retry, and avoiding fixed waits. Vendor documentation. Practitioner or vendor guidance, not a standard unless stated. · Checked 2026-10-18

    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).

  5. 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.

    register validation /^[a-zA-Z_0-9-]+$/ accepts hyphens; descriptive error text still omits them. · Checked 2026-11-06

    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.

  6. 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.

    parseSelectorString engine-name recognition accepts a hyphen. · Checked 2026-11-06

    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.

  7. 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.

    register propagates an engine registration to known contexts; early registration remains the documented predictable setup. · Checked 2026-11-06

    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
  1. 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.

    Selectors API: register/query/queryAll, test ID attributes and contentScript. Documentation name vocabulary is narrower than inspected Playwright 1.65 registration/parser, which also accepts hyphens. Register early for predictable setup; inspected implementation propagates registration to existing contexts. · Checked 2026-11-06

    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.

  2. 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.

    Says role locators are the preferred choice because they match how users and assistive tools see the page, label locators suit form fields, text locators suit non-interactive elements, and test ids are the most resilient fallback when role or text cannot be used. Says the test id attribute is data-testid by default and can be changed with testIdAttribute. Says a matching locator that resolves to more than one element throws in strict mode for actions, and that first, last and nth are not recommended. Vendor documentation for one tool. Practitioner guidance, not a standard. · Checked 2026-11-06

    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.

  3. 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.

    Selectors API: register/query/queryAll, test ID attributes and contentScript. Documentation name vocabulary is narrower than inspected Playwright 1.65 registration/parser, which also accepts hyphens. Register early for predictable setup; inspected implementation propagates registration to existing contexts. · Checked 2026-11-06

    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.

  4. Market practice

    Best practices (Playwright documentation) ↗ — Microsoft (Playwright) · Prefer user-facing locators over CSS or XPath (search excerpt, page not opened)

    Recommends user-facing locators such as role-based ones over Cascading Style Sheets (CSS) or XPath, web-first assertions that retry, and avoiding fixed waits. Vendor documentation. Practitioner or vendor guidance, not a standard unless stated. · Checked 2026-10-18

    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).

  5. 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.

    register validation /^[a-zA-Z_0-9-]+$/ accepts hyphens; descriptive error text still omits them. · Checked 2026-11-06

    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.

  6. 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.

    parseSelectorString engine-name recognition accepts a hyphen. · Checked 2026-11-06

    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.

  7. 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.

    register propagates an engine registration to known contexts; early registration remains the documented predictable setup. · Checked 2026-11-06

    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.