feat(web): run a selector inside a locator with browser.locator(css, { within }) (ENG-1211)
What
browser.locator(selector, { within }) runs a CSS or XPath selector inside another locator, the way Playwright's within.locator(selector) does:
const row = screen.getByRole('row', { name: 'Grace' });
await expect(browser.locator('td.price', { within: row })).toHaveText('$9');
await browser.locator('button', { within: browser.frameLocator('#grid').getByRole('row', { name: 'Linus' }) }).click();
within is any locator from this test's screen or browser (filtered, positioned, or, or inside frameLocator), and the result stays in its frame. It is web only, as the ticket decided: the shared Locator gets no locator(), and frame.locator, device, and desktop take no within. A second argument that is not an options object now throws INVALID_LOCATOR, so ['td'].map(browser.locator), which passes the index, needs map((selector) => browser.locator(selector)); the changeset says so.
Two fixes ride along, each with its own changeset:
- An exact
getByLabel(...)with a position and then a filter (.first().filter({ hasText })) lost the filter when used as a scope, ahas, or now awithin. - In
e2e mcp'srun_code, ascreenlocator and abrowserlocator could not be combined (or,filter({ has }),dragTo,within):INVALID_LOCATOR.
Why
Before this, a selector could not be scoped to a row except by writing one >> chain or switching to a getBy* query, so Playwright, Cypress (.find), and Selenium (findElement) ports had to be rewritten by hand. Passing { within } on main type-errors, and in JavaScript it is silently ignored: the selector searches the whole page.
How it works
- The engine contract's
selectorexpression takes an optionalscope, the fieldqueryalready has (packages/core/src/engine/contract.ts:125). It already crosses the engine protocol as JSON, so the protocol does not change. EngineFixtureContext.expressionOf(api, locator)gives a fixture the expression behind a test's locator (packages/core/src/engine/index.ts:280,packages/core/src/run/fixtures.ts:530). It uses the ownership checkfilter({ has })andoruse, moved intoownedLocator(packages/core/src/locator/screen.ts:390), which now compares the engine session rather than the screen object.run_codebuilds its ownscreenover the attempt's session, so the old object check refused its locators next tobrowser's.browser.locatorchecks the options and mints{ kind: 'selector', selector, scope }(packages/web/src/browser.ts:866). The web engine projects it as Playwright'sscope.locator(selector)(packages/web/src/locators.ts:362), andframeSelectorsfollowsscopeso a framedwithinkeeps its frame (packages/web/src/locators.ts:427).- The same projection fixes the label bug: an exact label filtered after a position keeps the filter on its composable Playwright locator (
packages/web/src/locators.ts:340). - Other resolvers honor
scopeinstead of silently matching the whole screen: core'sresolveExpressionruns the selector hook once over the scope's descendants (packages/core/src/engine/resolve.ts:140), mobile's resolver does the same (packages/mobile/src/locate.ts:57), and desktop's query-kind guard walks into it (packages/desktop/src/surface.ts:187). Error messages readgetByRole("row", name: "Ada") >> locator("td.discount")(packages/core/src/locator/expression.ts:205). @e2e-dev/webnow needsexpressionOf, so its@e2e-dev/coreande2epeer floors rise to0.25.1, the next core release.- Docs: the
locatorentry inreference/web.mdx(semantics, throws, the closed shadow root limit),reference/engine.mdx(scope,expressionOf), one line inbrowser.mdx, and the Playwright, Cypress, and Selenium guides, wherelocator.locator,.find('css'), andfindElementmove from the gap tables into the command maps. The consumer skill nameswithin.
I checked the semantics against Playwright: matches come back once each in Playwright's order, scope match by scope match (with nested within matches that is not document order: :scope > .direct under nested articles returns the outer one's late before the inner one's early), a leading //, :scope, and xpath=.. start at the scope element, and a has locator runs from each candidate, so a scope that is the candidate itself matches nothing. scoped-selectors.test.ts runs every case through the engine and Playwright on one page. One limit Playwright has no answer for: Playwright cannot see into a closed shadow root at all, and our reader can, so li and xpath=./li work under a within found there, but a :scope combinator on the root's direct child matches nothing. The docs say so.
Overlap with open PRs: #1822 adds context.withNode(api, locator, use) beside expressionOf in the same files (expressionOf takes its argument order), a textual rebase. #1503 deletes locators.ts; after it, splitFrames and selectorParts need to follow scope, and the in-page resolver needs to run the selector under each scope match. scoped-selectors.test.ts is the check.
Verified
Ran it locally: yes
- testbed, built CLI, a scratch test (not committed):
withinon a row in the page and on a row in an iframe, thenfillandclickthrough it:
✓ |web| tests/within-video.e2e.ts > within scopes a selector to one row, in the page and in a frame 536ms
Tests 1 passed (1)
- main against this branch, the same scratch test through each build's CLI:
| Case | main | this PR |
|---|---|---|
browser.locator('td.price', { within: graceRow }) | within ignored: 2 matches, LOCATOR_AMBIGUOUS | 1 match, $9 |
within a row inside an iframe | searches the top page: LOCATOR_AMBIGUOUS | finds $3 in the frame |
| no match | locator matched no nodes within locator("td.discount") | ... within getByRole("row", name: "Ada") >> locator("td.discount") |
within: 'tr' (a string) | accepted silently | INVALID_LOCATOR: browser.locator({ within }) requires an e2e locator |
-
run_code,mcp-run-code.test.ts, the new case with the ownership check as on main:INVALID_LOCATOR: or requires a locator made by this screen(and the same forwithin); passes here. Thewithinline counts*under the list, 3, so it fails with 58 when the scope is dropped. -
apps/testbed/tests/locator-query.e2e.ts, a Playwright port, rewrotediv.first().locator('p')as agetByTextscope; it now usesbrowser.locator('p', { within: browser.locator('div').first() })and passes 19/19. -
Gemini 4 Neon reviewed the design and the diff twice. Taken: the
run_codebug, the label bug, the closed-root:scopelimit (documented and pinned), test gaps. Declined: a protocol capability flag forscope(only the web fixture mints one and its engine ships in the same package; the contract says an engine must honor it), and a runtime check thatframe.locatorgot no second argument (the types forbid it; no fixture method checks arity). -
pnpm bench:abon the web benchmark suite (no model calls): same steps and outcomes on both builds, no timing change.
bench:ab table
bench-ab: ea008c20 (merge base with origin/main) vs dba811ff
apps/web-benchmark: e2e run --workers 1. 6 pairs in random order, resolved after 6 pairs. Threshold ±5% and 10 ms. darwin arm64, Apple M4 x10, Node 26.13.1, load 4.5.
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) | 61.10 s (60.88 s-61.47 s) | 61.77 s (60.73 s-62.49 s) | +1.2% (-1.5% to +2.9%) | same |
| suite (sum of attempts) | 59.81 s (59.49 s-60.07 s) | 60.42 s (59.46 s-61.23 s) | +1.2% (-1.5% to +3.1%) | same |
| action (events) | 31.05 s (30.88 s-31.21 s) | 30.94 s (30.88 s-30.98 s) | +0.1% (-2.2% to +0.5%) | same |
| step screen.scrollUntilVisible | 17.04 s (17.02 s-17.48 s) | 17.02 s (17.01 s-17.04 s) | -0.2% (-4.1% to +0.7%) | same |
| step locator.tap | 9.67 s (9.57 s-9.68 s) | 9.70 s (9.61 s-9.75 s) | +0.6% (-0.9% to +2.3%) | same |
| step app.open | 6.80 s (6.76 s-6.90 s) | 6.74 s (6.70 s-6.86 s) | -1.2% (-1.7% to +0.8%) | same |
| step expect.toBeHidden | 6.54 s (6.53 s-6.54 s) | 6.53 s (6.53 s-6.54 s) | -0.0% (-0.1% to +1.0%) | same, under the floor |
| step expect.toBeExpanded | 2.61 s (2.61 s-2.61 s) | 2.61 s (2.61 s-2.62 s) | -0.0% (-1.9% to +0.1%) | same, under the floor |
| step expect.toHaveText | 2.45 s (2.42 s-2.52 s) | 2.39 s (2.33 s-2.43 s) | -3.7% (-9.5% to +4.1%) | unresolved |
| step expect.not.toContainText | 2.01 s (2.01 s-2.01 s) | 2.01 s (2.01 s-2.01 s) | -0.1% (-0.1% to +0.2%) | same, under the floor |
| step expect.not.toBeVisible | 2.01 s (2.01 s-2.01 s) | 2.01 s (2.01 s-2.01 s) | -0.0% (-0.1% to +0.1%) | same, under the floor |
| step locator.longPress | 1.53 s (1.53 s-1.54 s) | 1.53 s (1.53 s-1.53 s) | -0.1% (-0.6% to +0.2%) | same, under the floor |
| step expect.toContainText | 1.20 s (1.20 s-1.20 s) | 1.20 s (1.20 s-1.20 s) | +0.2% (-0.4% to +0.3%) | same, under the floor |
| step expect.not.toBeHidden | 1.01 s (1.01 s-1.01 s) | 1.01 s (1.01 s-1.01 s) | -0.1% (-0.3% to +0.0%) | same, under the floor |
| step locator.fill | 925 ms (911 ms-934 ms) | 937 ms (930 ms-948 ms) | +2.0% (-0.9% to +2.9%) | same |
| step locator.dragTo | 690 ms (682 ms-697 ms) | 676 ms (655 ms-710 ms) | -3.6% (-6.2% to +6.8%) | unresolved |
| step locator.check | 296 ms (293 ms-301 ms) | 290 ms (287 ms-295 ms) | -1.2% (-9.7% to +0.7%) | same, under the floor |
| step locator.pressSequentially | 290 ms (289 ms-297 ms) | 291 ms (288 ms-293 ms) | +0.2% (-2.7% to +1.9%) | same, under the floor |
| step expect.toBeVisible | 280 ms (275 ms-285 ms) | 283 ms (281 ms-285 ms) | +1.6% (-2.3% to +3.9%) | same, under the floor |
| step locator.hover | 200 ms (196 ms-203 ms) | 197 ms (193 ms-200 ms) | -1.5% (-10.7% to +3.4%) | same, under the floor |
| step screen.swipe | 162 ms (153 ms-172 ms) | 153 ms (148 ms-165 ms) | -4.4% (-19.6% to +12.1%) | same, under the floor |
| step expect.toHaveCount | 145 ms (142 ms-146 ms) | 139 ms (137 ms-145 ms) | -2.4% (-4.5% to +0.3%) | same, under the floor |
| step locator.secondaryTap | 131 ms (128 ms-137 ms) | 137 ms (128 ms-147 ms) | +3.6% (-7.6% to +12.2%) | same, under the floor |
| step locator.doubleTap | 126 ms (122 ms-145 ms) | 127 ms (124 ms-145 ms) | +2.5% (-12.1% to +14.7%) | same, under the floor |
| step browser.waitForDownload | 122 ms (118 ms-123 ms) | 118 ms (115 ms-121 ms) | -0.8% (-5.3% to +2.6%) | same, under the floor |
| step locator.setInputFiles | 102 ms (98 ms-108 ms) | 102 ms (98 ms-103 ms) | -2.6% (-6.0% to +2.1%) | same, under the floor |
| step locator.scrollIntoView | 101 ms (99 ms-108 ms) | 109 ms (105 ms-112 ms) | +4.5% (-10.4% to +16.8%) | same, under the floor |
| step locator.selectOption | 91 ms (89 ms-95 ms) | 90 ms (89 ms-91 ms) | -1.7% (-14.8% to +3.4%) | same, under the floor |
| step browser.waitForPopup | 66 ms (65 ms-67 ms) | 65 ms (64 ms-66 ms) | -1.5% (-18.3% to +2.4%) | same, under the floor |
| step expect.toHaveValue | 52 ms (51 ms-53 ms) | 51 ms (49 ms-51 ms) | -3.8% (-11.7% to +0.0%) | same, under the floor |
| step screen.tapAt | 45 ms (42 ms-50 ms) | 45 ms (41 ms-46 ms) | -7.5% (-14.3% to +11.9%) | same, under the floor |
| step expect.toHaveURL | 44 ms (43 ms-45 ms) | 42 ms (41 ms-43 ms) | -5.7% (-29.3% to +1.2%) | same, under the floor |
| step locator.uncheck | 43 ms (34 ms-47 ms) | 31 ms (31 ms-42 ms) | -14.0% (-34.0% to +18.6%) | same, under the floor |
| step browser.evaluate | 34 ms (33 ms-39 ms) | 32 ms (31 ms-32 ms) | -8.9% (-21.0% to +9.9%) | same, under the floor |
| step app.back | 17 ms (16 ms-17 ms) | 16 ms (15 ms-16 ms) | -6.3% (-19.9% to 0.0%) | same, under the floor |
| step browser.keyboard.press | 10 ms (8 ms-12 ms) | 9 ms (8 ms-10 ms) | +0.0% (-38.5% to +43.6%) | same, under the floor |
| step browser.keyboard.type | 9 ms (8 ms-10 ms) | 9 ms (8 ms-9 ms) | -5.1% (-19.1% to +18.6%) | same, under the floor |
| step locator.press | 8 ms (7 ms-8 ms) | 8 ms (7 ms-8 ms) | 0.0% (-11.8% to +19.5%) | same |
| step expect.toBeFocused | 7 ms (7 ms-8 ms) | 8 ms (6 ms-8 ms) | +6.9% (-34.5% to +22.5%) | same, under the floor |
| step expect.toBeDisabled | 6 ms (6 ms-6 ms) | 7 ms (6 ms-8 ms) | +15.5% (-29.3% to +52.8%) | same, under the floor |
| step expect.toHaveTitle | 3 ms (3 ms-3 ms) | 3 ms (2 ms-4 ms) | 0.0% (-33.3% to +41.4%) | same |
| step expect.toBeChecked | 2 ms (2 ms-2 ms) | 2 ms (1 ms-2 ms) | -29.3% (-59.2% to 0.0%) | same, under the floor |
| step expect.toBeEnabled | 2 ms (1 ms-2 ms) | 1 ms (1 ms-1 ms) | -29.3% (-50.0% to +41.4%) | same, under the floor |
| step tab.close | 1 ms (1 ms-2 ms) | 1 ms (1 ms-1 ms) | 0.0% (-50.0% to +100.0%) | same |
Counters
| Counter | base | head |
|---|---|---|
| events engine | 842-846 | 843-849 |
| events navigation | 12-13 | 12 |
Tests outside the threshold: 0 of 95
With 95 tests compared at 95%, about 4.8 could land outside by chance.
The docs change is entries and table rows on pages pnpm docs:check compiles; no rendered screenshot.
Written by Claude Opus 5.5 in Claude Code (T3 Code), reviewed by Gemini 4 Neon (Antigravity).
https://github.com/user-attachments/assets/09cc8d51-a38d-4970-9da3-b24a536fc4d5