Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

fix: unify text/xpath selector resolution so a first/last/nth wrapper cannot change which element is found (PIN-037)

Luigi Agostib38809e126c8
diff --git a/internal/bridge/action_resolve.go b/internal/bridge/action_resolve.go
index 63a5ce8c..1237e621 100644
--- a/internal/bridge/action_resolve.go
+++ b/internal/bridge/action_resolve.go
@@ -467,83 +467,9 @@ func resolveElementMetaInFrame(ctx context.Context, frameID, functionDeclaration
return meta, nil
}
-func ResolveXPathToNodeID(ctx context.Context, xpath string) (int64, error) {
- var backendNodeID int64
- err := chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {
- // Use DOM.getDocument first to ensure the DOM is available.
- var docResult json.RawMessage
- if err := chromedp.FromContext(ctx).Target.Execute(ctx, "DOM.getDocument", map[string]any{"depth": 0}, &docResult); err != nil {
- return fmt.Errorf("get document: %w", err)
- }
-
- var searchResult json.RawMessage
- if err := chromedp.FromContext(ctx).Target.Execute(ctx, "DOM.performSearch", map[string]any{
- "query": xpath,
- }, &searchResult); err != nil {
- return fmt.Errorf("xpath search: %w", err)
- }
-
- var sr struct {
- SearchID string `json:"searchId"`
- ResultCount int `json:"resultCount"`
- }
- if err := json.Unmarshal(searchResult, &sr); err != nil {
- return err
- }
- if sr.ResultCount == 0 {
- return fmt.Errorf("xpath %q: %w", xpath, ErrSelectorNoMatch)
- }
-
- var getResult json.RawMessage
- if err := chromedp.FromContext(ctx).Target.Execute(ctx, "DOM.getSearchResults", map[string]any{
- "searchId": sr.SearchID,
- "fromIndex": 0,
- "toIndex": 1,
- }, &getResult); err != nil {
- return fmt.Errorf("get search results: %w", err)
- }
-
- var gr struct {
- NodeIDs []int64 `json:"nodeIds"`
- }
- if err := json.Unmarshal(getResult, &gr); err != nil {
- return err
- }
- if len(gr.NodeIDs) == 0 {
- return fmt.Errorf("xpath %q: no node IDs returned", xpath)
- }
-
- var descResult json.RawMessage
- if err := chromedp.FromContext(ctx).Target.Execute(ctx, "DOM.describeNode", map[string]any{
- "nodeId": gr.NodeIDs[0],
- }, &descResult); err != nil {
- return fmt.Errorf("describe node: %w", err)
- }
-
- var desc struct {
- Node struct {
- BackendNodeID int64 `json:"backendNodeId"`
- } `json:"node"`
- }
- if err := json.Unmarshal(descResult, &desc); err != nil {
- return err
- }
- backendNodeID = desc.Node.BackendNodeID
-
- _ = chromedp.FromContext(ctx).Target.Execute(ctx, "DOM.discardSearchResults", map[string]any{
- "searchId": sr.SearchID,
- }, nil)
-
- return nil
- }))
- return backendNodeID, err
-}
-
-// jsNormalizeHelper is the shared text-normalization snippet injected into both
-// findTextFn and resolveSelectorAtFn so the two embedded JS programs can't drift
-// apart. It lowercases, collapses runs of whitespace to a single space, and
-// trims — identical in both call sites. Indentation here is cosmetic (JS ignores
-// it); only the runtime behavior matters.
+// jsNormalizeHelper lowercases, collapses runs of whitespace to a single space,
+// and trims. Indentation here is cosmetic (JS ignores it); only the runtime
+// behavior matters.
const jsNormalizeHelper = `const normalize = (value) => String(value || "")
.toLowerCase()
.replace(/\s+/g, " ")
@@ -554,100 +480,7 @@ func ResolveTextToNodeID(ctx context.Context, text string) (int64, error) {
}
func ResolveTextToNodeIDInFrame(ctx context.Context, frameID, text string) (int64, error) {
- var backendNodeID int64
- // Implementation notes:
- // - Use `textContent` (not `innerText`) for the bulk scan. `innerText`
- // forces a synchronous layout pass per-element and is O(N^2) on large
- // pages; `textContent` is O(N). This fixes the intermittent
- // "context deadline exceeded" failures on dynamic/large fixtures.
- // - Exact-match pass first (single linear sweep). Fuzzy fallback is
- // only evaluated when no exact hit fires — most real lookups are
- // covered by the exact pass and cost nothing extra.
- // - "Leaf-most match wins": we keep the smallest element (by
- // descendant count) whose text contains the needle, so a button
- // that reads "Sign In" is preferred over its ancestor <body> which
- // technically also contains the string.
- const findTextFn = `function(needle) {
- const root = this.body || this.documentElement;
- if (!root) return null;
-
- ` + jsNormalizeHelper + `
- const semanticWeight = (el) => {
- const tag = (el.tagName || "").toLowerCase();
- if (tag === "button" || tag === "a" || tag === "input") return 0.25;
- const role = normalize(el.getAttribute && el.getAttribute("role"));
- if (role === "button" || role === "link" || role === "textbox") return 0.2;
- return 0;
- };
-
- const needleNorm = normalize(needle);
- if (!needleNorm) return null;
-
- const elements = root.querySelectorAll("*");
-
- // Exact-match pass: pick the leaf-most element whose textContent
- // contains the needle. textContent is cheap (no layout), so we
- // can afford to visit every node.
- let exactBest = null;
- let exactBestSize = Infinity;
- for (const el of elements) {
- const tc = normalize(el.textContent || "");
- if (!tc || !tc.includes(needleNorm)) continue;
- // "Leaf-most" = fewest descendants. Smaller subtree == more
- // specific match. Ties broken by semantic weight.
- const size = el.getElementsByTagName("*").length;
- if (size < exactBestSize ||
- (size === exactBestSize && exactBest && semanticWeight(el) > semanticWeight(exactBest))) {
- exactBest = el;
- exactBestSize = size;
- }
- }
- if (exactBest) return exactBest;
-
- // Fuzzy fallback: token-overlap score with semantic weighting.
- // Only runs if exact-match missed.
- const tokens = needleNorm.split(" ").filter(Boolean);
- if (tokens.length === 0) return null;
-
- let best = null;
- let bestScore = 0;
- for (const el of elements) {
- const tc = normalize(el.textContent || "");
- if (!tc) continue;
- let hits = 0;
- for (const token of tokens) {
- if (tc.includes(token)) hits++;
- }
- let score = hits / tokens.length + semanticWeight(el);
- if (score > bestScore) {
- bestScore = score;
- best = el;
- }
- }
- return (best && bestScore >= 0.7) ? best : null;
- }`
-
- // Bound the lookup with its own short deadline so a slow resolution
- // can't eat the entire action timeout. Callers can still pass a longer
- // outer deadline if they really want to wait — this just caps how long
- // we'll spend before giving up with "text not found".
- lookupCtx, cancel := context.WithTimeout(ctx, 3*time.Second)
- defer cancel()
-
- err := chromedp.Run(lookupCtx, chromedp.ActionFunc(func(ctx context.Context) error {
- nid, err := resolveNodeInFrame(ctx, frameID, findTextFn, []map[string]any{{"value": text}})
- if err != nil {
- // If the parent context is still alive, this was a real
- // "not found" rather than a timeout — surface it clearly.
- if lookupCtx.Err() != nil && ctx.Err() == nil {
- return fmt.Errorf("text %q lookup timed out after 3s (page may be large or unresponsive): %w", text, err)
- }
- return fmt.Errorf("text %q not found: %w", text, err)
- }
- backendNodeID = nid
- return nil
- }))
- return backendNodeID, err
+ return resolveSelectorAtInFrame(ctx, frameID, selector.Selector{Kind: selector.KindText, Value: text}, 0, false)
}
const resolveSelectorAtFn = `function(kind, value, index, fromEnd) {
@@ -671,7 +504,7 @@ const resolveSelectorAtFn = `function(kind, value, index, fromEnd) {
if (idx < 0 || idx >= items.length) return null;
return items[idx];
};
- const deepQueryAll = (selector) => {
+ const deepQueryAll = (selector, from) => {
const out = [];
const visit = (scope) => {
if (!scope || !scope.querySelectorAll) return;
@@ -680,36 +513,51 @@ const resolveSelectorAtFn = `function(kind, value, index, fromEnd) {
out.push(...Array.from(scope.querySelectorAll(selector)));
for (const el of elements) if (el.shadowRoot) visit(el.shadowRoot);
};
- visit(root);
+ visit(from || root);
return out;
};
+ // Source text is not rendered text: a CSS comment or a script string
+ // mentioning a label is nothing a user can see or click, so matching it
+ // hands back an element no action can operate on.
+ const nonRendered = new Set(["script", "style", "noscript", "template"]);
+ // At equal subtree size a control outranks a plain wrapper carrying the same
+ // label, so text: lands on the <button> rather than the <div> around it.
+ const semanticWeight = (el) => {
+ const tag = (el.tagName || "").toLowerCase();
+ if (tag === "button" || tag === "a" || tag === "input") return 0.25;
+ const role = normalize(el.getAttribute && el.getAttribute("role"));
+ if (role === "button" || role === "link" || role === "textbox") return 0.2;
+ return 0;
+ };
+ // Leaf-most wins: the fewest descendants is the most specific match. The sort
+ // is stable, so equally-weighted candidates keep document order and the index
+ // first/last/nth asks for stays meaningful.
+ const smallestFirst = (matches) => {
+ const minSize = Math.min(...matches.map((item) => item.size));
+ return matches
+ .filter((item) => item.size === minSize)
+ .sort((a, b) => semanticWeight(b.el) - semanticWeight(a.el))
+ .map((item) => item.el);
+ };
const textCandidates = (query) => {
- const elements = deepQueryAll("*");
- const exact = [];
- for (const el of elements) {
- const text = normalize(el.textContent || "");
- if (!query || !text || !(text === query || text.includes(query))) continue;
- exact.push({ el, size: el.getElementsByTagName("*").length });
- }
- if (exact.length) {
- const minSize = Math.min(...exact.map((item) => item.size));
- return exact.filter((item) => item.size === minSize).map((item) => item.el);
- }
+ if (!query) return [];
+ // textContent, not innerText: innerText forces a synchronous layout per
+ // element and is O(N^2) on large pages.
+ const elements = deepQueryAll("*", root.body || root)
+ .filter((el) => !nonRendered.has((el.tagName || "").toLowerCase()));
+ const measured = elements.map((el) => ({ el, text: normalize(el.textContent || ""), size: el.getElementsByTagName("*").length }));
+ const exact = measured.filter((item) => item.text && item.text.includes(query));
+ if (exact.length) return smallestFirst(exact);
const tokens = query.split(" ").filter(Boolean);
if (!tokens.length) return [];
- const fuzzy = [];
- for (const el of elements) {
- const text = normalize(el.textContent || "");
- if (!text) continue;
+ const fuzzy = measured.filter((item) => {
+ if (!item.text) return false;
let hits = 0;
- for (const token of tokens) if (text.includes(token)) hits++;
- if (hits / tokens.length >= 0.7) {
- fuzzy.push({ el, size: el.getElementsByTagName("*").length });
- }
- }
+ for (const token of tokens) if (item.text.includes(token)) hits++;
+ return hits / tokens.length >= 0.7;
+ });
if (!fuzzy.length) return [];
- const minSize = Math.min(...fuzzy.map((item) => item.size));
- return fuzzy.filter((item) => item.size === minSize).map((item) => item.el);
+ return smallestFirst(fuzzy);
};
try {
@@ -736,51 +584,64 @@ const resolveSelectorAtFn = `function(kind, value, index, fromEnd) {
}
}`
-func resolveSelectorAtInFrame(ctx context.Context, frameID string, sel selector.Selector, index int, fromEnd bool) (int64, error) {
- kind := string(sel.Kind)
+// textLookupDeadline bounds the text scan so a slow resolution cannot eat the
+// whole action timeout. It applies wherever text is matched, wrapped or not —
+// first:/last:/nth: reach the same scan.
+const textLookupDeadline = 3 * time.Second
+
+// resolveSelectorAt is the single entry point for every css/xpath/text
+// resolution. scopeLabel names the scope in errors; call runs the resolver
+// against whichever root the caller resolved.
+func resolveSelectorAt(ctx context.Context, sel selector.Selector, index int, fromEnd bool, scopeLabel string, call func(context.Context, []map[string]any) (int64, error)) (int64, error) {
switch sel.Kind {
case selector.KindCSS, selector.KindXPath, selector.KindText:
default:
return 0, fmt.Errorf("%s selector cannot be used with first/last/nth", sel.Kind)
}
- var backendNodeID int64
- err := chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {
- nid, err := resolveNodeInFrame(ctx, frameID, resolveSelectorAtFn, []map[string]any{
- {"value": kind},
- {"value": sel.Value},
- {"value": index},
- {"value": fromEnd},
- })
- if err != nil {
- return fmt.Errorf("%s %q: %w", sel.Kind, sel.Value, err)
- }
- backendNodeID = nid
- return nil
- }))
- return backendNodeID, err
-}
-
-func resolveSelectorAtWithinNode(ctx context.Context, scopeBackendNodeID int64, sel selector.Selector, index int, fromEnd bool) (int64, error) {
- kind := string(sel.Kind)
- switch sel.Kind {
- case selector.KindCSS, selector.KindXPath, selector.KindText:
- default:
- return 0, fmt.Errorf("%s selector cannot be used with first/last/nth", sel.Kind)
+ lookupCtx := ctx
+ if sel.Kind == selector.KindText {
+ var cancel context.CancelFunc
+ lookupCtx, cancel = context.WithTimeout(ctx, textLookupDeadline)
+ defer cancel()
}
- nid, err := resolveNodeWithinBackendNode(ctx, scopeBackendNodeID, resolveSelectorAtFn, []map[string]any{
- {"value": kind},
+ nid, err := call(lookupCtx, []map[string]any{
+ {"value": string(sel.Kind)},
{"value": sel.Value},
{"value": index},
{"value": fromEnd},
})
if err != nil {
- return 0, fmt.Errorf("%s %q inside topmost dialog: %w", sel.Kind, sel.Value, err)
+ if lookupCtx.Err() != nil && ctx.Err() == nil {
+ return 0, fmt.Errorf("%s %q%s lookup timed out after %s (page may be large or unresponsive): %w", sel.Kind, sel.Value, scopeLabel, textLookupDeadline, err)
+ }
+ return 0, fmt.Errorf("%s %q%s: %w", sel.Kind, sel.Value, scopeLabel, err)
}
return nid, nil
}
+func resolveSelectorAtInFrame(ctx context.Context, frameID string, sel selector.Selector, index int, fromEnd bool) (int64, error) {
+ return resolveSelectorAt(ctx, sel, index, fromEnd, "", func(ctx context.Context, args []map[string]any) (int64, error) {
+ var backendNodeID int64
+ err := chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {
+ nid, err := resolveNodeInFrame(ctx, frameID, resolveSelectorAtFn, args)
+ if err != nil {
+ return err
+ }
+ backendNodeID = nid
+ return nil
+ }))
+ return backendNodeID, err
+ })
+}
+
+func resolveSelectorAtWithinNode(ctx context.Context, scopeBackendNodeID int64, sel selector.Selector, index int, fromEnd bool) (int64, error) {
+ return resolveSelectorAt(ctx, sel, index, fromEnd, " inside topmost dialog", func(ctx context.Context, args []map[string]any) (int64, error) {
+ return resolveNodeWithinBackendNode(ctx, scopeBackendNodeID, resolveSelectorAtFn, args)
+ })
+}
+
func resolveNestedSelectorAtInFrame(ctx context.Context, frameID string, raw string, refCache *RefCache, index int, fromEnd bool) (int64, error) {
inner := selector.Parse(raw)
switch inner.Kind {
@@ -831,6 +692,10 @@ func resolveNestedSelectorWithinNode(ctx context.Context, scopeBackendNodeID int
}
}
+func ResolveXPathToNodeID(ctx context.Context, xpath string) (int64, error) {
+ return ResolveXPathToNodeIDInFrame(ctx, "", xpath)
+}
+
func ResolveCSSToNodeID(ctx context.Context, css string) (int64, error) {
return ResolveCSSToNodeIDInFrame(ctx, "", css)
}
@@ -844,23 +709,11 @@ func ResolveCSSToNodeIDInFrame(ctx context.Context, frameID, css string) (int64,
return resolveSelectorAtInFrame(ctx, frameID, selector.Selector{Kind: selector.KindCSS, Value: css}, 0, false)
}
+// XPath does not pierce shadow roots: document.evaluate cannot cross a shadow
+// boundary. It resolves through the shared walker for consistency and
+// isolated-world execution, not for shadow support.
func ResolveXPathToNodeIDInFrame(ctx context.Context, frameID, xpath string) (int64, error) {
- if frameID == "" {
- return ResolveXPathToNodeID(ctx, xpath)
- }
-
- var backendNodeID int64
- err := chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {
- nid, err := resolveNodeInFrame(ctx, frameID, `function(xpath) {
- return this.evaluate(xpath, this, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
- }`, []map[string]any{{"value": xpath}})
- if err != nil {
- return fmt.Errorf("xpath %q: %w", xpath, err)
- }
- backendNodeID = nid
- return nil
- }))
- return backendNodeID, err
+ return resolveSelectorAtInFrame(ctx, frameID, selector.Selector{Kind: selector.KindXPath, Value: xpath}, 0, false)
}
func ResolveFrameElementMetaInFrame(ctx context.Context, sel selector.Selector, frameID string) (FrameElementMeta, error) {
diff --git a/internal/bridge/action_resolve_unified_test.go b/internal/bridge/action_resolve_unified_test.go
new file mode 100644
index 00000000..35e2f2bb
--- /dev/null
+++ b/internal/bridge/action_resolve_unified_test.go
@@ -0,0 +1,255 @@
+package bridge
+
+import (
+ "context"
+ "encoding/base64"
+ "encoding/json"
+ "testing"
+ "time"
+
+ "github.com/chromedp/chromedp"
+ "github.com/pinchtab/pinchtab/internal/selector"
+ "github.com/pinchtab/pinchtab/internal/testbrowser"
+)
+
+// Every trap that made the wrapped and unwrapped resolvers disagree, on one
+// page: a <style> and a <script> whose source text mentions "Publish draft"
+// with a smaller subtree than the real button, a second control behind an open
+// shadow boundary, and a <div> wrapping a <button> that share a label.
+const unifiedSelectorFixtureHTML = `<!doctype html>
+<html>
+<head>
+<style>/* Publish draft button styling */
+#real { color: rebeccapurple; }</style>
+</head>
+<body>
+<script>const hint = "Publish draft";</script>
+<button id="real">Publish draft</button>
+<div id="decoy">Archive record</div>
+<div id="wrap"><button id="inner">Archive record</button></div>
+<div id="host"></div>
+<script>
+document.getElementById('host').attachShadow({mode: 'open'}).innerHTML =
+ '<button id="shadowbtn">Send invoice</button>';
+</script>
+</body>
+</html>`
+
+func newSelectorFixture(t *testing.T) context.Context {
+ t.Helper()
+ chromePath := testbrowser.Path(t)
+
+ alloc, cancelAlloc := chromedp.NewExecAllocator(context.Background(), append(
+ chromedp.DefaultExecAllocatorOptions[:],
+ chromedp.ExecPath(chromePath),
+ chromedp.UserDataDir(t.TempDir()),
+ chromedp.Flag("headless", true),
+ chromedp.Flag("no-sandbox", true),
+ )...)
+ ctx, cancelBrowser := chromedp.NewContext(alloc)
+ ctx, cancelTimeout := context.WithTimeout(ctx, 30*time.Second)
+ t.Cleanup(func() {
+ cancelTimeout()
+ cancelBrowser()
+ cancelAlloc()
+ })
+
+ dataURL := "data:text/html;base64," + base64.StdEncoding.EncodeToString([]byte(unifiedSelectorFixtureHTML))
+ if err := chromedp.Run(ctx,
+ chromedp.Navigate(dataURL),
+ chromedp.WaitVisible("#real", chromedp.ByID),
+ ); err != nil {
+ t.Fatal(err)
+ }
+ return ctx
+}
+
+func resolve(t *testing.T, ctx context.Context, raw string) int64 {
+ t.Helper()
+ nodeID, err := ResolveUnifiedSelector(ctx, selector.Parse(raw), nil)
+ if err != nil {
+ t.Fatalf("resolve %q: %v", raw, err)
+ }
+ return nodeID
+}
+
+// describeNode reports what a selector actually landed on ("button#real",
+// "style"), so a failure names the wrong element instead of printing two opaque
+// node ids.
+func describeNode(t *testing.T, ctx context.Context, backendNodeID int64) string {
+ t.Helper()
+ var out string
+ err := chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {
+ var raw json.RawMessage
+ if err := chromedp.FromContext(ctx).Target.Execute(ctx, "DOM.resolveNode", map[string]any{
+ "backendNodeId": backendNodeID,
+ }, &raw); err != nil {
+ return err
+ }
+ var resolved struct {
+ Object struct {
+ Description string `json:"description"`
+ } `json:"object"`
+ }
+ if err := json.Unmarshal(raw, &resolved); err != nil {
+ return err
+ }
+ out = resolved.Object.Description
+ return nil
+ }))
+ if err != nil {
+ t.Fatalf("describe node %d: %v", backendNodeID, err)
+ }
+ return out
+}
+
+// The regression guard for the whole card: a wrapper may choose among matches,
+// never change which matches exist. Before unification text: found the button,
+// first: found the <style>, and nth:0: was a third implementation again.
+func TestWrappedAndPlainTextSelectorsResolveTheSameNode(t *testing.T) {
+ ctx := newSelectorFixture(t)
+
+ plain := resolve(t, ctx, "text:Publish draft")
+ for _, raw := range []string{"first:text:Publish draft", "last:text:Publish draft", "nth:0:text:Publish draft"} {
+ if got := resolve(t, ctx, raw); got != plain {
+ t.Errorf("%s resolved to %s, want the same node as text:Publish draft (%s)",
+ raw, describeNode(t, ctx, got), describeNode(t, ctx, plain))
+ }
+ }
+
+ if want := resolve(t, ctx, "css:#real"); plain != want {
+ t.Errorf("text:Publish draft resolved to %s, want button#real (%s)",
+ describeNode(t, ctx, plain), describeNode(t, ctx, want))
+ }
+}
+
+// css: was rerouted through the deep walker for shadow support and text: was
+// left behind, so the same element was reachable by one selector kind and not
+// the other.
+func TestTextSelectorPiercesOpenShadowRootLikeCSS(t *testing.T) {
+ ctx := newSelectorFixture(t)
+
+ byCSS := resolve(t, ctx, "css:#shadowbtn")
+ for _, raw := range []string{"text:Send invoice", "first:text:Send invoice", "nth:0:text:Send invoice"} {
+ if got := resolve(t, ctx, raw); got != byCSS {
+ t.Errorf("%s resolved to %s, want button#shadowbtn (%s)",
+ raw, describeNode(t, ctx, got), describeNode(t, ctx, byCSS))
+ }
+ }
+}
+
+// A CSS comment or a script string mentioning a label is not a control. These
+// elements have tiny subtrees, so before the exclusion they outranked the real
+// button on the leaf-most rule.
+func TestTextSelectorNeverResolvesNonRenderedSourceText(t *testing.T) {
+ ctx := newSelectorFixture(t)
+
+ for _, raw := range []string{
+ "text:Publish draft",
+ "first:text:Publish draft",
+ "last:text:Publish draft",
+ "nth:0:text:Publish draft",
+ } {
+ got := describeNode(t, ctx, resolve(t, ctx, raw))
+ for _, forbidden := range []string{"style", "script", "noscript", "template"} {
+ if got == forbidden || len(got) >= len(forbidden) && got[:len(forbidden)] == forbidden {
+ t.Errorf("%s resolved to %s — source text is not rendered text", raw, got)
+ }
+ }
+ }
+}
+
+// semanticWeight came from the deleted resolver and had to be carried over, or
+// unification would make the common case worse than it was.
+//
+// The fixture has to make the two rules disagree to pin the second one. #wrap
+// loses to #inner on subtree size alone, so a wrapping div proves nothing about
+// semantic weight; #decoy is a bare div with the same label and the same zero
+// descendants, so it ties #inner on size and only semanticWeight breaks it —
+// and #decoy comes first in the document, so DOM order alone picks it.
+func TestTextSelectorPrefersControlOverPlainElementOfEqualSize(t *testing.T) {
+ ctx := newSelectorFixture(t)
+
+ want := resolve(t, ctx, "css:#inner")
+ for _, raw := range []string{"text:Archive record", "first:text:Archive record"} {
+ if got := resolve(t, ctx, raw); got != want {
+ t.Errorf("%s resolved to %s, want button#inner (%s)",
+ raw, describeNode(t, ctx, got), describeNode(t, ctx, want))
+ }
+ }
+
+ // The wrapper is a candidate the size rule must have already discarded.
+ if got := resolve(t, ctx, "text:Archive record"); got == resolve(t, ctx, "css:#wrap") {
+ t.Error("text:Archive record resolved to the wrapping div, not the button inside it")
+ }
+}
+
+// The deadline used to live in the deleted text resolver, so the wrapped forms
+// never had one. resolveSelectorAt is now the only way any css/xpath/text
+// selector is resolved — plain or wrapped, frame-scoped or dialog-scoped — so
+// bounding it here bounds every text path.
+func TestResolveSelectorAtBoundsTextLookupsOnly(t *testing.T) {
+ for _, tc := range []struct {
+ kind selector.Kind
+ bounded bool
+ }{
+ {selector.KindText, true},
+ {selector.KindCSS, false},
+ {selector.KindXPath, false},
+ } {
+ var deadline time.Time
+ var hasDeadline bool
+ _, err := resolveSelectorAt(context.Background(), selector.Selector{Kind: tc.kind, Value: "x"}, 0, false, "",
+ func(ctx context.Context, _ []map[string]any) (int64, error) {
+ deadline, hasDeadline = ctx.Deadline()
+ return 7, nil
+ })
+ if err != nil {
+ t.Fatalf("%s: %v", tc.kind, err)
+ }
+ if hasDeadline != tc.bounded {
+ t.Errorf("%s selector: deadline present = %v, want %v", tc.kind, hasDeadline, tc.bounded)
+ continue
+ }
+ if tc.bounded && time.Until(deadline) > textLookupDeadline {
+ t.Errorf("%s selector: deadline is %s away, want at most %s", tc.kind, time.Until(deadline), textLookupDeadline)
+ }
+ }
+}
+
+// Kinds resolved elsewhere must still be rejected rather than reaching the
+// browser with a kind the resolver's switch does not implement.
+func TestResolveSelectorAtRejectsUnwrappableKinds(t *testing.T) {
+ called := false
+ _, err := resolveSelectorAt(context.Background(), selector.Selector{Kind: selector.KindSemantic, Value: "Save"}, 0, false, "",
+ func(context.Context, []map[string]any) (int64, error) {
+ called = true
+ return 0, nil
+ })
+ if err == nil {
+ t.Fatal("semantic selector should not be resolvable through the unified resolver")
+ }
+ if called {
+ t.Error("an unsupported kind must be rejected before the browser is asked")
+ }
+}
+
+// document.evaluate cannot cross a shadow boundary. XPath gains consistency and
+// isolated-world execution from the unification; it does not gain shadow
+// support, and this pins that boundary rather than leaving it ambiguous.
+func TestXPathDoesNotPierceShadowRoots(t *testing.T) {
+ ctx := newSelectorFixture(t)
+
+ if nodeID, err := ResolveUnifiedSelector(ctx, selector.Parse(`xpath://button[@id="shadowbtn"]`), nil); err == nil {
+ t.Fatalf("xpath resolved a shadow-root element to %s; document.evaluate cannot cross shadow boundaries",
+ describeNode(t, ctx, nodeID))
+ }
+
+ // The same XPath against a light-DOM element still works, so the failure
+ // above is the shadow boundary and not a broken XPath path.
+ light := resolve(t, ctx, `xpath://button[@id="real"]`)
+ if want := resolve(t, ctx, "css:#real"); light != want {
+ t.Errorf("xpath light-DOM lookup resolved to %s, want button#real (%s)",
+ describeNode(t, ctx, light), describeNode(t, ctx, want))
+ }
+}