Skip to content

feat(sdk-go): add role and text locator builders - #3193

Open
supremeboxlogos wants to merge 6 commits into
amel/role-text-locators-pythonfrom
amel/role-text-locators-go
Open

supremeboxlogos wants to merge 6 commits into
amel/role-text-locators-pythonfrom
amel/role-text-locators-go

Conversation

@supremeboxlogos

Copy link
Copy Markdown
Member

why

Complete the stack with a focused Go API review. Stacked on #3192; approximately 560 changed lines including documentation and tests.

what changed

  • Add lazy GetByRole/GetByText on Page and PageLocator, plus descendant PageLocator.Locator.
  • Use canonical generated options and LiteralText/RegexText values; preserve caller-option snapshots and earlier First/Nth selections while keeping existing locator actions.
  • Document Go usage and add real-browser coverage to the existing local integration suite. No new RPCs, extension runtime changes, or smoke-test infrastructure.

test plan

  • Go SDK/internal package tests, focused semantic/TextMatch race tests, and vet passed.
  • Existing real-Chrome integration, including the explicit semantic-locator subtest, passed: accessible-name/description regexes, ECMAScript lookbehind, indexed scope, click, literal/regex text, and untouched sibling scope.
  • Independent examples check, generator tests/freshness, and embedded ZIP freshness passed.
  • 27 reference checks passed; final documentation matches the reviewed combined candidate.
  • Independent strict public-builder review passed.

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

changeset-bot Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: cc48c11

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

This PR includes changesets to release 1 package
Name Type
@browserbasehq/stagehand-go Minor

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 6 files

Confidence score: 4/5

  • GetByRole in packages/sdk-go/semantic_locator.go accepts unsupported roles and Level: 0, so invalid input fails only on the first browser operation. Validate the role and options before returning the locator.
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/sdk-go/semantic_locator.go">

<violation number="1" location="packages/sdk-go/semantic_locator.go:16">
P2: `GetByRole` returns a locator without error for unsupported roles or `Level: 0`, so the invalid input fails only on the first browser operation. Validate the role and options before returning the locator, matching the TypeScript and Python builders.</violation>
</file>
Architecture diagram
sequenceDiagram
    participant GoSDK as Go SDK (User Code)
    participant PL as PageLocator
    participant Page as Page
    participant RPC as RPC Client
    participant Ext as Extension Runtime (Chrome)
    participant DOM as DOM/Accessibility Tree

    Note over GoSDK,Page: NEW: Semantic Locator Builder API
    
    GoSDK->>Page: GetByRole(role, options)
    Page->>Page: newSemanticLocator(role step)
    Page-->>GoSDK: PageLocator (lazy, no RPC)
    
    GoSDK->>Page: GetByText(text, options)
    Page->>Page: newSemanticLocator(text step)
    Page-->>GoSDK: PageLocator (lazy, no RPC)
    
    GoSDK->>PL: GetByRole(role, options)
    PL->>PL: extendSemanticLocator()
    alt Existing semantic query
        PL->>PL: Decode existing step array
    else Plain CSS selector
        PL->>PL: Encode selector as first step
    end
    opt Locator has Nth/First selection
        PL->>PL: Append nth step to array
    end
    PL->>PL: Append role step
    PL-->>GoSDK: PageLocator (lazy, no RPC)

    GoSDK->>PL: GetByText(text, options)
    PL->>PL: extendSemanticLocator()
    PL-->>GoSDK: PageLocator (lazy, no RPC)
    
    GoSDK->>PL: Locator(selector)
    alt Selector contains reserved semantic prefix
        PL-->>GoSDK: Error (reject nested query)
    else Valid selector
        PL->>PL: extendSemanticLocator()
        PL-->>GoSDK: PageLocator (lazy, no RPC)
    end

    Note over GoSDK,Ext: Lazy Evaluation - First Action Trigger
    
    GoSDK->>PL: Count() / Click() / InnerText()
    PL->>RPC: locator.count / locator.click / locator.inner_text
    RPC->>Ext: JSON-RPC with encoded query steps
    Ext->>DOM: Resolve semantic steps (ARIA role, accessible name/regex)
    DOM-->>Ext: Matching elements
    Ext-->>RPC: Result
    RPC-->>PL: Locator action result
    PL-->>GoSDK: Result / error

    Note over GoSDK,Ext: Option Snapshot Behavior
    
    GoSDK->>PL: Build locator with options
    PL->>PL: Snapshot options at build time
    GoSDK->>GoSDK: Mutate original options after build
    Note over PL,GoSDK: Existing locator unaffected (snapshot preserved)
Loading

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

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

Comment thread packages/sdk-go/semantic_locator.go Resolved
// visible text. Hidden elements are excluded unless IncludeHidden is true.
// Pass nil options for defaults.
func (p *Page) GetByRole(role AriaRole, options *GetByRoleOptions) (*PageLocator, error) {
return newSemanticLocator(p.rpc, p.PageID(), nil, RoleQueryStep{Kind: "role", Role: role, Options: options})

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: GetByRole returns a locator without error for unsupported roles or Level: 0, so the invalid input fails only on the first browser operation. Validate the role and options before returning the locator, matching the TypeScript and Python builders.

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/sdk-go/semantic_locator.go, line 16:

<comment>`GetByRole` returns a locator without error for unsupported roles or `Level: 0`, so the invalid input fails only on the first browser operation. Validate the role and options before returning the locator, matching the TypeScript and Python builders.</comment>

<file context>
@@ -0,0 +1,100 @@
+// visible text. Hidden elements are excluded unless IncludeHidden is true.
+// Pass nil options for defaults.
+func (p *Page) GetByRole(role AriaRole, options *GetByRoleOptions) (*PageLocator, error) {
+	return newSemanticLocator(p.rpc, p.PageID(), nil, RoleQueryStep{Kind: "role", Role: role, Options: options})
+}
+
</file context>

Comment thread packages/docs/v4/reference/page.mdx Resolved

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