Skip to content

feat(sdk-ts): add role and text locator builders - #3191

Open
supremeboxlogos wants to merge 6 commits into
amel/role-text-locators-protocolfrom
amel/role-text-locators-ts
Open

supremeboxlogos wants to merge 6 commits into
amel/role-text-locators-protocolfrom
amel/role-text-locators-ts

Conversation

@supremeboxlogos

Copy link
Copy Markdown
Member

why

Expose readable role/text queries in TypeScript as a language-sized review. Stacked on #3190; no Python/Go public API changes.

what changed

  • Add lazy Page/Locator getByRole and getByText builders plus descendant Locator.locator, using the shared protocol contract and native RegExp values.
  • Preserve earlier nth selections and snapshot caller options when composing a query. Existing locator actions remain the execution path.
  • Document TypeScript usage, matching rules, and chaining examples.
  • Make documentation coverage language-local so SDKs can land independently. Keep exact cross-language RPC operation/binding parity; remove the redundant unconditional pure-callable equality check and retain shared TS/Python binding checks.

test plan

  • 434 SDK checks passed; 4 skipped. SDK typecheck, build, and scoped lint passed.
  • 62 real-Chrome cases passed, including public-builder filters, index-preserving composition, and a scoped click with an identical earlier out-of-scope button.
  • 27 reference guards and 15 structural SDK parity guards passed.
  • Strict API/boundary review passed; no new smoke-test infrastructure.

@supremeboxlogos
supremeboxlogos requested a review from a team as a code owner October 15, 2026 06:44
@changeset-bot

changeset-bot Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: fea6d6d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 21 packages
Name Type
@browserbasehq/stagehand Minor
browse Patch
@browserbasehq/stagehand-examples Patch
@browserbasehq/stagehand-integrations-claude-cua-sdk Patch
@browserbasehq/stagehand-integrations Patch
@browserbasehq/eve Patch
@browserbasehq/stagehand-integrations-gemini-cua-sdk Patch
@browserbasehq/stagehand-integrations-example-pi-facade Patch
@browserbasehq/stagehand-integrations-claude-agent-sdk Patch
@browserbasehq/stagehand-integrations-example-claude-code-facade Patch
@browserbasehq/stagehand-integrations-codex-sdk Patch
@browserbasehq/stagehand-integrations-example-codex-facade Patch
@browserbasehq/stagehand-integrations-cursor-sdk Patch
@browserbasehq/stagehand-integrations-example-cursor-facade Patch
@browserbasehq/stagehand-integrations-deepagents-sdk Patch
@browserbasehq/stagehand-integrations-eve-sdk Patch
@browserbasehq/stagehand-integrations-fx-sdk Patch
@browserbasehq/stagehand-integrations-mastra-sdk Patch
@browserbasehq/stagehand-integrations-example-mastra-facade Patch
@browserbasehq/stagehand-integrations-pi-sdk Patch
@browserbasehq/stagehand-integrations-example-vercel-ai-facade Patch

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

@cubic-dev-ai cubic-dev-ai Bot 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.

1 issue found across 11 files

Confidence score: 4/5

  • packages/docs/v4/reference/locator.mdx advertises role/text builders to Python and Go users, though those SDKs don’t expose them. Keep that guidance in the TypeScript tab or label it as TypeScript-only.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="packages/docs/v4/reference/locator.mdx">

<violation number="1" location="packages/docs/v4/reference/locator.mdx:7">
P2: This shared introduction and the matching rules advertise role/text builders to Python and Go users, but those SDKs do not expose these methods. Move the role/text guidance into the TypeScript tab or label it as TypeScript-only.</violation>
</file>
Architecture diagram
sequenceDiagram
    participant Dev as TS SDK Consumer
    participant Page as Page (sdk-ts)
    participant Loc as Locator (sdk-ts)
    participant Query as locatorQuery.ts
    participant Proto as stagehand-protocol
    participant RPC as RPC Client
    participant Browser as Browser Extension

    Note over Dev,Browser: Lazy Locator Builder Flow

    Dev->>Page: getByRole("button", { name: "Save" })
    Page->>Query: roleQueryStep(role, options)
    Query->>Query: Convert RegExp to {source, flags}
    Query->>Proto: encodeLocatorQuery([step])
    Proto-->>Page: Encoded selector string
    Page->>Loc: new Locator(descriptor)
    Loc-->>Dev: Lazy Locator (no RPC)

    Dev->>Loc: getByText("Draft")
    Loc->>Query: textQueryStep(text, options)
    Query->>Proto: encodeLocatorQuery([...steps])
    Proto-->>Loc: Updated descriptor
    Loc-->>Dev: Composed Lazy Locator

    Note over Dev,Browser: Scoped Descendant Composition

    Dev->>Loc: locator("span")
    Loc->>Loc: appendLocatorStep(descriptor, {kind:"selector"})
    Loc->>Loc: Preserve previous nth index in query order
    Loc-->>Dev: Composed Lazy Locator

    Note over Dev,Browser: nth Selection Preservation

    Dev->>Page: locator("#cards").nth(1)
    Page->>Loc: Locator with nth=1
    Dev->>Loc: getByRole("heading", { level: 3 })
    Loc->>Loc: appendLocatorStep - moves nth into query steps
    Loc->>Loc: Clear descriptor.nth, embed as {kind:"nth"}
    Dev->>Loc: getByText("Two")
    Loc->>Loc: Append text step after nth
    Loc-->>Dev: Scoped locator preserving index

    Note over Dev,Browser: Action Execution (existing path)

    Dev->>Loc: click({ timeout: 5000 })
    Loc->>RPC: send("locator.click", descriptor)
    RPC->>Browser: RPC call with encoded query
    Browser-->>RPC: Result
    RPC-->>Loc: Click completed
    Loc-->>Dev: void

    Note over Dev,Browser: Local Option Validation

    Dev->>Page: getByRole("heading", { level: 0 })
    alt Invalid options (level=0)
        Page-->>Dev: Throw validation error (no RPC)
    else Valid options
        Page-->>Dev: Returns lazy Locator
    end

    Note over Dev,Browser: RegExp Handling

    Dev->>Page: getByText(/Alpha\nBeta/, { exact: true })
    Page->>Query: Convert to {source: "Alpha\\nBeta", flags: ""}
    Query->>Proto: Encode with native RegExp data
    Proto-->>Page: Selector string
    Page-->>Dev: Lazy Locator with regex preserved
Loading

Reply with feedback, questions, or to request a fix.

View guided diff | Turn on auto-fix | Re-trigger cubic

Comment thread packages/docs/v4/reference/locator.mdx Resolved
---

Create a `Locator` from a page selector, then reuse it for element interactions and state queries. `first()` and `nth()` refine the local locator descriptor; the resulting descriptor is sent with later operations.
Create a `Locator` from a page selector, role, or text, then reuse it for element interactions and state queries. Builders are synchronous and lazy: they record a query without contacting the browser. Each later action or read resolves that query against the current DOM.

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.

P2: This shared introduction and the matching rules advertise role/text builders to Python and Go users, but those SDKs do not expose these methods. Move the role/text guidance into the TypeScript tab or label it as TypeScript-only.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At packages/docs/v4/reference/locator.mdx, line 7:

<comment>This shared introduction and the matching rules advertise role/text builders to Python and Go users, but those SDKs do not expose these methods. Move the role/text guidance into the TypeScript tab or label it as TypeScript-only.</comment>

<file context>
@@ -4,16 +4,27 @@ description: "Target elements and perform focused DOM interactions"
 ---
 
-Create a `Locator` from a page selector, then reuse it for element interactions and state queries. `first()` and `nth()` refine the local locator descriptor; the resulting descriptor is sent with later operations.
+Create a `Locator` from a page selector, role, or text, then reuse it for element interactions and state queries. Builders are synchronous and lazy: they record a query without contacting the browser. Each later action or read resolves that query against the current DOM.
 
-Selectors pierce shadow DOM, including closed roots.
</file context>

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant