feat(expect): add toBeEmpty locator matcher (ENG-1229)
What
expect(locator).toBeEmpty() waits for a node to be empty, with Playwright's rules on the web.
await expect(screen.getByLabel('Search')).toBeEmpty();
await expect(screen.getByTestId('cart')).not.toBeEmpty();
- Web: an
inputortextareais empty when its value is'', read as it is, so a space is not empty. Any other element is empty when its trimmedtextContentis'', hidden text included. - Mobile: a text field is empty when it has no value, so its label or hint doesn't count. Any other node is empty when nothing in its subtree shows text or a value.
- Desktop reports no empty state yet, so
toBeEmptyfails either way there, with a message that says so.
It takes only { timeout }, works with .not and a custom message, and is POLICY_DENIED on a secure field, like toHaveValue.
Takes over #1869 by @ftaakash. Their commit is the first one here, with them as author.
Why
toBeEmpty is on the Playwright parity list (ENG-1229). #1869 decided value or text by role in core, because a semantic node has no tag. That made it pass on six elements that show text: an ARIA combobox, an ARIA listbox, a non-editable ARIA textbox, a native select, a submit input and a checkbox. Playwright decides by tag, so the engine has to answer. toBeEditable (#1962) took the same route for read-only.
How it works
- The engine contract gets an
emptynode state (packages/core/src/engine/contract.ts:201). Engines report it on the nodeslocatereturns, never in an observation and never on a secure field, so agent prompts, replay-cache anchors and the mobile golden trees don't change. - The web reader computes it in the page with Playwright's rule (
emptyOf,packages/web/src/in-page/read-semantics.ts:1699), only in the single-node projection that locator reads use (:1841). It checks the tag name, notinstanceof, so a filled input moved in from another frame still reads by its value. - The mobile engine adds it to each located node (
withEmptyState,packages/mobile/src/nodes.ts:129, called frompackages/mobile/src/surface.ts:1299). Fields go by role (textbox,searchbox) rather thanmayTakeText, since an iOSotherholding only text is as often a container as a drawn field. Below a container, a field counts only by its value and a secure field counts as nothing. toBeEmptyreads only that state (packages/e2e/src/expect/async.ts:482), the waytoBeEditablereadsreadonly. A node with no state keeps the poll waiting and fails withobserved: no empty state, which this engine does not report.- State-matcher failure messages and MCP
locatelines leaveemptyout (async.ts:689,packages/e2e/src/session-tools/catalog.ts:289), sotoBeCheckednever printsstates: empty. - Docs: the
expectand engine references, the Playwright and Cypress migration rows, the skill, and the ja/zhtoBeEmptysections. The peer floors already require>=0.25.0, the unreleased version this ships in.
Not done: desktop. It's a macOS-only prototype, and #1962 also left it without readonly. iOS reports a field drawn by Flutter or Compose Multiplatform as a plain view, so its placeholder counts and an empty one never reads empty. The reference page documents this.
Issue
Part of #1855. Replaces #1869.
Verified
Ran it locally: yes (Linux, Chromium)
Testbed, the built CLI against a scratch test (not committed): Playwright's cases, the six elements from #1869's review table, a fill then clear, a custom-message failure and a password field.
✓ |web| tests/zz-to-be-empty.e2e.ts > toBeEmpty reads fields by value and anything else by text content 9.55s
× |web| tests/zz-to-be-empty.e2e.ts > a failing toBeEmpty says what it read
× |web| tests/zz-to-be-empty.e2e.ts > toBeEmpty on a password field is denied
ASSERTION_FAILED: the fruit picker is cleared: expect.toBeEmpty failed
locator: getByTestId("combobox")
expected: empty
observed: not empty (match count 1)
POLICY_DENIED: reading values from a secure field is denied: getByLabel("Password")
The two failures are the intended ones. The password value appears in no file under .e2e/.
| Element | #1869 head (f65722e13) | this PR |
|---|---|---|
<button role="combobox">Select a fruit</button> | empty | not empty |
<ul role="listbox"><li role="option">Apple</li></ul> | empty | not empty |
<div role="textbox">hello</div> | empty | not empty |
<select> with options | empty | not empty |
<input type="submit" value="Go"> | empty | not empty |
<input type="checkbox"> | empty | not empty |
The left column is what #1869's own web-platform.test.ts asserted at that head. Each case now passes not.toBeEmpty() in web-platform.test.ts:124.
- Mutants: reading rendered text instead of
textContentfails the hidden-text case;instanceofinstead of the tag name fails the adopted-input case. The mobile unit test has a container holding only a labeled empty field and a filled password field; the old subtree rule read it as not empty. - Testbed default suite: 607 passed. An earlier run hit the
locator-frame.e2e.ts"should wait for frame to go" flake, which #1869's CI also hit on a head that didn't touch the web engine. - Mobile on a device: not run. No simulator on this machine. Covered by the engine and node unit tests on fixture Android and iOS snapshots.
pnpm bench:ab --suite testbed -- tests/forms.e2e.ts, a locator-read-heavy suite. Every located node on the web now also computes its empty state:
bench-ab: 425cdb28 (merge base with origin/main) vs d5ed00b1 (this branch before its rebase onto main, same code)
apps/testbed: e2e run --workers 1 tests/forms.e2e.ts. 6 pairs in random order, resolved after 6 pairs. Threshold ±5% and 10 ms. linux x64, AMD Ryzen 7 8845HS w/ Radeon 780M Graphics x16, Node 24.18.0, load 1.1.
Behavior
Every test ran the same steps with the same outcome on every run of both builds.
Timings
| Metric | base median (IQR) | head median (IQR) | change (95% CI) | verdict |
|---|---|---|---|---|
| wall (process) | 3.15 s (3.14 s-3.15 s) | 3.15 s (3.15 s-3.16 s) | -0.0% (-0.2% to +0.6%) | same, under the floor |
| suite (sum of attempts) | 2.10 s (2.10 s-2.11 s) | 2.11 s (2.11 s-2.11 s) | +0.3% (-0.3% to +0.4%) | same, under the floor |
| action (events) | 529 ms (527 ms-534 ms) | 528 ms (526 ms-530 ms) | -0.2% (-1.7% to +1.4%) | same, under the floor |
| step expect.not.toBeChecked | 1.01 s (1.01 s-1.01 s) | 1.01 s (1.01 s-1.01 s) | -0.1% (-0.1% to +0.3%) | same, under the floor |
| step locator.fill | 276 ms (274 ms-277 ms) | 278 ms (275 ms-278 ms) | -0.2% (-1.6% to +2.2%) | same, under the floor |
| step app.open | 190 ms (188 ms-193 ms) | 192 ms (189 ms-192 ms) | -0.0% (-2.6% to +2.7%) | same, under the floor |
| step locator.tap | 93 ms (91 ms-95 ms) | 90 ms (88 ms-91 ms) | -1.6% (-8.5% to +1.6%) | same, under the floor |
| step locator.pressSequentially | 62 ms (61 ms-62 ms) | 61 ms (60 ms-63 ms) | -1.6% (-4.0% to +4.9%) | same, under the floor |
| step locator.uncheck | 49 ms (47 ms-52 ms) | 49 ms (47 ms-51 ms) | -4.9% (-9.1% to +10.6%) | same, under the floor |
| step locator.check | 41 ms (38 ms-45 ms) | 45 ms (42 ms-45 ms) | +8.6% (-15.7% to +25.5%) | same, under the floor |
| step expect.toBeVisible | 31 ms (30 ms-32 ms) | 32 ms (31 ms-32 ms) | +3.3% (-1.5% to +6.8%) | same, under the floor |
| step locator.selectOption | 30 ms (29 ms-30 ms) | 30 ms (29 ms-31 ms) | +1.7% (-1.7% to +3.6%) | same, under the floor |
| step locator.clear | 30 ms (29 ms-31 ms) | 29 ms (29 ms-30 ms) | -3.3% (-6.5% to +3.4%) | same, under the floor |
| step expect.toHaveValue | 13 ms (13 ms-13 ms) | 15 ms (15 ms-16 ms) | +15.4% (+3.5% to +28.1%) | same, under the floor |
| step expect.toHaveCount | 10 ms (9 ms-10 ms) | 10 ms (9 ms-11 ms) | 0.0% (-9.5% to +16.0%) | same |
| step expect.toHaveText | 8 ms (7 ms-8 ms) | 6 ms (5 ms-6 ms) | -24.4% (-37.5% to -7.4%) | same, under the floor |
| step expect.toBeChecked | 3 ms (3 ms-4 ms) | 4 ms (3 ms-4 ms) | 0.0% (-22.5% to +33.3%) | same |
Counters
Every counter matched on every run of both builds.
Tests outside the threshold: 0 of 608
With 608 tests compared at 95%, about 30.4 could land outside by chance.
Taken over and finished by Claude Code (Opus 5.5).
https://github.com/user-attachments/assets/07a4a79d-35db-4232-a224-6487072c75bd