Skip to content

Automate browser based workflows with AI

Task V3: address form controls inside repeated components by anchoring on their host (#8261)

pedrohsdbe8234fd52c17
diff --git a/skyvern/forge/taskv3/tools.py b/skyvern/forge/taskv3/tools.py
index 172f57990..32ec8bcc8 100644
--- a/skyvern/forge/taskv3/tools.py
+++ b/skyvern/forge/taskv3/tools.py
@@ -205,7 +205,7 @@ _FIND_SUGGESTION_JS = (
const want = toks(args.value || '');
pQSA('[data-tv3-sugg]').forEach((e) => e.removeAttribute('data-tv3-sugg'));
if (!want.size || !preReady()) return null;
- const field = pQS(args.field);
+ const field = pQS(args.field) || (args.el && args.el.isConnected ? args.el : null);
// No field means no geometry gate, and without it the scan below is page-wide and will happily
// tag -- and then click -- a row far from the control the caller typed into. Refuse instead:
// "cannot judge" and "nothing reacted" are both safe, and a confident wrong click is not.
@@ -267,17 +267,20 @@ _VERIFY_COMMIT_JS = (
+ r"""
const toks = (s) => new Set(String(s).toLowerCase().replace(/[\/,]/g, ' ').split(/\s+/).filter((w) => w.length >= 3));
const overlaps = (a, b) => { const B = toks(b); for (const w of toks(a)) if (B.has(w)) return true; return false; };
- const el = pQS(args.field);
+ const el = pQS(args.field) || (args.el && args.el.isConnected ? args.el : null);
+ // null (not '') when there is nothing to read: the caller must tell "read it, no commit" from
+ // "could not read it", and a later second probe would answer about a different instant.
+ if (!el) return null;
const typed = String(args.typed || '').trim();
const chosen = String(args.chosen || '').trim() || typed;
- const cur = el ? (el.value || '').trim() : '';
+ const cur = (el.value || '').trim();
const tagged = pQS('[data-tv3-sugg]');
const listClosed = !tagged || tagged.getBoundingClientRect().height === 0;
// A short normalized value ("New York" -> "NY", "United States" -> "US") has no >=3-char token to
// overlap, so accept it on causality alone (it changed / the list closed). Longer values must still
// relate to the chosen suggestion so an unrelated change can't read as a successful commit.
if (cur && (cur !== typed || listClosed) && (toks(cur).size === 0 || overlaps(cur, chosen) || overlaps(cur, typed))) return cur;
- const cont = el ? el.closest('div,li,fieldset') : null;
+ const cont = el.closest('div,li,fieldset');
if (cont) {
for (const h of cont.querySelectorAll('input[type=hidden]')) {
const v = (h.value || '').trim();
@@ -299,17 +302,17 @@ _VERIFY_COMMIT_JS = (
# Both readings happen in ONE evaluation: as two round trips, an ordinary re-render landing between
# them lets each describe a different moment.
_PROBE_REACH_JS = (
- r"""(sel) => {
+ r"""(arg) => {
const _roots = """
+ _SHADOW_ROOTS_JS
+ r""";
try {
- if (document.querySelector(sel)) return '';
+ if (document.querySelector(arg.sel)) return '';
} catch (e) { return 'unprobeable'; }
try {
- for (const root of _roots(document)) { if (root.querySelector(sel)) return 'component'; }
+ for (const root of _roots(document)) { if (root.querySelector(arg.sel)) return 'component'; }
} catch (e) { return ''; }
- return '';
+ return (arg.el && arg.el.isConnected ? arg.el : null) ? 'component' : '';
}"""
)
@@ -318,13 +321,15 @@ _PROBE_REACH_JS = (
# Pierces open shadow roots because the caller compares against what `page.click` would resolve, and
# a document-only probe reports "gone" for every element a web component renders.
_SELECTOR_EXISTS_JS = (
- r"""(sel) => {
+ r"""(arg) => {
const _roots = """
+ _SHADOW_ROOTS_JS
+ r""";
try {
let found = null;
- for (const root of _roots(document)) { found = root.querySelector(sel); if (found) break; }
+ for (const root of _roots(document)) { found = root.querySelector(arg.sel); if (found) break; }
+ // The executor can resolve a selector no single root can match; that is existence too.
+ if (!found) found = (arg.el && arg.el.isConnected ? arg.el : null);
return !!found;
} catch (e) { return true; }
}"""
@@ -385,7 +390,11 @@ _ROOT_QUERY_JS = (
# actionability wait never resolves against it, so select_option probes visibility first and only
# forces past actionability when the element exists but is genuinely hidden this way.
_SELECT_VISIBILITY_JS = (
- r"""(sel) => {
+ r"""(arg) => {
+ const sel = arg.sel;
+ // A node the page replaced between the executor's lookup and this evaluate is not evidence about
+ // the live page: reading a detached one reports a stale value as a current verdict.
+ const _executorEl = arg.el && arg.el.isConnected ? arg.el : null;
const _visibleProxy = """
+ _VISIBLE_PROXY_JS
+ r""";
@@ -393,7 +402,7 @@ _SELECT_VISIBILITY_JS = (
+ _ROOT_QUERY_JS
+ r""";
try {
- const el = _q.find(sel);
+ const el = _q.find(sel) || _executorEl;
if (!el) return { exists: false, visible: false };
const r = el.getBoundingClientRect();
const cs = getComputedStyle(el);
@@ -412,12 +421,16 @@ _SELECT_VISIBILITY_JS = (
# Read back after a forced select_option so a styled proxy that silently didn't sync from its
# native control is caught rather than reported as a successful selection.
_SELECT_READBACK_JS = (
- r"""(sel) => {
+ r"""(arg) => {
+ const sel = arg.sel;
+ // A node the page replaced between the executor's lookup and this evaluate is not evidence about
+ // the live page: reading a detached one reports a stale value as a current verdict.
+ const _executorEl = arg.el && arg.el.isConnected ? arg.el : null;
const _q = """
+ _ROOT_QUERY_JS
+ r""";
try {
- const el = _q.find(sel);
+ const el = _q.find(sel) || _executorEl;
if (!el) return null;
const idx = el.selectedIndex;
const opt = idx >= 0 ? el.options[idx] : null;
@@ -430,7 +443,11 @@ _SELECT_READBACK_JS = (
# A skinned checkbox/radio is a zero-size or invisible native input whose visible <label> is the
# real click target; the label is tagged (stale tags cleared first) so click can act on it.
_SKINNED_CHECKBOX_PROBE_JS = (
- r"""(sel) => {
+ r"""(arg) => {
+ const sel = arg.sel;
+ // A node the page replaced between the executor's lookup and this evaluate is not evidence about
+ // the live page: reading a detached one reports a stale value as a current verdict.
+ const _executorEl = arg.el && arg.el.isConnected ? arg.el : null;
const _visibleProxy = """
+ _VISIBLE_PROXY_JS
+ r""";
@@ -441,7 +458,7 @@ _SKINNED_CHECKBOX_PROBE_JS = (
// Cleared across roots, not just the document: a tag left inside a component would still be
// matched by the executor's piercing engine and clicked as though it were this call's proxy.
_q.all('[data-tv3-proxy]').forEach((e) => e.removeAttribute('data-tv3-proxy'));
- const el = _q.find(sel);
+ const el = _q.find(sel) || _executorEl;
if (!el) return { exists: false, skinned: false, labelTagged: false };
const type = String(el.type || '').toLowerCase();
if (el.tagName === 'INPUT' && type === 'file') return { exists: true, skinned: false, labelTagged: false, file: true };
@@ -476,11 +493,15 @@ _SKINNED_CHECKBOX_PROBE_JS = (
# Read twice (before and after the forced click): a proxy that does not sync from its native input
# must fail loud, not read as a successful toggle.
_CHECKBOX_CHECKED_JS = (
- r"""(sel) => {
+ r"""(arg) => {
+ const sel = arg.sel;
+ // A node the page replaced between the executor's lookup and this evaluate is not evidence about
+ // the live page: reading a detached one reports a stale value as a current verdict.
+ const _executorEl = arg.el && arg.el.isConnected ? arg.el : null;
const _q = """
+ _ROOT_QUERY_JS
+ r""";
- try { const el = _q.find(sel); return el ? !!el.checked : null; } catch (e) { return null; }
+ try { const el = _q.find(sel) || _executorEl; return el ? !!el.checked : null; } catch (e) { return null; }
}"""
)
@@ -490,7 +511,8 @@ _CHECKBOX_CHECKED_JS = (
# click on a multi-select menu (which commits WITHOUT closing) can be verified by its state change.
# Also takes the visible-DOM pre-snapshot (data-tv3-pre) so a menu the click opens reads as a reaction.
_CLICK_PRECHECK_JS = (
- r"""(clicked) => {"""
+ r"""(arg) => {
+ const clicked = arg.sel;"""
+ _PIERCED_QUERY_JS
+ r"""
const vis = (el) => {
@@ -518,7 +540,7 @@ _CLICK_PRECHECK_JS = (
const openRows = [];
for (const el of pQSA('[data-tv3-menu]')) if (vis(el)) openRows.push(el);
let target = null;
- try { target = pQS(clicked); } catch (e) { target = null; }
+ try { target = pQS(clicked) || (arg.el && arg.el.isConnected ? arg.el : null); } catch (e) { target = (arg.el && arg.el.isConnected ? arg.el : null); }
let isOption = false;
let containsMenu = false;
let optText = '';
@@ -547,7 +569,8 @@ _CLICK_PRECHECK_JS = (
# destroyed or hidden — reads 0), plus the clicked row's current state fingerprint for the multi-select
# commit check. Field names are distinct from _CLICK_PRECHECK_JS's on purpose (tests dispatch on them).
_MENU_AFTER_JS = (
- r"""(clicked) => {"""
+ r"""(arg) => {
+ const clicked = arg.sel;"""
+ _PIERCED_QUERY_JS
+ r"""
const vis = (el) => {
@@ -574,7 +597,7 @@ _MENU_AFTER_JS = (
const rows = [];
for (const el of pQSA('[data-tv3-menu]')) if (vis(el)) { stillOpen++; rows.push(el); }
let target = null;
- try { target = pQS(clicked); } catch (e) { target = null; }
+ try { target = pQS(clicked) || (arg.el && arg.el.isConnected ? arg.el : null); } catch (e) { target = (arg.el && arg.el.isConnected ? arg.el : null); }
let optState = '';
if (target) {
for (const el of rows) {
@@ -595,12 +618,13 @@ _MENU_AFTER_JS = (
# stay valid until the menu re-renders, so the model can pick an option without a re-observe re-minting
# ids (the staging trace's staleness trap). Existing tags are cleared only when a new menu is tagged.
_FIND_MENU_JS = (
- r"""(clicked) => {"""
+ r"""(arg) => {
+ const clicked = arg.sel;"""
+ _PIERCED_QUERY_JS
+ r"""
const vis = (r) => r.width > 0 && r.height > 0;
let trigger = null;
- try { trigger = pQS(clicked); } catch (e) { return null; }
+ try { trigger = pQS(clicked) || (arg.el && arg.el.isConnected ? arg.el : null); } catch (e) { return null; }
if (!trigger) return null;
// The reaction gate below is the whole basis for calling these rows a menu the click just opened.
// A navigation destroys window, so an absent snapshot here means the page under us is not the page
@@ -980,6 +1004,68 @@ _OBSERVE_JS = (
takenMarkers.add(m);
return attr('data-tv3', m);
};
+ // Every scope the executor searches under a host: the host's own light subtree, the root it owns,
+ // and any root nested beneath either. A descendant combinator is shadow-transparent to the
+ // executor, so content SLOTTED into the component matches `#host #ctrl` too -- counting the root
+ // alone undercounts, and an undercount is what hands out a selector that denotes two elements.
+ const hostScopes = (host) => {
+ const scopes = [host];
+ const stack = [host];
+ const own = host.shadowRoot;
+ if (own && own.nodeType === 11) { scopes.push(own); stack.push(own); }
+ while (stack.length) {
+ const scope = stack.pop();
+ let kids;
+ try { kids = scope.querySelectorAll('*'); } catch (e) { return null; }
+ for (const k of kids) {
+ let sr = null;
+ try { sr = k.shadowRoot; } catch (e) { return null; }
+ if (sr && sr.nodeType === 11 && scopes.indexOf(sr) === -1) { scopes.push(sr); stack.push(sr); }
+ }
+ }
+ return scopes;
+ };
+ // resolvesTo, scoped to one host. Same shape and same `found === target` guarantee: whatever the
+ // string turns out to select under this host, it is accepted only if the one element it selects
+ // is THIS one.
+ const scopedResolvesTo = (host, s, target) => {
+ const scopes = hostScopes(host);
+ if (!scopes) return false;
+ let found = null;
+ let n = 0;
+ for (const scope of scopes) {
+ let hits;
+ try { hits = scope.querySelectorAll(s); } catch (e) { return false; }
+ n += hits.length;
+ if (n > 1) return false;
+ if (hits.length === 1) found = hits[0];
+ }
+ return n === 1 && found === target;
+ };
+ // Shadow encapsulation scopes ids to their own root, so a design system reuses one internal id in
+ // every instance and no unscoped selector can single one out. The host itself is outside the root
+ // it owns, so it can be named the ordinary way, and anchoring on it scopes the reused id without
+ // writing anything into the component.
+ const hostAnchored = (el, host) => {
+ if (!host || !el.id) return null;
+ const raw = String(el.id);
+ if (_FORGEABLE.test(raw)) return null;
+ const esc = window.CSS && CSS.escape ? CSS.escape(raw) : null;
+ const ctrl = esc === raw && raw === raw.trimEnd() ? '#' + esc : attr('id', raw);
+ if (!scopedResolvesTo(host, ctrl, el)) return null;
+ // naturalSelector reports its cause through shared state; the control's own cause is already
+ // settled by the time we get here and must survive naming the host.
+ const why = naturalWhy;
+ const inconclusive = checkInconclusive;
+ const hostSel = naturalSelector(host);
+ naturalWhy = why;
+ checkInconclusive = inconclusive;
+ // A host we could not name is left alone rather than marked. Minting here would hand out a
+ // handle on the one page where uniqueness could not be checked -- an unreadable root makes the
+ // host's own count inconclusive, and mintOn keeps a marker it could not verify.
+ if (!hostSel) return null;
+ return hostSel + ' ' + ctrl;
+ };
// Controls inside a component that we could not name, split by CAUSE: these need different
// fixes, and one merged tally would send the follow-up after the wrong one.
// anonymous -- no id/name/data-testid at all
@@ -987,9 +1073,8 @@ _OBSERVE_JS = (
// cross-root count is >1 and no unscoped selector can single this one out
// unverifiable -- has one, but a root threw, so the count could not be taken
// unsafe -- has one carrying a character that could forge a payload line
- // A host-anchored selector (`#host #ctrl`) is the fix for the first two. It is not built here,
- // but it IS verifiable in page: a unique host selector plus `#ctrl` occurring exactly once across
- // the host's own root and every root nested under it. See SKY-14710.
+ // A duplicated ID is recovered by anchoring on the host (`#host #ctrl`), which is the shape a
+ // design system produces; a name or testid reused across instances is not, and neither is the rest.
// Records named by a marker we wrote, re-checked after the walk: a later element can mutate an
// earlier one, and an element's own attributeChangedCallback can move our marker onto a peer.
const mintedOn = [];
@@ -1044,15 +1129,19 @@ _OBSERVE_JS = (
// the base engine terminated cleanly. Not writing restores that behavior exactly. A control
// with an id, name or data-testid of its own is unaffected, which is the ordinary case.
if (host) {
- if (naturalWhy === 'duplicated') unnamedDuplicated++;
- else if (naturalWhy === 'unverifiable') unnamedUnverifiable++;
- else if (naturalWhy === 'unsafe') unnamedUnsafe++;
- else unnamedAnonymous++;
- continue;
+ if (naturalWhy === 'duplicated') selector = hostAnchored(el, host);
+ if (!selector) {
+ if (naturalWhy === 'duplicated') unnamedDuplicated++;
+ else if (naturalWhy === 'unverifiable') unnamedUnverifiable++;
+ else if (naturalWhy === 'unsafe') unnamedUnsafe++;
+ else unnamedAnonymous++;
+ continue;
+ }
+ } else {
+ selector = mintOn(el);
+ if (!selector) { dropped++; continue; }
+ mintedValue = el.getAttribute('data-tv3');
}
- selector = mintOn(el);
- if (!selector) { dropped++; continue; }
- mintedValue = el.getAttribute('data-tv3');
}
let label = (el.getAttribute('aria-label') || el.getAttribute('placeholder') || '').trim();
if (!label && el.labels && el.labels[0]) label = (el.labels[0].innerText || '').trim();
@@ -1593,6 +1682,20 @@ def build_browser_tools(
"it (the section header, the step, the modal trigger), then re-observe."
)
+ async def _probe_arg(page: Any, selector: str) -> dict[str, Any]:
+ # Probes resolve per root, which cannot match a host-anchored selector whose two halves
+ # straddle a shadow boundary. The executor's own engine can, so it supplies the element the
+ # action will actually land on -- consulted only where the per-root lookup finds nothing.
+ # Only a composed selector needs it, and hostAnchored composes with a space, so anything
+ # without one keeps its single round trip. A quoted space costs a spare lookup, never a miss.
+ if " " not in selector:
+ return {"sel": selector, "el": None}
+ try:
+ element = await page.query_selector(selector)
+ except Exception:
+ element = None
+ return {"sel": selector, "el": element}
+
async def _clear_proxy_tags(page: Any) -> None:
try:
await page.evaluate(
@@ -1623,7 +1726,7 @@ def build_browser_tools(
# a live price) keeps moving. Only a state that holds across two reads is evidence.
await asyncio.sleep(0.15)
try:
- again_raw = await page.evaluate(_MENU_AFTER_JS, selector)
+ again_raw = await page.evaluate(_MENU_AFTER_JS, await _probe_arg(page, selector))
except Exception:
return True
again = again_raw if isinstance(again_raw, dict) else {}
@@ -1633,7 +1736,7 @@ def build_browser_tools(
if url_before and url_now and url_now != url_before:
return f"Selected option {opt!r} — the page navigated.", None
try:
- after_raw = await page.evaluate(_MENU_AFTER_JS, selector)
+ after_raw = await page.evaluate(_MENU_AFTER_JS, await _probe_arg(page, selector))
except Exception:
# the page tearing down right after an option click is a navigation — commit evidence
return f"Selected option {opt!r} — the page navigated.", None
@@ -1647,7 +1750,7 @@ def build_browser_tools(
# bounded settle, only on this would-be-error path.
await asyncio.sleep(0.6)
try:
- settled_raw = await page.evaluate(_MENU_AFTER_JS, selector)
+ settled_raw = await page.evaluate(_MENU_AFTER_JS, await _probe_arg(page, selector))
except Exception:
return f"Selected option {opt!r} — the page navigated.", None
settled = settled_raw if isinstance(settled_raw, dict) else {}
@@ -1658,7 +1761,7 @@ def build_browser_tools(
# No-commit evidence is already established: a crash of this last informational probe must
# not fall through to the caller's fail-open bare ok.
try:
- submenu = await page.evaluate(_FIND_MENU_JS, selector)
+ submenu = await page.evaluate(_FIND_MENU_JS, await _probe_arg(page, selector))
except Exception:
submenu = None
if isinstance(submenu, dict) and submenu.get("count"):
@@ -1674,10 +1777,10 @@ def build_browser_tools(
# arbitrary row, so any open/closed/selected claim could be false. Say nothing.
return None, None
try:
- after_raw = await page.evaluate(_MENU_AFTER_JS, selector)
+ after_raw = await page.evaluate(_MENU_AFTER_JS, await _probe_arg(page, selector))
except Exception:
return None, None
- found = await page.evaluate(_FIND_MENU_JS, selector)
+ found = await page.evaluate(_FIND_MENU_JS, await _probe_arg(page, selector))
if isinstance(found, dict) and found.get("count"):
return _menu_open_note(found, selector), None
if isinstance(after_raw, dict) and not after_raw.get("stillOpen"):
@@ -1686,7 +1789,7 @@ def build_browser_tools(
'an option\'s [data-tv3-menu="N"] selector while the menu is open.'
), None
return None, None
- found = await page.evaluate(_FIND_MENU_JS, selector)
+ found = await page.evaluate(_FIND_MENU_JS, await _probe_arg(page, selector))
if isinstance(found, dict) and found.get("count"):
return _menu_open_note(found, selector), None
return None, None
@@ -1699,7 +1802,7 @@ def build_browser_tools(
if _TV3_MARKER_SELECTOR_RE.match(selector.strip()):
missing = False
try:
- missing = not await page.evaluate(_SELECTOR_EXISTS_JS, selector)
+ missing = not await page.evaluate(_SELECTOR_EXISTS_JS, await _probe_arg(page, selector))
except Exception:
missing = False
if missing:
@@ -1716,7 +1819,7 @@ def build_browser_tools(
)
pre: dict[str, Any] | None = None
try:
- pre_raw = await page.evaluate(_CLICK_PRECHECK_JS, selector)
+ pre_raw = await page.evaluate(_CLICK_PRECHECK_JS, await _probe_arg(page, selector))
if isinstance(pre_raw, dict):
pre = pre_raw
except Exception:
@@ -1727,14 +1830,17 @@ def build_browser_tools(
# "its state changed" commit evidence on a no-op click.
try:
await page.hover(selector, timeout=2000)
- hovered = await page.evaluate(_MENU_AFTER_JS, selector)
+ hovered = await page.evaluate(_MENU_AFTER_JS, await _probe_arg(page, selector))
if isinstance(hovered, dict) and hovered.get("optState"):
pre["optState"] = hovered["optState"]
except Exception:
pass
url_before = await _url(page)
+ # One resolution for the whole pre-click phase: these run back to back with no mutation
+ # between them, so re-asking the executor per probe would only buy round trips.
+ pre_click_arg = await _probe_arg(page, selector)
try:
- skin_probe = await page.evaluate(_SKINNED_CHECKBOX_PROBE_JS, selector)
+ skin_probe = await page.evaluate(_SKINNED_CHECKBOX_PROBE_JS, pre_click_arg)
except Exception:
skin_probe = None
if isinstance(skin_probe, dict) and skin_probe.get("file"):
@@ -1765,7 +1871,7 @@ def build_browser_tools(
await _clear_proxy_tags(page)
return ToolResult.error(f"{selector} is disabled — it cannot be toggled until the page enables it")
try:
- checked_before = await page.evaluate(_CHECKBOX_CHECKED_JS, selector)
+ checked_before = await page.evaluate(_CHECKBOX_CHECKED_JS, pre_click_arg)
except Exception:
checked_before = None
if checked_before is True and skin_probe.get("radio"):
@@ -1785,10 +1891,22 @@ def build_browser_tools(
await _clear_proxy_tags(page)
base = f"clicked {selector} via its label — now at {await _url(page)}"
elif skinned:
- await page.evaluate(
- "(sel) => { const _q = " + _ROOT_QUERY_JS + "; const el = _q.find(sel); if (el) el.click(); }",
- selector,
+ # Resolved again here rather than reused: this evaluate is the action, not a probe, and a
+ # selector naming a control through its host resolves ONLY through the handle -- a node the
+ # page replaced while the probes ran would be clicked off-document, silently.
+ fired = await page.evaluate(
+ "(arg) => { const _q = "
+ + _ROOT_QUERY_JS
+ + "; const el = _q.find(arg.sel) || arg.el;"
+ + " if (!el || !el.isConnected) return false; el.click(); return true; }",
+ await _probe_arg(page, selector),
)
+ if not fired:
+ await _clear_proxy_tags(page)
+ return ToolResult.error(
+ f"{selector} left the page before the click could land — it was replaced by a "
+ "re-render; re-observe and act on fresh selectors"
+ )
base = f"clicked {selector} (hidden native control, toggled directly) — now at {await _url(page)}"
else:
try:
@@ -1796,7 +1914,7 @@ def build_browser_tools(
except Exception as e:
gone = False
try:
- gone = not await page.evaluate(_SELECTOR_EXISTS_JS, selector)
+ gone = not await page.evaluate(_SELECTOR_EXISTS_JS, await _probe_arg(page, selector))
except Exception:
gone = False
if gone:
@@ -1810,7 +1928,7 @@ def build_browser_tools(
if skinned:
try:
- checked_after = await page.evaluate(_CHECKBOX_CHECKED_JS, selector)
+ checked_after = await page.evaluate(_CHECKBOX_CHECKED_JS, await _probe_arg(page, selector))
except Exception:
checked_after = None
if checked_after is None:
@@ -1844,7 +1962,9 @@ def build_browser_tools(
await page.hover(selector, timeout=15000)
return ToolResult.ok(f"hovered {selector}")
- async def _commit_typeahead(page: Any, selector: str, value: str, rounds: int) -> tuple[str | None, str | None]:
+ async def _commit_typeahead(
+ page: Any, selector: str, value: str, rounds: int
+ ) -> tuple[str | None, str | None, bool]:
# Poll for the suggestion list rendered IN REACTION to the value already typed into `selector`,
# click the best match, and verify the field committed. Site-agnostic (see _FIND_SUGGESTION_JS).
# Returns (committed_value, suggestion_text): suggestion_text is None when no suggestion ever
@@ -1854,7 +1974,10 @@ def build_browser_tools(
for _ in range(rounds):
await asyncio.sleep(0.4)
try:
- found = await page.evaluate(_FIND_SUGGESTION_JS, {"value": value, "field": selector})
+ found = await page.evaluate(
+ _FIND_SUGGESTION_JS,
+ {"value": value, "field": selector, "el": (await _probe_arg(page, selector))["el"]},
+ )
except Exception as e:
LOG.debug("taskv3 typeahead suggestion-find failed", selector=selector, error=str(e))
found = None
@@ -1862,7 +1985,7 @@ def build_browser_tools(
best_txt = str(found["text"])
break
if not best_txt:
- return None, None
+ return None, None, False
# Click the tagged best row. If the list re-rendered and dropped the tag, re-find (re-tag the
# current best) and click once more — never blind-press ArrowDown/Enter, which would commit
# whichever row the widget happens to highlight rather than the one we actually scored.
@@ -1872,7 +1995,10 @@ def build_browser_tools(
clicked = True
except Exception:
try:
- refound = await page.evaluate(_FIND_SUGGESTION_JS, {"value": value, "field": selector})
+ refound = await page.evaluate(
+ _FIND_SUGGESTION_JS,
+ {"value": value, "field": selector, "el": (await _probe_arg(page, selector))["el"]},
+ )
if isinstance(refound, dict) and refound.get("text"):
best_txt = str(refound["text"])
await page.click('[data-tv3-sugg="1"]', timeout=3000)
@@ -1882,18 +2008,30 @@ def build_browser_tools(
if not clicked:
# a suggestion surfaced but we couldn't click it — report un-committed, don't guess
LOG.debug("taskv3 typeahead could not click suggestion", selector=selector, suggestion=best_txt)
- return None, best_txt
+ return None, best_txt, False
await asyncio.sleep(0.3)
+ readable = False
try:
- committed = (
- (await page.evaluate(_VERIFY_COMMIT_JS, {"field": selector, "typed": value, "chosen": best_txt})) or ""
- ).strip()
+ read = await page.evaluate(
+ _VERIFY_COMMIT_JS,
+ {
+ "field": selector,
+ "typed": value,
+ "chosen": best_txt,
+ "el": (await _probe_arg(page, selector))["el"],
+ },
+ )
+ # null means the field could not be read at all; '' means it was read and holds nothing.
+ readable = read is not None
+ committed = str(read or "").strip()
except Exception as e:
LOG.debug("taskv3 typeahead commit-verify failed", selector=selector, error=str(e))
committed = ""
- return (committed or None), best_txt
+ return (committed or None), best_txt, readable
- async def _type_and_commit(page: Any, selector: str, value: str, rounds: int) -> tuple[str | None, str | None]:
+ async def _type_and_commit(
+ page: Any, selector: str, value: str, rounds: int
+ ) -> tuple[str | None, str | None, bool]:
# Keystroke-type (so a widget's async suggestion fetch fires on real key events). Snapshot the
# visible DOM just before typing so the finder treats only NEW/reacting nodes as suggestions —
# static page text that merely shares a word with the value can't be mistaken for one.
@@ -1910,7 +2048,7 @@ def build_browser_tools(
# Without the pre-snapshot the reaction-gate can't tell a new suggestion from static page
# text, so don't run the finder ungated (it could click unrelated content) — leave the typed
# value and let the caller re-observe.
- return None, None
+ return None, None, False
return await _commit_typeahead(page, selector, value, rounds)
# Input kinds that are never typeaheads — skip the suggestion probe (and its latency) for these.
@@ -1940,7 +2078,7 @@ def build_browser_tools(
# and a selector our probe cannot parse. Empty on failure -- claiming a reason we did not
# establish would be its own false statement.
try:
- reach = str(await page.evaluate(_PROBE_REACH_JS, selector) or "")
+ reach = str(await page.evaluate(_PROBE_REACH_JS, await _probe_arg(page, selector)) or "")
except Exception:
return None
if reach == "component":
@@ -1976,18 +2114,17 @@ def build_browser_tools(
if text and clear and not press_enter and await _field_type(page, selector) not in _NON_TYPEAHEAD_TYPES:
# keystroke-type (via _type_and_commit) so a widget that fetches suggestions on key events —
# not just on a single `input` from fill — still surfaces them, then commit the best match.
- committed, opt_txt = await _type_and_commit(page, selector, text, rounds=3)
+ committed, opt_txt, readable = await _type_and_commit(page, selector, text, rounds=3)
if opt_txt and committed:
return ToolResult.ok(
f"typed into {selector}; it is a typeahead — selected {opt_txt!r} (committed value: {committed!r})"
)
if opt_txt and not committed:
- # The verifier pierces open shadow roots, so inside a component it does read the
- # field -- but a widget that portals its list elsewhere in the page, or renders it in
- # a closed root, is still beyond it, and "did not commit" would then be its own
- # unearned claim on a field that is often filled. Halting a batch on that is as wrong
- # as proceeding on a real failure, so say which one we actually established.
- why = await _unverifiable_because(page, selector)
+ # The verifier pierces open roots and also reads the element the executor resolved,
+ # so inside a component the failure is established rather than guessed. A list portaled
+ # elsewhere, or a field in a closed root, is still beyond both -- and that is exactly
+ # what the read reports by returning nothing, so the softening follows the read.
+ why = None if readable else await _unverifiable_because(page, selector)
if why:
return ToolResult.ok(
f"clicked suggestion {opt_txt!r} for {selector}; {why}, so the commit could not "
@@ -2028,7 +2165,7 @@ def build_browser_tools(
label = args.get("label")
value = args.get("value")
try:
- probe = await page.evaluate(_SELECT_VISIBILITY_JS, selector)
+ probe = await page.evaluate(_SELECT_VISIBILITY_JS, await _probe_arg(page, selector))
except Exception:
probe = None
# force bypasses actionability for a select a design system hides behind a styled proxy;
@@ -2045,7 +2182,7 @@ def build_browser_tools(
if not force:
return ToolResult.ok(f"selected on {selector}")
try:
- readback = await page.evaluate(_SELECT_READBACK_JS, selector)
+ readback = await page.evaluate(_SELECT_READBACK_JS, await _probe_arg(page, selector))
except Exception:
readback = None
committed = False
@@ -2153,7 +2290,7 @@ def build_browser_tools(
return error
selector = args["selector"]
value = _resolve_text(args["value"])
- committed, opt_txt = await _type_and_commit(page, selector, value, rounds=8)
+ committed, opt_txt, readable = await _type_and_commit(page, selector, value, rounds=8)
if opt_txt is None:
# The suggestion finder pierces open shadow roots, so inside a component it now sees a
# list rendered in that root -- but a portalled or closed-root list stays invisible, so a
@@ -2173,7 +2310,7 @@ def build_browser_tools(
"— do not assume success or move on as if it were"
)
if not committed:
- why = await _unverifiable_because(page, selector)
+ why = None if readable else await _unverifiable_because(page, selector)
if why:
return ToolResult.ok(
f"selected {opt_txt!r} for {selector}; {why}, so the commit could not be verified — "
diff --git a/tests/unit/test_taskv3_tools.py b/tests/unit/test_taskv3_tools.py
index 3d0d535ab..fdd279498 100644
--- a/tests/unit/test_taskv3_tools.py
+++ b/tests/unit/test_taskv3_tools.py
@@ -2842,23 +2842,20 @@ async def test_observe_lists_skinned_checkbox_with_visible_label() -> None:
@_skip_no_browser
@pytest.mark.asyncio
async def test_observe_reports_a_reused_component_id_as_reused_not_as_anonymous() -> None:
- # The singleton fixture above only proves observe can name a component control with its own id;
- # the dominant real case is a form built from the SAME component twice (First name, Last name),
- # and a design system hard-codes the same internal id in every instance because shadow
- # encapsulation scopes ids to their own root. Both instances resolve to a cross-root count of 2,
- # so both are dropped — but the note must say "reused", not "no id of their own", because saying
- # the latter about a control that DOES have an id misdescribes the page.
+ # A design system hard-codes the same internal id in every instance, because shadow encapsulation
+ # scopes ids to their own root, so both instances resolve to a cross-root count of 2. Anchoring on
+ # the host recovers that whenever the host can be named; these hosts deliberately cannot be, which
+ # is what leaves the controls unnamed and keeps this the case the note has to describe correctly.
+ # The note must say "reused", not "no id of their own", because saying the latter about a control
+ # that DOES have an id misdescribes the page.
async with _live_page(
- """<ds-form-field id="ff1"></ds-form-field>
- <ds-form-field id="ff2"></ds-form-field>
+ """<ds-form-field></ds-form-field>
+ <ds-form-field></ds-form-field>
<script>
- function openRoot(id, html) {
- var r = document.getElementById(id).attachShadow({mode: 'open'});
- r.innerHTML = html;
- return r;
+ for (const h of document.querySelectorAll('ds-form-field')) {
+ h.attachShadow({mode: 'open'}).innerHTML =
+ '<input id="first-name" name="firstName" style="width:80px;height:20px">';
}
- openRoot('ff1', '<input id="first-name" name="firstName" style="width:80px;height:20px">');
- openRoot('ff2', '<input id="first-name" name="firstName" style="width:80px;height:20px">');
</script>"""
) as page:
tools = build_browser_tools(_fixed_page_provider(page))
@@ -3648,8 +3645,8 @@ async def test_stale_id_probe_does_not_report_shadow_hosted_selector_as_gone() -
from skyvern.forge.taskv3.tools import _SELECTOR_EXISTS_JS # noqa: PLC0415
async with _shadow_page() as page:
- assert await page.evaluate(_SELECTOR_EXISTS_JS, "#first-name") is True
- assert await page.evaluate(_SELECTOR_EXISTS_JS, "#no-such-element") is False
+ assert await page.evaluate(_SELECTOR_EXISTS_JS, {"sel": "#first-name", "el": None}) is True
+ assert await page.evaluate(_SELECTOR_EXISTS_JS, {"sel": "#no-such-element", "el": None}) is False
# And at any nesting depth. The walk feeding this probe used to stop at ten roots, so a control
# below that answered "gone" and the click path failed loud on an element that was really there.
@@ -3665,7 +3662,7 @@ async def test_stale_id_probe_does_not_report_shadow_hosted_selector_as_gone() -
</script>"""
) as page:
assert await page.locator("#deep-ctrl").count() == 1, "fixture is not armed"
- assert await page.evaluate(_SELECTOR_EXISTS_JS, "#deep-ctrl") is True
+ assert await page.evaluate(_SELECTOR_EXISTS_JS, {"sel": "#deep-ctrl", "el": None}) is True
@_skip_no_browser
@@ -4085,11 +4082,11 @@ async def test_selector_exists_probe_is_not_fooled_by_a_named_getter_shadow_root
</script>"""
) as page:
# The genuine shadow-hosted control is found through the real root...
- assert await page.evaluate(_SELECTOR_EXISTS_JS, "#really-real") is True
+ assert await page.evaluate(_SELECTOR_EXISTS_JS, {"sel": "#really-real", "el": None}) is True
# ...the decoy's child is found through the document, as an ordinary element...
- assert await page.evaluate(_SELECTOR_EXISTS_JS, "#inside-the-decoy") is True
+ assert await page.evaluate(_SELECTOR_EXISTS_JS, {"sel": "#inside-the-decoy", "el": None}) is True
# ...and nothing the page can name into existence is reported as present.
- assert await page.evaluate(_SELECTOR_EXISTS_JS, "#no-such-element") is False
+ assert await page.evaluate(_SELECTOR_EXISTS_JS, {"sel": "#no-such-element", "el": None}) is False
@_skip_no_browser
@@ -4645,6 +4642,8 @@ _SHARED_ID_DROPZONES_HTML = """
<ds-drop id="dz-single"></ds-drop>
<ds-drop id="dz-a"></ds-drop>
<ds-drop id="dz-b"></ds-drop>
+ <ds-drop class="anon"></ds-drop>
+ <ds-drop class="anon"></ds-drop>
<script>
function mk(hostId, inputId, labelText) {
var r = document.getElementById(hostId).attachShadow({mode: 'open'});
@@ -4656,6 +4655,13 @@ _SHARED_ID_DROPZONES_HTML = """
mk('dz-single', 'resume-unique', 'Upload resume (only one)');
mk('dz-a', 'resume', 'Upload resume');
mk('dz-b', 'resume', 'Upload cover letter');
+ // A pair whose hosts cannot be named either, so they stay retained-but-unlisted and the note
+ // keeps having something to count that the listing does not.
+ for (const h of document.querySelectorAll('ds-drop.anon')) {
+ h.attachShadow({mode: 'open'}).innerHTML =
+ '<label for="portfolio" style="display:inline-block;width:240px;height:40px">Upload portfolio</label>' +
+ '<input type="file" id="portfolio" style="position:absolute;width:0;height:0;opacity:0">';
+ }
</script>
</body></html>
"""
@@ -4669,10 +4675,37 @@ async def test_hidden_native_note_counts_only_the_controls_it_actually_listed()
r = await _tool(tools, "observe").handler({})
assert r.status == "ok"
tagged = [ln for ln in r.content.splitlines() if "[hidden-native" in ln and not ln.startswith("note:")]
- assert len(tagged) == 1 and "resume-unique" in tagged[0], r.content
- assert "note: 1 native control(s) hidden behind styled proxies" in r.content, r.content
- # the two that share an id are dropped, and said to be dropped, rather than counted as listed
+ assert len(tagged) == 3, r.content
+ assert "note: 3 native control(s) hidden behind styled proxies" in r.content, r.content
+ # The two whose hosts have no name of their own stay unlisted, and are said to be dropped
+ # rather than counted as listed: the note claims what it printed, not what it walked.
assert "reused by another instance of the same component" in r.content
+ assert "portfolio" not in r.content, r.content
+
+
+def _printed_selectors(content: str) -> list[str]:
+ return re.findall(r"^\[(.*?)\] ", content, re.M)
+
+
+# Two instances of one component, each holding a control under the same id. Shadow encapsulation
+# scopes ids to their own root, so a design system reuses one id per instance and every flat form
+# is cross-root ambiguous. Each inner control reports which instance it belongs to, and the twin
+# carries the same listener, so a receipt naming one instance is proof the other was not touched.
+_REPEATED_COMPONENT_HTML = """
+<ds-field id="fld-a"></ds-field>
+<ds-field id="fld-b"></ds-field>
+<script>
+window.hits = [];
+window.build = function (hostId, token) {
+ var host = document.getElementById(hostId);
+ var r = host.shadowRoot || host.attachShadow({mode: 'open'});
+ r.innerHTML = '<button id="apply" style="width:90px;height:24px">Apply</button>';
+ r.querySelector('button').addEventListener('click', function () { window.hits.push(token); });
+};
+window.build('fld-a', 'a-v1');
+window.build('fld-b', 'b-v1');
+</script>
+"""
# --- Reaction probes inside open shadow roots. Perception already pierces; these cover the
@@ -4736,7 +4769,7 @@ async def test_click_precheck_recognizes_a_shadow_hosted_menu_option() -> None:
+ '<li id="o2" data-tv3-menu="2" role="option">Beta</li></ul>';
</script>"""
) as page:
- pre = await page.evaluate(_CLICK_PRECHECK_JS, '[data-tv3-menu="1"]')
+ pre = await page.evaluate(_CLICK_PRECHECK_JS, {"sel": '[data-tv3-menu="1"]', "el": None})
assert pre["isOption"] is True
assert pre["menuOpen"] is True
assert pre["optText"] == "Alpha"
@@ -4774,7 +4807,7 @@ async def test_menu_after_does_not_report_an_open_shadow_menu_as_closed() -> Non
</script>"""
) as page:
assert await page.is_visible('[data-tv3-menu="1"]') is True
- after = await page.evaluate(_MENU_AFTER_JS, '[data-tv3-menu="1"]')
+ after = await page.evaluate(_MENU_AFTER_JS, {"sel": '[data-tv3-menu="1"]', "el": None})
assert after["stillOpen"] == 2
@@ -4796,14 +4829,14 @@ async def test_menu_finder_refuses_when_no_snapshot_arms_it() -> None:
async with _live_page(html) as page:
# No precheck has run: window carries no snapshot, exactly as after a navigation.
assert await page.evaluate("() => window.__tv3_pre === undefined") is True
- assert await page.evaluate(_FIND_MENU_JS, "#pick") is None
+ assert await page.evaluate(_FIND_MENU_JS, {"sel": "#pick", "el": None}) is None
# ...and the same page WITH a snapshot still finds nothing, because nothing reacted — so the
# refusal above is about the missing snapshot, not about an unfindable menu.
- await page.evaluate(_CLICK_PRECHECK_JS, "#pick")
- assert await page.evaluate(_FIND_MENU_JS, "#pick") is None
+ await page.evaluate(_CLICK_PRECHECK_JS, {"sel": "#pick", "el": None})
+ assert await page.evaluate(_FIND_MENU_JS, {"sel": "#pick", "el": None}) is None
# Prove the fixture is findable at all once the rows genuinely appear in reaction.
await page.evaluate("() => { document.getElementById('menu').remove(); }")
- await page.evaluate(_CLICK_PRECHECK_JS, "#pick")
+ await page.evaluate(_CLICK_PRECHECK_JS, {"sel": "#pick", "el": None})
await page.evaluate(
"(h) => { const d = document.createElement('div'); d.innerHTML = h;"
" document.body.appendChild(d.firstElementChild); }",
@@ -4813,7 +4846,7 @@ async def test_menu_finder_refuses_when_no_snapshot_arms_it() -> None:
<div role="option" style="height:20px">Gamma Inc</div>
</div>""",
)
- found = await page.evaluate(_FIND_MENU_JS, "#pick")
+ found = await page.evaluate(_FIND_MENU_JS, {"sel": "#pick", "el": None})
assert isinstance(found, dict) and found.get("count") == 3, found
@@ -4840,10 +4873,10 @@ async def test_menu_finder_sees_rows_written_straight_into_a_shadow_root() -> No
});
</script>"""
) as page:
- await page.evaluate(_CLICK_PRECHECK_JS, "#pick")
+ await page.evaluate(_CLICK_PRECHECK_JS, {"sel": "#pick", "el": None})
await page.click("#pick")
await page.wait_for_timeout(100)
- found = await page.evaluate(_FIND_MENU_JS, "#pick")
+ found = await page.evaluate(_FIND_MENU_JS, {"sel": "#pick", "el": None})
assert isinstance(found, dict) and found.get("count") == 3, found
assert [o["text"] for o in found["options"]] == ["Alpha", "Beta", "Gamma"]
@@ -4872,13 +4905,13 @@ async def test_click_precheck_flags_a_shadow_host_that_wraps_the_open_menu() ->
});
</script>"""
) as page:
- await page.evaluate(_CLICK_PRECHECK_JS, "#pick")
+ await page.evaluate(_CLICK_PRECHECK_JS, {"sel": "#pick", "el": None})
await page.click("#pick")
await page.wait_for_timeout(100)
- found = await page.evaluate(_FIND_MENU_JS, "#pick")
+ found = await page.evaluate(_FIND_MENU_JS, {"sel": "#pick", "el": None})
assert isinstance(found, dict) and found.get("count") == 3, found
- pre = await page.evaluate(_CLICK_PRECHECK_JS, "#m")
+ pre = await page.evaluate(_CLICK_PRECHECK_JS, {"sel": "#m", "el": None})
assert pre["menuOpen"] is True, pre
# The host encloses every tagged row through its own shadow root, so this click cannot be
# attributed to any one option.
@@ -4947,7 +4980,7 @@ async def test_pre_snapshot_does_not_provoke_the_re_render_it_then_misreads() ->
# A STRING, not a dict: _FIND_MENU_JS takes the clicked selector. A dict stringifies to
# "[object Object]", pQS throws on it, and the probe returns null at the trigger step —
# passing for a reason that has nothing to do with the reaction gate under test.
- assert await page.evaluate(_FIND_MENU_JS, "#city") is None
+ assert await page.evaluate(_FIND_MENU_JS, {"sel": "#city", "el": None}) is None
assert await page.evaluate("() => window.renders") == 1, "the snapshot must not mutate the page"
# The fixture really is reactive: stamping an attribute the old way does re-render it, so the
@@ -4976,12 +5009,12 @@ async def test_pre_snapshot_survives_a_container_the_page_rebuilds_by_cloning()
<a href="/c" role="option" style="display:block;height:20px">Result Gamma</a>
</div>"""
) as page:
- await page.evaluate(_CLICK_PRECHECK_JS, "#sort")
+ await page.evaluate(_CLICK_PRECHECK_JS, {"sel": "#sort", "el": None})
# The page rebuilds the container: every node is new, but every node carries the mark.
await page.evaluate(
"() => { const r = document.getElementById('results'); r.parentNode.replaceChild(r.cloneNode(true), r); }"
)
- assert await page.evaluate(_FIND_MENU_JS, "#sort") is None
+ assert await page.evaluate(_FIND_MENU_JS, {"sel": "#sort", "el": None}) is None
@_skip_no_browser
@@ -5011,10 +5044,10 @@ async def test_reaction_gate_rejects_a_page_supplied_snapshot_impostor() -> None
});
</script>"""
) as page:
- await page.evaluate(_CLICK_PRECHECK_JS, "#pick")
+ await page.evaluate(_CLICK_PRECHECK_JS, {"sel": "#pick", "el": None})
# The impostor answers "not in the snapshot" for every row. Without the instanceof check the
# probe believes it, and reports three static links as a menu the click just opened.
- assert await page.evaluate(_FIND_MENU_JS, "#pick") is None
+ assert await page.evaluate(_FIND_MENU_JS, {"sel": "#pick", "el": None}) is None
@_skip_no_browser
@@ -5121,6 +5154,224 @@ _PROXY_MENU_IN_SHADOW_HTML = """
"""
+@_skip_no_browser
+async def test_a_control_in_a_repeated_component_is_addressed_through_its_host() -> None:
+ # The gap this closes: both instances are correctly refused a flat selector (the id names two
+ # elements), which left them with no selector at all and therefore unreachable by every tool.
+ # Anchoring on the host — which lives outside the root it owns, and so can be named the ordinary
+ # way — separates them without writing anything into the component.
+ async with _live_page(_REPEATED_COMPONENT_HTML) as page:
+ tools = build_browser_tools(_fixed_page_provider(page))
+ assert await page.locator("#apply").count() == 2, "fixture must reproduce the reused id"
+
+ r = await _tool(tools, "observe").handler({})
+ selectors = _printed_selectors(r.content)
+ assert len(selectors) == 2, r.content
+ for sel in selectors:
+ assert await page.locator(sel).count() == 1, f"{sel} must denote exactly one element"
+
+ # Pick the instance the digest attributes to the SECOND host, and re-render it before acting:
+ # a mechanism that is correct only until the component rebuilds passes a fixture that never
+ # rebuilds, and fails in production.
+ chosen = next(s for s in selectors if "fld-b" in s)
+ await page.evaluate("() => { window.build('fld-b', 'b-v2'); window.hits = []; }")
+
+ await _tool(tools, "click").handler({"selector": chosen})
+ # The twin carries the same listener, so its silence is the discriminating half of this proof,
+ # and the v2 token is what shows the selector re-resolved to the rebuilt node.
+ assert await page.evaluate("() => window.hits") == ["b-v2"], chosen
+
+
+@_skip_no_browser
+async def test_a_host_anchored_control_is_driven_rather_than_reported_gone() -> None:
+ # observe naming a control that the act path then refuses to resolve is the seam that cost this
+ # module its worst regression: the probes resolve per root, which cannot match a selector whose
+ # two halves straddle a shadow boundary, so a click fell through to the full actionability wait
+ # and then reported an element that is plainly present as removed by a re-render.
+ async with _content_page(_SHARED_ID_DROPZONES_HTML) as page:
+ tools = build_browser_tools(_fixed_page_provider(page))
+ r = await _tool(tools, "observe").handler({})
+ listed = [ln for ln in r.content.splitlines() if "[hidden-native" in ln and not ln.startswith("note:")]
+ assert len(listed) == 3, r.content
+ assert "[#dz-b #resume]" in r.content, r.content
+
+ chosen = next(s for s in _printed_selectors(r.content) if "dz-b" in s)
+ started = time.monotonic()
+ cr = await _tool(tools, "click").handler({"selector": chosen})
+ assert cr.status == "error"
+ assert "file_upload" in cr.content, cr.content
+ assert "no longer exists" not in cr.content, cr.content
+ # The redirect is decided by a probe, not by waiting for Playwright to give up on a control
+ # the page renders at zero size.
+ assert time.monotonic() - started < 5, "the refusal must not cost an actionability timeout"
+
+
+@_skip_no_browser
+async def test_a_host_anchored_selector_counts_what_the_host_slots_as_well_as_what_it_owns() -> None:
+ # A descendant combinator is shadow-transparent to the executor, so `#host #ctrl` also matches a
+ # light-DOM child slotted INTO the component. Scoping the uniqueness count to the root alone
+ # would undercount, and undercounting is the direction that mints an ambiguous selector — which
+ # the executor resolves by silently taking the first of several rather than refusing.
+ async with _live_page(
+ """<ds-field id="host-one"><input id="dup" style="width:80px;height:20px"></ds-field>
+ <script>
+ var r = document.getElementById('host-one').attachShadow({mode: 'open'});
+ r.innerHTML = '<slot></slot><input id="dup" style="width:80px;height:20px">';
+ </script>"""
+ ) as page:
+ tools = build_browser_tools(_fixed_page_provider(page))
+ assert await page.locator("#host-one #dup").count() == 2, "fixture must arm the slotted twin"
+ r = await _tool(tools, "observe").handler({})
+ selectors = _printed_selectors(r.content)
+ # Armed on the DIGEST too, not only on the fixture: the property below is quantified over what
+ # was printed, so an empty or collapsed digest would satisfy it without protecting anything.
+ # The light-DOM twin is still named, and the one under the host is refused AND said to be.
+ assert len(selectors) == 1, r.content
+ assert "#host-one #dup" not in r.content, r.content
+ assert "reused by another instance of the same component" in r.content, r.content
+ for sel in selectors:
+ assert await page.locator(sel).count() == 1, f"{sel} denotes more than one element: {r.content}"
+
+
+@_skip_no_browser
+async def test_file_upload_reaches_the_dropzone_of_the_instance_it_was_given(
+ monkeypatch: pytest.MonkeyPatch, tmp_path: Any
+) -> None:
+ # The shape this whole mechanism exists for: a run fills every named field correctly and still
+ # fails the application because it cannot attach the file. click only ever redirects here, so
+ # the redirect passing is not evidence the upload lands — this asserts the file itself arrives,
+ # in the instance named and not its twin.
+ import skyvern.forge.sdk.api.files as _files # noqa: PLC0415
+
+ staged = tmp_path / "cv.pdf"
+ staged.write_bytes(b"%PDF-1.4 cv")
+
+ async def _staged_file(source: str, output_dir: Any = None, organization_id: Any = None) -> str:
+ return str(staged)
+
+ monkeypatch.setattr(_files, "download_file", _staged_file)
+
+ async with _content_page(_SHARED_ID_DROPZONES_HTML) as page:
+ tools = build_browser_tools(_fixed_page_provider(page))
+ r = await _tool(tools, "observe").handler({})
+ chosen = next(s for s in _printed_selectors(r.content) if "dz-b" in s)
+
+ ur = await _tool(tools, "file_upload").handler({"selector": chosen, "file": "https://example.test/cv.pdf"})
+ assert ur.status == "ok", ur.content
+
+ # Read every instance's own input: the twin holding zero files is the half of this that
+ # rules out a 50/50 landing.
+ landed = await page.evaluate(
+ """() => Array.from(document.querySelectorAll('ds-drop')).map((h) => {
+ const i = h.shadowRoot && h.shadowRoot.querySelector('input[type=file]');
+ return [h.id || h.className, i ? i.files.length : -1];
+ })"""
+ )
+ assert ["dz-b", 1] in landed, landed
+ assert [name for name, count in landed if count > 0] == ["dz-b"], landed
+
+
+@_skip_no_browser
+async def test_a_typeahead_in_a_repeated_component_is_not_reported_as_unfilled() -> None:
+ # Naming these controls makes them reachable by every probe that resolves a selector against the
+ # main document, and those cannot see one whose two halves straddle a boundary. The read-back
+ # then found nothing and the tool reported the field NOT filled while it held the value — a
+ # false claim about the page, which is worse than the silence it replaced.
+ async with _live_page(
+ """<div id="portal"></div>
+ <ds-field id="f1"></ds-field>
+ <ds-field id="f2"></ds-field>
+ <script>
+ for (const h of document.querySelectorAll('ds-field')) {
+ var r = h.attachShadow({mode: 'open'});
+ r.innerHTML = '<input id="city" type="text" autocomplete="off" style="width:140px;height:24px">';
+ (function (inp) {
+ inp.addEventListener('input', function () {
+ var p = document.getElementById('portal');
+ p.innerHTML = '';
+ if (!inp.value) return;
+ ['Lisbon', 'Lisbon Airport', 'Lisboa Centro'].forEach(function (t) {
+ var d = document.createElement('div');
+ d.textContent = t;
+ d.setAttribute('role', 'option');
+ d.style.cssText = 'width:200px;height:24px;cursor:pointer';
+ d.addEventListener('click', function () { inp.value = t; p.innerHTML = ''; });
+ p.appendChild(d);
+ });
+ });
+ })(r.querySelector('input'));
+ }
+ </script>"""
+ ) as page:
+ tools = build_browser_tools(_fixed_page_provider(page))
+ r = await _tool(tools, "observe").handler({})
+ chosen = next(s for s in _printed_selectors(r.content) if "f2" in s)
+
+ tr = await _tool(tools, "type").handler({"selector": chosen, "text": "Lisbon"})
+ landed = await page.evaluate(
+ """() => Array.from(document.querySelectorAll('ds-field')).map(
+ (h) => h.shadowRoot.querySelector('input').value)"""
+ )
+ assert landed == ["", "Lisbon"], landed
+ # The value is in the page, so "NOT filled" would be false; and the twin staying empty is
+ # what rules out the typing having gone to the wrong instance.
+ assert tr.status == "ok", tr.content
+ assert "NOT filled" not in tr.content, tr.content
+ assert "Lisbon" in tr.content, tr.content
+
+
+@_skip_no_browser
+async def test_a_component_typeahead_that_rejects_the_value_still_fails_loudly() -> None:
+ # The other half of the same fix, and the one that decides whether it is safe: teaching the
+ # read-back to see into a component means "did not commit" is now established rather than
+ # guessed, so it must halt the batch. Softening it here would let a run submit an empty
+ # required field believing the outcome was merely unverified.
+ async with _live_page(
+ """<div id="portal"></div>
+ <ds-field id="f1"></ds-field>
+ <ds-field id="f2"></ds-field>
+ <script>
+ for (const h of document.querySelectorAll('ds-field')) {
+ var r = h.attachShadow({mode: 'open'});
+ r.innerHTML = '<input id="city" type="text" autocomplete="off" style="width:140px;height:24px">';
+ (function (inp) {
+ inp.addEventListener('input', function () {
+ var p = document.getElementById('portal');
+ p.innerHTML = '';
+ if (!inp.value) return;
+ ['Lisbon', 'Lisbon Airport'].forEach(function (t) {
+ var d = document.createElement('div');
+ d.textContent = t;
+ d.setAttribute('role', 'option');
+ d.style.cssText = 'width:200px;height:24px;cursor:pointer';
+ // The widget refuses the selection instead of accepting it.
+ d.addEventListener('click', function () { inp.value = ''; p.innerHTML = ''; });
+ p.appendChild(d);
+ });
+ });
+ })(r.querySelector('input'));
+ }
+ </script>"""
+ ) as page:
+ tools = build_browser_tools(_fixed_page_provider(page))
+ r = await _tool(tools, "observe").handler({})
+ chosen = next(s for s in _printed_selectors(r.content) if "f2" in s)
+
+ tr = await _tool(tools, "type").handler({"selector": chosen, "text": "Lisbon"})
+ assert (
+ await page.evaluate("() => document.querySelector('#f2').shadowRoot.querySelector('input').value") == ""
+ ), "fixture must actually reject the value"
+ assert tr.status == "error", tr.content
+ assert "NOT filled" in tr.content, tr.content
+ assert "could not be verified" not in tr.content, tr.content
+
+
@_skip_no_browser
@pytest.mark.asyncio
async def test_pierced_probes_and_hidden_native_listing_agree_on_the_shadow_hosted_widget() -> None: