Repository navigation
[fix]: make count() respect nth() and first() - #3443
abhiramshaji-cyber wants to merge 3 commits into
Conversation
🦋 Changeset detectedLatest commit: b660f7d The changes in this PR will be included in the next version bump. This PR includes changesets to release 19 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
This PR is from an external contributor and must be approved by a stagehand team member with write access before CI can run. |
|
Hey @shrey150, hope you're doing well! I've noticed you've been approving a lot of the external PRs lately (#3463, #3412 and more), which must be a lot on top of everything else, so thank you for keeping that moving. No rush at all on this one. I just wanted to ask if anything would make it easier to look at, or if you'd like the approach changed. One thing I wasn't sure about: should |
Hey Stagehand team, hope you're doing well!
Asking a narrowed locator like
locator("li").nth(3)how many elements it matches returned the count for the whole list, and this PR makes it return 1 when that element exists and 0 when it does not.What this code is for
Locator.count()inpackages/extension/understudy/locator.tsanswerslocator.countcalls from the SDKs. The TypeScript and Python SDKs send the selector plus an optionalnthover the protocol.resolveLocatorinruntime.tsturns that intopage.deepLocator(selector).nth(nth), andDeepLocatorDelegate.count()builds the realLocatorwith that index and calls itscount(). So every SDK call, and every deep or iframe locator, ends up in this one method.Before this PR
A script loads a page with five
<li>items and callspage.locator("li").nth(3).count().locator.countwithselector: "li"andnth: 3.resolveLocatorreturns a deep locator narrowed to index 3, which builds aLocatorwithnthIndexset to 3.count()callsthis.selectorResolver.count(this.selectorQuery), which counts everylion the page: 5.count()returns that 5 directly and never looks atnthIndex.nth(10).count()andfirst().count()also return 5, so a check likeif (await item.count() > 0)passes for an index that does not exist.After this PR
Same script, same call.
count()now checksnthIndex. It is 3, and 3 is below 5, so it returns 1.nth(5).count()andnth(10).count()return 0, since that index has no element.first().count()returns 1 when anything matches and 0 on an empty page.page.locator("li").count()with nonth()still returns 5.The diagram
flowchart LR classDef box fill:#eef1ff,stroke:#4c5bd4,color:#111827 classDef bad fill:#ffd6d6,stroke:#cc0000,color:#111827,stroke-width:3px classDef good fill:#c8f0c8,stroke:#008800,color:#111827,stroke-width:3px subgraph BEFORE["BEFORE: nth(3).count() returns 5"] direction TB B1["locator('li').nth(3).count()"] --> B2["resolveLocator applies nth(3)"] B2 --> B3["selectorResolver.count gives 5"] B3 --> B4{"nthIndex is never read"} B4 -->|"nth(3), nth(10), first()"| B5["returns 5"] B4 -->|"no nth()"| B6["locator('li').count() returns 5"] end subgraph AFTER["AFTER: nth(3).count() returns 1"] direction TB A1["locator('li').nth(3).count()"] --> A2["resolveLocator applies nth(3)"] A2 --> A3["selectorResolver.count gives 5"] A3 --> A4{"THE CHANGE: nthIndex < total"} A4 -->|"nth(3) gives 1, nth(10) gives 0"| A5["returns 1 or 0"] A4 -->|"no nth()"| A6["locator('li').count() returns 5"] end B1 ~~~ A1 class B1,B2,B3,B6 box class A1,A2,A3,A6 box class B4,B5 bad class A4,A5 good linkStyle 3 stroke:#cc0000,stroke-width:5px linkStyle 8 stroke:#008800,stroke-width:5px style BEFORE fill:#fff5f5,stroke:#cc0000,color:#111827 style AFTER fill:#f3fff3,stroke:#008800,color:#111827The change
Locator.count()keeps the existingselectorResolver.countcall and then readsnthIndex: when it is-1(nonth()was called) it returns the total as before, otherwise it returnsnthIndex < total ? 1 : 0. That is the whole fix, 3 lines. A changeset marks it as a patch for the extension and both SDKs that bundle it.What changes for a user
locator(...).count()withoutnth()returns exactly what it did before, for CSS, XPath andtext=selectors.nth(i).count()andfirst().count()now return 1 or 0, which is what Playwright returns for a narrowed locator and what the docs describe as "the elements matched by this locator".locator.countto the extension and never count on their own.textContent,isVisible,clickand the rest) already usednthIndexthroughresolveNode, socount()now agrees with them.why
count()was the onlyLocatormethod that ignorednth(), so it disagreed withtextContent()andisVisible()on the same narrowed locator.what changed
packages/extension/understudy/locator.ts:count()returns 1 or 0 for a locator narrowed bynth()orfirst(). Plus one unit test table, one integration test, and a changeset.test plan
locatorActions.test.tsfor nonth()(5 and 0 matches),nth(0),nth(3),nth(4)(last),nth(5)(equal to the total),nth(10), andnth(0)on 0 matches. Against unpatchedmain, the 5 narrowed cases fail with the total returned; with the fix all 159 tests in the file pass.locatorCount.test.tsagainst a real headless Chrome with five<li>items, covering CSS, XPath andtext=selectors plusfirst()on no matches. Before the fix it fails withexpected 5 to be 1; after,locatorCountandlocatorNthpass (16 of 16).pnpm --filter ./packages/extension test:unit: 1198 passed.pnpm typecheck, extension and SDK typecheck,pnpm lint(no new findings),oxfmt --check, andcheck-changesets.tsall pass.I'd love your view on one thing: I followed Playwright and return 1 or 0 for a narrowed locator. Would you rather
count()keep reporting the total, with the docs updated instead? Happy to switch if that fits Stagehand better.This touches the same code as two open PRs, and I want to make sure it plays nicely with both:
nth()(thank you, separatinghasExplicitNthfrom the-1sentinel is a really nice cleanup). The two merge without a git conflict, but they do interact: with fix(extension): support Playwright-style negative nth() indices #3417,nth(-1)keepsnthIndexat-1, so thenthIndex < 0check here would return the total for it. Whichever lands second should check!this.hasExplicitNthinstead, and count a negative index as 1 when its absolute value is at most the total and 0 otherwise. I'm glad to do that rebase and add the negative cases in whichever order you prefer.count()and adds a test at the same spot inlocatorCount.test.ts, so whichever lands second needs a small, mechanical rebase. Happy to take that on.Any feedback or tips are very welcome, thank you for taking a look!
Summary by cubic
Fixes
Locator.count()so a locator narrowed withnth()orfirst()returns 1 when the index exists and 0 when it does not, instead of the count for the whole selector match.count()was the onlyLocatormethod that ignorednth(), so it disagreed withtextContent()andisVisible()on the same narrowed locator.Behavior
count()withoutnth()still returns the total match count.locator.countto the extension.count()docs inpackages/docs/v4/reference/locator.mdxnow describe the narrowed behavior for the TypeScript, Python, and Go SDKs.Testing
locatorActions.test.tscovers nonth(),nth(0),nth(3),nth(4),nth(5),nth(10), and 0 matches.locatorCount.test.tscovers CSS, XPath, andtext=selectors plusfirst()on no matches.Written for commit b660f7d. Summary will update on new commits.