Skip to content

fix(extension): support Playwright-style negative nth() indices - #3417

Open
brennanbutler01 wants to merge 1 commit into
browserbase:mainfrom
brennanbutler01:fix/locator-nth-negative
Open

brennanbutler01 wants to merge 1 commit into
browserbase:mainfrom
brennanbutler01:fix/locator-nth-negative

Conversation

@brennanbutler01

@brennanbutler01 brennanbutler01 commented Nov 9, 2026 •

Copy link
Copy Markdown

why

locator().nth(-1) and deepLocator().nth(-1) threw RangeError, while Playwright's contract is that negative indices count from the end of the match list (nth(-1) selects the last match). The -1 sentinel additionally carried three different meanings across the locator classes (see #3415): "no narrowing" in Locator, "preserve all matches" in DeepLocatorDelegate, and a hard error from the public nth() methods.

Closes #3415

what changed

  • Locator now tracks explicit narrowing separately from the -1 sentinel (hasExplicitNth), so an explicit nth(-1) no longer collides with the internal default.
  • Locator.nth() accepts finite negative indices; resolveNode() and resolveNodesForMask() resolve an explicit negative index against the match count (count, then index from the end) via resolveActionIndex().
  • DeepLocatorDelegate.nth() accepts finite negative indices and threads the explicit flag through to the underlying Locator; the default delegate keeps its narrowing-free masking semantics.
  • Non-negative nth() behavior and the not-found error path (e.g. nth(-5) on 3 matches → "Could not find an element") are unchanged.

test plan

  • New packages/extension/tests/locator-nth.test.ts covers: nth(-1) no longer throws on either class, explicit-flag/sentinel separation, unchanged non-negative semantics, RangeError still thrown for non-finite indices, and delegate identity-stability for repeated identical nth() calls.
  • Existing suites pass: pnpm --filter ./packages/extension test:unit → 74 files / 1196 tests passing (incl. the 8 new tests), extension typecheck clean, root pnpm typecheck clean, pnpm lint clean (pre-existing warnings in unrelated packages only).
  • Manual reasoning for the end-to-end path: nth(-1) → resolveActionIndex() → count() + resolveAtIndex(total - 1); the same selector with a non-negative index hits the unchanged resolveAtIndex fast path.

Summary by cubic

Locator.nth() and DeepLocatorDelegate.nth() now accept negative indices, counting from the end of the match list like Playwright. nth(-1) selects the last match instead of throwing a RangeError. Explicit narrowing is tracked separately from the internal -1 sentinel, so default behavior and explicit nth(-1) don't collide.

Changes

  • Negative indices are resolved against the match count at resolution time.
  • Non-finite indices still throw RangeError.
  • Non-negative behavior and not-found error paths are unchanged.

Written for commit 4581d11. Summary will update on new commits.

View guided diff

locator().nth(-1) and deepLocator().nth(-1) threw RangeError, while
Playwright's contract is that negative indices count from the end of
the match list. The -1 sentinel additionally carried three different
meanings: 'no narrowing' in Locator, 'preserve all matches' in
DeepLocatorDelegate, and a hard error from the public nth() methods.

Track explicit narrowing separately from the sentinel and resolve
explicit negative indices against the match count at resolution time
(Closes browserbase#3415)

Amp-Thread-ID: https://ampcode.com/threads/T-01a1bd90-aa00-7234-b10d-723391130866
Co-authored-by: Amp <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 9, 2026 01:13
@changeset-bot

changeset-bot Bot commented Nov 9, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 4581d11

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

This PR is from an external contributor and must be approved by a stagehand team member with write access before CI can run.
Approving the latest commit mirrors it into an internal PR owned by the approver.
If new commits are pushed later, the internal PR stays open but is marked stale until someone approves the latest external commit and refreshes it.

@github-actions github-actions Bot added external-contributor Tracks PRs mirrored from external contributor forks. external-contributor:awaiting-approval Waiting for a stagehand team member to approve the latest external commit. labels Nov 9, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external-contributor:awaiting-approval Waiting for a stagehand team member to approve the latest external commit. external-contributor Tracks PRs mirrored from external contributor forks.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

locator().nth(-1) throws RangeError instead of selecting the last match (diverges from Playwright contract)

2 participants