Repository navigation
Conversation
|
|
This PR is from an external contributor and must be approved by a stagehand team member with write access before CI can run. |
`Locator.nth()` spread the current descriptor and then overwrote `nth`,
so narrowing an already narrowed locator retargeted the whole match list
instead of the set it matched. `page.locator("li").nth(2).first()` sent
`nth: 0` and acted on the first `li` on the page, and
`page.locator("li").nth(2).nth(1)` acted on the second `li`. A helper
that takes a locator and calls `first()` to be explicit could therefore
click a different element than the one the caller picked.
`first()` of a one-element set is that element, so it now returns the
locator unchanged when it is already narrowed, and `nth(0)` does the
same. The wire descriptor carries a single index and cannot express
"matches nothing", so a further narrowing to another index is refused
rather than silently acting on a different element.
Replayed onto `packages/shared/browser-api/` after browserbase#3372 moved the shared
client code out of `packages/sdk-ts/`; the test moves with it.
d541382 to
4488de1
Compare
|
Rebased onto current Local check (isolated harness, real |
What is wrong
A
Locatoris described by one wire index,nth(schema), and both narrowing helpers write over it:So a locator that is already narrowed loses the element it was narrowed to:
In Playwright both calls narrow the elements the locator already matches, so
nth(2).first()stays on the thirdli. Here a helper that receives an already narrowed locator and calls.first()silently acts on a different element, with no error — the worst shape for a bug in a browser driver.What changed
packages/sdk-ts/src/locator.ts, two guards, no protocol change:first()returns the locator unchanged whendescriptor.nthis set. A narrowed locator matches one element, andfirst()of a one-element set is that element.nth(0)on a narrowed locator also returns it unchanged, for the same reason.nth(k)withk > 0on a narrowed locator throws aRangeErrornaming the index it already carries. Such a set matches nothing, andLocatorDescriptorcarries a singlenth, so "matches nothing" cannot be expressed on the wire. The choice here is between refusing and silently acting on another element; this PR refuses.Nothing else moved: an unnarrowed locator still narrows exactly as before, and the index is still validated by
LocatorDescriptorSchemawhenever it is written.One question for the maintainers: if you would rather have Playwright's "matches nothing" than a throw, that needs the descriptor to express an empty match (a second index, or an explicit
none), which spanspackages/protocoland both SDKs. Say which you prefer and I will follow it — thefirst()half is independent either way.Test evidence
New
packages/sdk-ts/tests/locatorNarrowing.test.ts, in the style of the existingclientLocatorOptions.test.ts(aPageover a command client that throws on any RPC, so only descriptor construction is exercised). Six cases:first()on an unnarrowed locator,first()onnth(2),nth(0)onnth(2),nth(1)onnth(2)refusing,nth(3)on an unnarrowed locator, and the schema still rejecting a negative index.Run against the SDK's
Locatorclass in an isolated Vitest harness (Vitest 5.0.5, Node 22): the file imports the reallocator.tsverbatim, with only its import specifiers pointed at local copies ofLocatorDescriptorSchema/StagehandMethods/ the type module.main(9bb60e2)expected 0 to be 2,expected 1 to be 2, and "expected function to throw")locator.ts, keeping the testoxfmt --checkis clean on both files.Not verified
vitest run --root ../.. packages/sdk-ts/testshere (the workspace needs a fullpnpm install), so the new test file has not been executed by the repo's runner, andtsc --noEmit -p tsconfig.jsonhas not been run on it. It mirrors the existing test's imports andPageconstruction exactly for that reason.nth: 2against the full match list is unchanged by this PR.locator.py, same singlenth) and the wire schema are untouched; anth(2).first()chain through the Python SDK still replaces the index.Summary by cubic
Fixes locator narrowing so
first()andnth()compose on an already narrowed locator instead of overwriting its index. Previouslypage.locator("li").nth(2).first()sentnth: 0and acted on the firstli; now it keeps the third one.first()andnth(0)on a narrowed locator return it unchanged.nth(k)withk > 0on a narrowed locator throws aRangeErrorinstead of silently retargeting another element, since the wire descriptor cannot express an empty match.LocatorDescriptorSchemawhenever it is written.Written for commit 4488de1. Summary will update on new commits.