Skip to content

Automate browser based workflows with AI

Add mode='direct' to SkyvernPage click/fill for static script action recording (#5264)

pedrohsdb28adde908fb0
diff --git a/skyvern/core/script_generations/extract_form_fields.js b/skyvern/core/script_generations/extract_form_fields.js
index 9d8e75b99..eb367bc6b 100644
--- a/skyvern/core/script_generations/extract_form_fields.js
+++ b/skyvern/core/script_generations/extract_form_fields.js
@@ -79,7 +79,7 @@
const elType = (el.getAttribute("type") || "").toLowerCase();
const vis = elType === "file" ? "" : ":visible";
if (el.name) return tag + '[name="' + el.name + '"]' + vis;
- if (el.id) return "#" + el.id + vis;
+ if (el.id) return "#" + CSS.escape(el.id) + vis;
// File inputs with no name/id: no name/id, use data-automation-id or type selector
if (elType === "file") {
const autoId = el.getAttribute("data-automation-id");
@@ -106,7 +106,7 @@
}
function buildOptionSelector(el) {
- if (el.id) return "#" + el.id;
+ if (el.id) return "#" + CSS.escape(el.id);
const tag = el.tagName.toLowerCase();
const name = el.name;
const value = el.value;
@@ -256,24 +256,23 @@
if (["hidden", "submit", "button", "image", "reset"].includes(type))
continue;
// Skip inputs inside multiselect containers — platform extension handles those
- if (
- el.closest('[data-uxi-widget-type="multiselect"]') ||
- el.closest('[role="combobox"][aria-autocomplete="list"]')
- )
- continue;
+ if (el.closest('[data-uxi-widget-type="multiselect"]')) continue;
// Allow aria-checked inputs through even if visually hidden (hides
// real radio/checkbox inputs behind styled overlays at opacity:0)
const hasAriaChecked = el.hasAttribute("aria-checked");
if (type !== "file" && !hasAriaChecked && !isVisible(el)) continue;
- // Custom <input aria-checked> without type="radio" for custom radios.
- // Detect these by the presence of aria-checked attribute.
+ // Detect special input types by ARIA attributes:
+ // - aria-checked → custom radio
+ // - role="combobox" → search dropdown (type to filter, click to select)
const effectiveType =
type === "checkbox" || type === "radio"
? type
: el.hasAttribute("aria-checked")
? "radio"
- : type;
+ : el.getAttribute("role") === "combobox"
+ ? "search-dropdown"
+ : type;
if (effectiveType === "checkbox" || effectiveType === "radio") {
if (el.name) {
@@ -395,7 +394,7 @@
selector: selector,
tag: el.tagName.toLowerCase(),
type:
- type ||
+ effectiveType ||
(el.tagName.toLowerCase() === "select"
? "select"
: el.tagName.toLowerCase() === "textarea"
@@ -461,7 +460,8 @@
"')";
}
}
- if (!optSelector && optEl.id) optSelector = "#" + optEl.id;
+ if (!optSelector && optEl.id)
+ optSelector = "#" + CSS.escape(optEl.id);
if (optSelector) {
fallbackOptions.push({
label: optLabel,
@@ -534,7 +534,7 @@
const optSelector = r.getAttribute("data-automation-id")
? '[data-automation-id="' + r.getAttribute("data-automation-id") + '"]'
: r.id
- ? "#" + r.id
+ ? "#" + CSS.escape(r.id)
: null;
if (optSelector) {
options.push({
@@ -597,7 +597,7 @@
r.getAttribute("data-automation-id") +
'"]'
: r.id
- ? "#" + r.id
+ ? "#" + CSS.escape(r.id)
: null;
if (optSelector) {
options.push({
@@ -639,7 +639,7 @@
const selector = cbId
? '[data-automation-id="' + cbId + '"]'
: cb.id
- ? "#" + cb.id
+ ? "#" + CSS.escape(cb.id)
: null;
if (selector) {
fields.push({
diff --git a/skyvern/core/script_generations/script_skyvern_page.py b/skyvern/core/script_generations/script_skyvern_page.py
index b2f8d72a5..a3b8fb7a4 100644
--- a/skyvern/core/script_generations/script_skyvern_page.py
+++ b/skyvern/core/script_generations/script_skyvern_page.py
@@ -2,6 +2,7 @@ from __future__ import annotations
import asyncio
import os
+import re
from pathlib import Path
from typing import Any, Callable
@@ -306,12 +307,19 @@ class ScriptSkyvernPage(SkyvernPage):
pass # Don't block if download detection fails
self._record(call)
+ # Ensure selector is in kwargs for action recording (it may be a positional arg).
+ # Copy kwargs to avoid mutating the caller's dict.
+ recording_kwargs = dict(kwargs)
+ if "selector" not in recording_kwargs and args:
+ first_arg = args[0]
+ if isinstance(first_arg, str):
+ recording_kwargs["selector"] = first_arg
# Auto-create action after execution and store result
await self._create_action_and_result_after_execution(
action_type=action,
intention=prompt,
status=action_status,
- kwargs=kwargs,
+ kwargs=recording_kwargs,
call_result=call.result,
call_error=call.error,
download_triggered=download_triggered,
@@ -409,9 +417,16 @@ class ScriptSkyvernPage(SkyvernPage):
select_option = None
response: str | None = kwargs.get("response")
file_url = kwargs.get("file_url")
+
+ # Mask sensitive values (passwords, etc.) by checking if the
+ # input value matches any registered sensitive value on the context.
+ selector = kwargs.get("selector", "")
+ sensitive = context.sensitive_values if context else set()
+ is_sensitive = bool(call_result and str(call_result) in sensitive)
+
if not response:
if action_type == ActionType.INPUT_TEXT:
- text = str(call_result)
+ text = "••••••••" if is_sensitive else str(call_result)
response = text
elif action_type == ActionType.SELECT_OPTION:
option_value = str(call_result) or ""
@@ -465,12 +480,34 @@ class ScriptSkyvernPage(SkyvernPage):
)
created_action = await app.DATABASE.create_action(action)
- # Skip LLM reasoning in script mode — use static string instead
+ # Skip LLM reasoning in script mode — use static string instead.
+ # Build a descriptive label from the selector for the timeline.
if context and context.script_mode:
+ label = intention[:80] if intention else ""
+ if not label and selector:
+ # Extract a human-readable name from the selector
+
+ name_match = re.search(r'name="([^"]+)"', selector)
+ id_match = re.search(r'id="([^"]+)"', selector) if not name_match else None
+ auto_match = (
+ re.search(r'data-automation-id="([^"]+)"', selector)
+ if not name_match and not id_match
+ else None
+ )
+ match = name_match or id_match or auto_match
+ if match:
+ raw = match.group(1)
+ # Convert camelCase/kebab-case to readable: "legalName--firstName" → "First Name"
+ readable = re.sub(r"[-_]+", " ", raw).strip()
+ readable = re.sub(r"([a-z])([A-Z])", r"\1 \2", readable).title()
+ label = readable
+ else:
+ label = selector[:60]
+ reasoning = f"Script execution: {label}" if label else "Script execution"
await app.DATABASE.update_action_reasoning(
organization_id=str(context.organization_id),
action_id=str(created_action.action_id),
- reasoning=f"Script execution: {intention[:80]}" if intention else "Script execution",
+ reasoning=reasoning,
)
else:
asyncio.create_task(
@@ -978,6 +1015,11 @@ class ScriptSkyvernPage(SkyvernPage):
step_id=context.step_id,
step_order=step.order,
action_order=context.action_order,
+ # Static (pinned) scripts verify page state themselves —
+ # skip LLM verification which can reject valid completions
+ # (e.g. sign-in page with pending email verification).
+ # AI-generated cached scripts still get LLM verification.
+ verified=bool(context.is_static_script),
)
# result = await ActionHandler.handle_action(self.scraped_page, task, step, self.page, action)
result = await handle_complete_action(action, self.page, self.scraped_page, task, step)
diff --git a/skyvern/core/script_generations/skyvern_page.py b/skyvern/core/script_generations/skyvern_page.py
index c2bec6c8d..f80ce83fc 100644
--- a/skyvern/core/script_generations/skyvern_page.py
+++ b/skyvern/core/script_generations/skyvern_page.py
@@ -217,6 +217,7 @@ class SkyvernPage(Page):
*,
prompt: str | None = None,
ai: str | None = "fallback",
+ mode: str | None = None,
**kwargs: Any,
) -> str | None: ...
@@ -226,6 +227,7 @@ class SkyvernPage(Page):
*,
prompt: str,
ai: str | None = "fallback",
+ mode: str | None = None,
**kwargs: Any,
) -> str | None: ...
@@ -236,6 +238,7 @@ class SkyvernPage(Page):
*,
prompt: str | None = None,
ai: str | None = "fallback",
+ mode: str | None = None,
**kwargs: Any,
) -> str | None:
"""Click an element using a CSS selector, AI-powered prompt matching, or both.
@@ -249,6 +252,9 @@ class SkyvernPage(Page):
selector: CSS selector for the target element.
prompt: Natural language description of which element to click.
ai: AI behavior mode. Defaults to "fallback" which tries selector first, then AI.
+ mode: When ``"direct"``, perform a raw Playwright click with no AI
+ fallback or element preparation. The action is still recorded
+ in the DB so it appears in the timeline.
**kwargs: All Playwright click parameters (timeout, force, modifiers, etc.)
Returns:
@@ -264,8 +270,20 @@ class SkyvernPage(Page):
# Try selector first, fall back to AI if selector fails
await page.click("#open-invoice-button", prompt="Click on the 'Open Invoice' button")
+
+ # Raw Playwright click (still recorded in the timeline)
+ await page.click('[data-automation-id="nextButton"]', mode="direct")
```
"""
+ # Direct mode: raw Playwright click, no AI fallback or element prep.
+ if mode == "direct":
+ if not selector:
+ raise ValueError("mode='direct' requires a selector.")
+ timeout = kwargs.pop("timeout", settings.BROWSER_ACTION_TIMEOUT_MS)
+ locator = self.page.locator(selector).first
+ await locator.click(timeout=timeout, **kwargs)
+ return selector
+
# Backward compatibility
intention = kwargs.pop("intention", None)
if intention is not None and prompt is None:
@@ -369,6 +387,7 @@ class SkyvernPage(Page):
*,
prompt: str | None = None,
ai: str | None = "fallback",
+ mode: str | None = None,
totp_identifier: str | None = None,
totp_url: str | None = None,
**kwargs: Any,
@@ -382,6 +401,7 @@ class SkyvernPage(Page):
value: str | None = None,
selector: str | None = None,
ai: str | None = "fallback",
+ mode: str | None = None,
totp_identifier: str | None = None,
totp_url: str | None = None,
**kwargs: Any,
@@ -395,6 +415,7 @@ class SkyvernPage(Page):
*,
prompt: str | None = None,
ai: str | None = "fallback",
+ mode: str | None = None,
totp_identifier: str | None = None,
totp_url: str | None = None,
**kwargs: Any,
@@ -411,6 +432,9 @@ class SkyvernPage(Page):
value: The text value to input into the field.
prompt: Natural language description of which field to fill and what value.
ai: AI behavior mode. Defaults to "fallback" which tries selector first, then AI.
+ mode: When ``"direct"``, perform a raw Playwright fill with no AI
+ fallback or element preparation. The action is still recorded
+ in the DB so it appears in the timeline.
totp_identifier: TOTP identifier for time-based one-time password fields.
totp_url: URL to fetch TOTP codes from for authentication.
@@ -431,9 +455,23 @@ class SkyvernPage(Page):
prompt="Fill the email address with [email protected]"
)
+
+ # Raw Playwright fill (still recorded in the timeline)
+ await page.fill('input[data-automation-id="email"]', "[email protected]", mode="direct")
```
"""
+ # Direct mode: raw Playwright fill, no AI fallback or element prep.
+ if mode == "direct":
+ if not selector:
+ raise ValueError("mode='direct' requires a selector.")
+ if value is None:
+ raise ValueError("mode='direct' requires a value.")
+ timeout = kwargs.pop("timeout", settings.BROWSER_ACTION_TIMEOUT_MS)
+ locator = self.page.locator(selector).first
+ await locator.fill(value, timeout=timeout, **kwargs)
+ return value
+
# Backward compatibility
intention = kwargs.pop("intention", None)
if intention is not None and prompt is None:
@@ -1587,6 +1625,24 @@ class SkyvernPage(Page):
max_steps=3,
)
+ elif field_type in ("search-dropdown", "dropdown"):
+ # Combobox / React Select: click to open, type to filter,
+ # click the matching option.
+ str_value = str(value)
+ locator = self.page.locator(selector).first
+ await locator.click(timeout=5000)
+ await asyncio.sleep(0.3)
+ await locator.fill("")
+ search_text = str_value.split(",")[0].strip()[:25]
+ await self.page.keyboard.type(search_text, delay=50)
+ await asyncio.sleep(0.5)
+ option = self.page.locator('[class*="select__option"]:visible').first
+ try:
+ await option.click(timeout=3000)
+ except Exception:
+ await self.page.keyboard.press("Enter")
+ await asyncio.sleep(0.3)
+
else:
await self.fill(selector=selector, value=str(value), ai=None)
@@ -1868,6 +1924,7 @@ class SkyvernPage(Page):
start_time = time.monotonic()
pages_filled = 0
prev_field_signature: str | None = None
+ consecutive_validation_failures = 0
for page_num in range(max_pages):
elapsed = time.monotonic() - start_time
@@ -2025,13 +2082,24 @@ class SkyvernPage(Page):
return errs;
}""")
if post_click_errors:
+ consecutive_validation_failures += 1
LOG.warning(
"fill_multipage_form: validation errors after Save and Continue",
page_num=page_num,
error_count=len(post_click_errors),
errors=post_click_errors[:5],
+ consecutive_failures=consecutive_validation_failures,
)
await self._dump_html(debug_dir, f"p{page_num}_04_validation_errors")
+ if consecutive_validation_failures >= 3:
+ LOG.warning(
+ "fill_multipage_form: too many consecutive validation failures, stopping",
+ page_num=page_num,
+ failures=consecutive_validation_failures,
+ )
+ break
+ else:
+ consecutive_validation_failures = 0
except Exception:
pass
diff --git a/skyvern/forge/agent_functions.py b/skyvern/forge/agent_functions.py
index d84689b94..2ca95db7c 100644
--- a/skyvern/forge/agent_functions.py
+++ b/skyvern/forge/agent_functions.py
@@ -650,6 +650,14 @@ class AgentFunction:
"""
return None
+ def try_import_static_script(self, script_path: str) -> Any | None:
+ """Try to import a static script module as a fallback when spec_from_file_location fails.
+
+ Override in subclass for platform-specific import logic.
+ Returns the loaded module or None.
+ """
+ return None
+
async def ensure_static_script(
self,
workflow: Any,
diff --git a/skyvern/forge/sdk/core/skyvern_context.py b/skyvern/forge/sdk/core/skyvern_context.py
index e2cd8a82f..1d4f6a9bf 100644
--- a/skyvern/forge/sdk/core/skyvern_context.py
+++ b/skyvern/forge/sdk/core/skyvern_context.py
@@ -56,6 +56,8 @@ class SkyvernContext:
loop_output_values: list[dict[str, Any]] | None = None
script_run_parameters: dict[str, Any] = field(default_factory=dict)
script_mode: bool = False
+ is_static_script: bool = False
+ sensitive_values: set[str] = field(default_factory=set)
ai_mode_override: str | None = None
script_llm_call_count: int = 0
last_classify_result: str | None = None
diff --git a/skyvern/forge/sdk/workflow/service.py b/skyvern/forge/sdk/workflow/service.py
index df6f570eb..a5c7dfb2b 100644
--- a/skyvern/forge/sdk/workflow/service.py
+++ b/skyvern/forge/sdk/workflow/service.py
@@ -1276,17 +1276,39 @@ class WorkflowService:
spec = importlib.util.spec_from_file_location("user_script", script_path)
if spec and spec.loader:
loaded_script_module = importlib.util.module_from_spec(spec)
- spec.loader.exec_module(loaded_script_module)
- param_cls = getattr(loaded_script_module, "GeneratedWorkflowParameters", None)
+ try:
+ spec.loader.exec_module(loaded_script_module)
+ except Exception:
+ # Static scripts may fail with spec_from_file_location
+ # due to circular imports. Delegate to AgentFunction for
+ # platform-specific fallback loading.
+ LOG.warning("exec_module failed, trying import_module fallback", exc_info=True)
+ loaded_script_module = app.AGENT_FUNCTION.try_import_static_script(script_path)
+ param_cls = (
+ getattr(loaded_script_module, "GeneratedWorkflowParameters", None)
+ if loaded_script_module
+ else None
+ )
await skyvern.setup(
script_parameters,
generated_parameter_cls=param_cls,
)
- LOG.info(
- "Successfully loaded script module",
- script_id=script.script_id,
- block_count=len(script_blocks_by_label),
- )
+ if loaded_script_module:
+ # Mark static (pinned) scripts so complete() skips LLM verification
+ if script.is_pinned:
+ pinned_ctx = skyvern_context.current()
+ if pinned_ctx:
+ pinned_ctx.is_static_script = True
+ LOG.info(
+ "Successfully loaded script module",
+ script_id=script.script_id,
+ block_count=len(script_blocks_by_label),
+ )
+ else:
+ LOG.warning(
+ "Script module failed to load, blocks will fall back to agent",
+ script_id=script.script_id,
+ )
else:
LOG.warning(
"Script file not found at path",
@@ -1328,6 +1350,10 @@ class WorkflowService:
script_parameters,
generated_parameter_cls=param_cls,
)
+ # Mark context so static scripts skip LLM completion verification
+ static_ctx = skyvern_context.current()
+ if static_ctx:
+ static_ctx.is_static_script = True
LOG.info(
"Static script loaded successfully",
script_id=script.script_id if script else None,
@@ -1568,6 +1594,10 @@ class WorkflowService:
if not context or not context.generate_script:
return
+ # Skip script generation for static (pinned) scripts
+ if context.is_static_script:
+ return
+
disable_script_generation = await app.EXPERIMENTATION_PROVIDER.is_feature_enabled_cached(
"DISABLE_GENERATE_SCRIPT_AFTER_BLOCK",
workflow_run.workflow_run_id,
@@ -4823,6 +4853,19 @@ class WorkflowService:
# Manages cached workflow script regeneration with conditional-aware locking and versioning
if existing_script:
+ # Pinned static scripts (created by ensure_static_script) should
+ # never be regenerated — they are hand-written and authoritative.
+ # Only check for pinned scripts when running a static script (avoids
+ # an extra DB query for every non-static cached-script workflow).
+ ctx = skyvern_context.current()
+ if ctx and ctx.is_static_script:
+ LOG.info(
+ "Skipping script generation for pinned static script",
+ script_id=existing_script.script_id,
+ workflow_run_id=workflow_run.workflow_run_id,
+ )
+ return None
+
cached_block_labels: set[str] = set()
script_blocks = await app.DATABASE.get_script_blocks_by_script_revision_id(
script_revision_id=existing_script.script_revision_id,
diff --git a/skyvern/services/script_service.py b/skyvern/services/script_service.py
index 1e7019a13..54c30205b 100644
--- a/skyvern/services/script_service.py
+++ b/skyvern/services/script_service.py
@@ -470,6 +470,9 @@ async def _create_workflow_block_run_and_task(
prompt = _render_template_with_label(prompt, label)
if url:
url = _render_template_with_label(url, label)
+ # Include script parameters as navigation_payload so handlers
+ # (e.g. file upload) can find URLs like resume_link in the payload.
+ nav_payload = context.script_run_parameters or None
task = await app.DATABASE.create_task(
# fix HACK: changed the type of url to str | None to support None url. url is not used in the script right now.
url=url or "",
@@ -477,7 +480,7 @@ async def _create_workflow_block_run_and_task(
navigation_goal=prompt,
data_extraction_goal=prompt if block_type == BlockType.EXTRACTION else None,
extracted_information_schema=schema,
- navigation_payload=None,
+ navigation_payload=nav_payload,
status="running",
organization_id=organization_id,
workflow_run_id=workflow_run_id,
@@ -1721,12 +1724,15 @@ async def run_task(
context: skyvern_context.SkyvernContext | None = None
if cache_key and cached_fn:
- # Auto-create workflow block run and task if workflow_run_id is available
+ # Auto-create workflow block run and task if workflow_run_id is available.
+ # Use `label` (the workflow block label) for the block run so the
+ # framework can match it, and `cache_key` to look up the cached function.
+ block_label = label or cache_key
workflow_run_block_id, task_id, step_id = await _create_workflow_block_run_and_task(
block_type=BlockType.NAVIGATION,
prompt=prompt,
url=url,
- label=cache_key,
+ label=block_label,
model=model,
created_by="script",
)
diff --git a/tests/unit/test_static_script_infrastructure.py b/tests/unit/test_static_script_infrastructure.py
new file mode 100644
index 000000000..8aed46d94
--- /dev/null
+++ b/tests/unit/test_static_script_infrastructure.py
@@ -0,0 +1,229 @@
+"""Tests for static script infrastructure: mode='direct', BLOCK_MAP resolution, nativeSel."""
+
+from __future__ import annotations
+
+import types
+from unittest.mock import MagicMock
+
+import pytest
+
+# ---------------------------------------------------------------------------
+# nativeSel (JavaScript helper) — test via a Python reimplementation
+# ---------------------------------------------------------------------------
+
+
+def _native_sel(sel: str | None) -> str | None:
+ """Python reimplementation of nativeSel() from the platform form fields JS extension."""
+ import re
+
+ if not sel:
+ return None
+ result = re.sub(r":visible", "", sel)
+ result = re.sub(r':has-text\("[^"]*"\)', "", result)
+ return result.strip() or None
+
+
+class TestNativeSel:
+ def test_strips_visible(self) -> None:
+ assert _native_sel('input[name="foo"]:visible') == 'input[name="foo"]'
+
+ def test_strips_has_text(self) -> None:
+ assert _native_sel('label:has-text("Submit") input:visible') == "label input"
+
+ def test_handles_parens_in_has_text(self) -> None:
+ assert _native_sel(':has-text("Click (here)")') is None # fully stripped
+
+ def test_none_input(self) -> None:
+ assert _native_sel(None) is None
+
+ def test_empty_string(self) -> None:
+ assert _native_sel("") is None
+
+ def test_no_pseudo_selectors(self) -> None:
+ assert _native_sel('input[type="text"]') == 'input[type="text"]'
+
+ def test_multiple_visible(self) -> None:
+ assert _native_sel('[role="option"]:visible, .item:visible') == '[role="option"], .item'
+
+
+# ---------------------------------------------------------------------------
+# BLOCK_MAP resolution logic
+# ---------------------------------------------------------------------------
+
+
+class TestBlockMapResolution:
+ """Test the BLOCK_MAP matching logic from ensure_static_script."""
+
+ def _resolve(
+ self,
+ blocks: list[tuple[str, str]],
+ module_attrs: list[str],
+ block_map: dict[str, str],
+ ) -> dict[str, str]:
+ """Simulate the BLOCK_MAP resolution loop from agent_functions.py.
+
+ Args:
+ blocks: List of (label, block_type) tuples
+ module_attrs: Function names available on the module
+ block_map: The BLOCK_MAP dict
+
+ Returns:
+ Dict mapping block_label -> cache_key
+ """
+ result: dict[str, str] = {}
+ module = types.ModuleType("test_module")
+ for attr in module_attrs:
+ setattr(module, attr, lambda: None)
+
+ for label, block_type in blocks:
+ if not label:
+ continue
+ if hasattr(module, label):
+ cache_key = label
+ else:
+ cache_key = block_map.get(block_type, None)
+ if not cache_key or not hasattr(module, cache_key):
+ continue
+ result[label] = cache_key
+ return result
+
+ def test_exact_match(self) -> None:
+ result = self._resolve(
+ blocks=[("create_account", "login")],
+ module_attrs=["create_account"],
+ block_map={},
+ )
+ assert result == {"create_account": "create_account"}
+
+ def test_block_map_match(self) -> None:
+ result = self._resolve(
+ blocks=[("register_or_login", "login"), ("fill_and_submit", "navigation")],
+ module_attrs=["create_account", "fill_application"],
+ block_map={"login": "create_account", "navigation": "fill_application"},
+ )
+ assert result == {
+ "register_or_login": "create_account",
+ "fill_and_submit": "fill_application",
+ }
+
+ def test_wait_block_skipped(self) -> None:
+ result = self._resolve(
+ blocks=[("register_or_login", "login"), ("wait_block", "wait"), ("fill_app", "navigation")],
+ module_attrs=["create_account", "fill_application"],
+ block_map={"login": "create_account", "navigation": "fill_application"},
+ )
+ assert "wait_block" not in result
+ assert len(result) == 2
+
+ def test_no_label_skipped(self) -> None:
+ result = self._resolve(
+ blocks=[("", "login")],
+ module_attrs=["create_account"],
+ block_map={"login": "create_account"},
+ )
+ assert result == {}
+
+ def test_block_map_typo_skipped(self) -> None:
+ """BLOCK_MAP points to non-existent function — should skip, not crash."""
+ result = self._resolve(
+ blocks=[("my_login", "login")],
+ module_attrs=["create_account"],
+ block_map={"login": "nonexistent_function"},
+ )
+ assert result == {}
+
+ def test_block_map_zero_matches_when_empty(self) -> None:
+ result = self._resolve(
+ blocks=[("block1", "custom_type")],
+ module_attrs=["some_func"],
+ block_map={"login": "create_account"},
+ )
+ assert result == {}
+
+ def test_three_blocks_with_mixed_matching(self) -> None:
+ """Workflow with 3 blocks: exact match, BLOCK_MAP, and unmapped."""
+ result = self._resolve(
+ blocks=[
+ ("create_account", "login"),
+ ("fill_form", "navigation"),
+ ("unknown_step", "extraction"),
+ ],
+ module_attrs=["create_account", "fill_application"],
+ block_map={"navigation": "fill_application"},
+ )
+ assert result == {
+ "create_account": "create_account",
+ "fill_form": "fill_application",
+ }
+ assert "unknown_step" not in result
+
+
+# ---------------------------------------------------------------------------
+# mode="direct" on click() and fill()
+# ---------------------------------------------------------------------------
+
+
+class TestModeDirectClick:
+ """Test that mode='direct' short-circuits before backward compat / validation."""
+
+ @pytest.mark.asyncio
+ async def test_direct_click_requires_selector(self) -> None:
+ from skyvern.core.script_generations.skyvern_page import SkyvernPage
+
+ page = MagicMock()
+ skyvern_page = SkyvernPage.__new__(SkyvernPage)
+ skyvern_page.page = page
+
+ with pytest.raises(ValueError, match="mode='direct' requires a selector"):
+ await skyvern_page.click(mode="direct")
+
+ @pytest.mark.asyncio
+ async def test_direct_fill_requires_selector(self) -> None:
+ from skyvern.core.script_generations.skyvern_page import SkyvernPage
+
+ page = MagicMock()
+ skyvern_page = SkyvernPage.__new__(SkyvernPage)
+ skyvern_page.page = page
+
+ with pytest.raises(ValueError, match="mode='direct' requires a selector"):
+ await skyvern_page.fill(value="test", mode="direct")
+
+ @pytest.mark.asyncio
+ async def test_direct_fill_requires_value(self) -> None:
+ from skyvern.core.script_generations.skyvern_page import SkyvernPage
+
+ page = MagicMock()
+ skyvern_page = SkyvernPage.__new__(SkyvernPage)
+ skyvern_page.page = page
+
+ with pytest.raises(ValueError, match="mode='direct' requires a value"):
+ await skyvern_page.fill(selector="input", mode="direct")
+
+
+# ---------------------------------------------------------------------------
+# Consecutive validation failure limit
+# ---------------------------------------------------------------------------
+
+
+class TestValidationFailureLimit:
+ def test_counter_logic(self) -> None:
+ """Verify the consecutive failure counter logic."""
+ consecutive = 0
+ max_failures = 3
+ pages = []
+
+ for page_num in range(10):
+ has_errors = page_num < 5 # first 5 pages have errors
+ if has_errors:
+ consecutive += 1
+ if consecutive >= max_failures:
+ pages.append(f"p{page_num}:STOPPED")
+ break
+ pages.append(f"p{page_num}:retry")
+ else:
+ consecutive = 0
+ pages.append(f"p{page_num}:ok")
+
+ # Should stop at page 2 (3rd consecutive failure)
+ assert len(pages) == 3
+ assert pages[-1] == "p2:STOPPED"