[feat]: add `locator.locator` chaining
SE
3d ago+4088+4088+0123456789012345678901234567890123456789-1789-1789-0123456789012345678901234567890123456789
why
- users need a way to combine selector types, & narrow queries down to specific parts of the DOM
what changed
- added locator chaining. eg:
const pay = page .locator({ type: "css", value: ".card" }) .nth(1) .locator({ type: "text", value: "Pay" }); await pay.click();pay = ( page.locator({"type": "css", "value": ".card"}) .nth(1) .locator({"type": "text", "value": "Pay"}) ) await pay.click() nth()now applies to the latest step of the chain. calling it again replaces that step's index, and earlier steps keep theirs.- strings and
{ type: "legacy" }still work. the SDK converts them to a chain before sending, with the same behavior as before (>>hops, deep XPath, etc.). a shared fixture (tests/fixtures/locator-normalization.json) runs the same cases through the TS and Python converters so they stay in sync.
protocol changes:
Locator.selectoris now aLocatorChain:{ steps: [...] }, where each step is either- a
querystep, which searches the current scope, or the previous matches' descendants (or the iframe's document when a match is an iframe), or - a
framestep, which requires the previous matches to be iframes and enters them. only legacy>>hops produce frame steps; there's no public API for them.
- a
- each step has its own optional
nth. the top-levelLocator.nthand thelegacywire variant are removed. - applies everywhere a locator goes on the wire, including act/observe/extract
locator/ignoreLocatorsand screenshotmask. the SDK APIs for these are unchanged.
extension changes:
- new chain resolver (
understudy/locatorChain.ts,selectorTarget.ts). the CSS, XPath and text engines can now search within a scope element, including shadow roots. - removed the extension's legacy-string parsing (
deepLocator,focusSelectors, snapshot capture), now that the SDKs do it.
behavioural notes:
- if a
locatormatches an iframe, the next query searches inside the iframe's document
test plan
- protocol: chain schema + type tests (
locator-chain.test.ts,.test-d.ts), wire-casing, object model - SDK normalization: shared legacy → chain fixture run in TS (
browser-api/tests/locatorSelector.test.ts) and Python (test_locator_normalization.py) - extension: chain resolver unit tests, act/observe/extract/cache, screenshot masks
- TS smoke tests against a real extension: scoped chains + intermediate indices, same-origin and cross-origin iframes, masking every match, chain scopes in inference snapshots, normalizing legacy strings at the boundary
Summary by cubic
Adds locator.locator() in TypeScript and Python so queries can be chained and narrowed to specific parts of the DOM, with each subsequent query resolving inside an iframe document when the previous match is an iframe.
Protocol changes
Locator.selectorbecomes aLocatorChainwithsteps; the top-levelLocator.nthand the{ type: "legacy" }wire variant are removed, and each step carries its own optionalnth.- Legacy string selectors (
>>hops, deep XPath, engine prefixes) are normalized to chains in the SDKs, which share a fixture to stay in sync; the extension's own legacy parsing is removed. - Malformed XPath now fails with an
Invalid XPatherror everywhere instead of being silently truncated to a partial selector that could match the wrong element, and resolution falls back to the frame scope when an iframe match can't be resolved.
Behavior notes
nth()applies to the latest step only; calling it again replaces that step's index.- A step matching an iframe scopes the next query to that iframe's document.
- Act/observe/extract
locator,ignoreLocators, and screenshotmasktake the chain without changes to their SDK APIs.