Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Render the login block URL before the credential profile boot (SKY-13479) (#8066)
Celal Zamanoğlu8fe8134397ee
diff --git a/skyvern/forge/sdk/workflow/service.py b/skyvern/forge/sdk/workflow/service.pyindex 8d3eee9dc..b5a24499c 100644--- a/skyvern/forge/sdk/workflow/service.py+++ b/skyvern/forge/sdk/workflow/service.py@@ -219,6 +219,7 @@ from skyvern.utils.css_selector import build_action_summaries_with_timing # shafrom skyvern.utils.secret_headers import merge_masked_headersfrom skyvern.utils.secret_redaction import redact_console_log_bytes, redact_har_bytesfrom skyvern.utils.strings import is_uuid+from skyvern.utils.url_validators import prepend_scheme_and_validate_urlfrom skyvern.utils.url_validators import validate_url as validate_url_with_blocked_host_checkfrom skyvern.webeye.actions.action_types import ActionTypefrom skyvern.webeye.browser_state import BrowserState@@ -5534,6 +5535,36 @@ class WorkflowService:exc_info=True,)+ def _rendered_login_block_url(self, block: BlockTypeVar, workflow_run_id: str) -> str | None:+ # The profile boot resolves the url before the block does, so mirror the block's own order: a+ # direct parameter key, then jinja. The blocked-host check still runs downstream at navigation.+ if not block.url:+ return block.url++ try:+ workflow_run_context = app.WORKFLOW_CONTEXT_MANAGER.get_workflow_run_context(workflow_run_id)+ raw_url = block.url+ if workflow_run_context.has_parameter(raw_url) and workflow_run_context.has_value(raw_url):+ raw_url = workflow_run_context.get_value(raw_url) or raw_url+ rendered_url = block.format_block_parameter_template_from_workflow_run_context(+ raw_url, workflow_run_context+ )+ if not rendered_url:+ return block.url+ # A no-op resolution stays byte-identical, except for a scheme-less value: those never boot+ # today, so prepending the scheme the block would have added cannot change working behavior.+ if rendered_url == block.url and rendered_url.lower().startswith(("http://", "https://")):+ return block.url+ return prepend_scheme_and_validate_url(rendered_url)+ except Exception:+ LOG.warning(+ "Login block URL did not render for the profile boot; using the raw block URL",+ workflow_run_id=workflow_run_id,+ block_label=block.label,+ exc_info=True,+ )+ return block.url+async def _prepare_login_block_browser_profile(self,*,@@ -5588,6 +5619,7 @@ class WorkflowService:# retries don't stack the browser-session prefix repeatedly.original_navigation_goal = block.navigation_goalif resolved_browser_profile_id:+ login_url = self._rendered_login_block_url(block, workflow_run_id)decision = await self._evaluate_debug_session_profile_decision(workflow_run=workflow_run,browser_session_id=browser_session_id,@@ -5612,11 +5644,11 @@ class WorkflowService:browser_session_id=browser_session_id,credential_browser_profile_id=resolved_browser_profile_id,)- if decision.attach_browser_session_id and block.url:+ if decision.attach_browser_session_id and login_url:try:await app.BROWSER_MANAGER.get_or_create_for_workflow_run(workflow_run=workflow_run,- url=block.url,+ url=login_url,browser_session_id=decision.attach_browser_session_id,)except Exception:@@ -5633,7 +5665,7 @@ class WorkflowService:workflow_run_id=workflow_run_id,block_label=block.label,browser_profile_id=resolved_browser_profile_id,- url=block.url,+ url=login_url,)# A prior block may already have opened this run's browser; get_or_create_for_workflow_run# then returns that cached context and ignores browser_profile_id, so the credential@@ -5685,18 +5717,18 @@ class WorkflowService:# Create the browser with the saved profile and navigate to the login block's URL. The# user enters the post-login target URL; the saved cookies authenticate once it loads.- profile_loaded = bool(block.url) and not browser_already_open- if block.url and not browser_already_open:+ profile_loaded = bool(login_url) and not browser_already_open+ if login_url and not browser_already_open:try:browser_state = await app.BROWSER_MANAGER.get_or_create_for_workflow_run(workflow_run=workflow_run,- url=block.url,+ url=login_url,browser_profile_id=resolved_browser_profile_id,browser_session_id=decision.attach_browser_session_id,)working_page = await browser_state.get_working_page()if working_page and working_page.url == "about:blank":- await browser_state.navigate_to_url(page=working_page, url=block.url)+ await browser_state.navigate_to_url(page=working_page, url=login_url)# Wait for the page to settle so cookies/redirects completeif working_page:try:diff --git a/tests/unit/test_seed_precedence_engine.py b/tests/unit/test_seed_precedence_engine.pyindex 27fe40448..36cd3c5e3 100644--- a/tests/unit/test_seed_precedence_engine.py+++ b/tests/unit/test_seed_precedence_engine.py@@ -2,12 +2,13 @@ from __future__ import annotationsfrom datetime import datetime, timezonefrom types import SimpleNamespace-from unittest.mock import AsyncMock+from unittest.mock import AsyncMock, MagicMockimport pytestfrom skyvern.forge import appfrom skyvern.forge.sdk.db.enums import BrowserSeedSource+from skyvern.forge.sdk.workflow.context_manager import WorkflowContextManager, WorkflowRunContextfrom skyvern.forge.sdk.workflow.models.block import LoginBlockfrom skyvern.forge.sdk.workflow.models.parameter import (CredentialParameter,@@ -808,9 +809,14 @@ def _output_parameter(key: str) -> OutputParameter:return OutputParameter(output_parameter_id=f"{key}_id", key=key, workflow_id="wf", created_at=now, modified_at=now)-def _login_block(label: str, credential_param: CredentialParameter | WorkflowParameter) -> LoginBlock:+def _login_block(+ label: str,+ credential_param: CredentialParameter | WorkflowParameter,+ *,+ url: str = "https://example.com/login",+) -> LoginBlock:return LoginBlock(- url="https://example.com/login",+ url=url,label=label,title=label,navigation_goal="log in",@@ -819,6 +825,18 @@ def _login_block(label: str, credential_param: CredentialParameter | WorkflowPar)+def _string_parameter(key: str) -> WorkflowParameter:+ now = datetime.now(timezone.utc)+ return WorkflowParameter(+ key=key,+ workflow_parameter_id=f"wp_{key}",+ workflow_parameter_type=WorkflowParameterType.STRING,+ workflow_id="wf_test",+ created_at=now,+ modified_at=now,+ )++def _workflow_credential_parameter(key: str, default_value: str | None) -> WorkflowParameter:now = datetime.now(timezone.utc)return WorkflowParameter(@@ -1216,3 +1234,165 @@ async def test_login_block_no_cached_browser_still_credential_seeds(monkeypatch:browser_profile_id="cred_profile_x",browser_seed_source=BrowserSeedSource.credential,)+++async def _prepare_login_block_profile_boot(+ monkeypatch: pytest.MonkeyPatch,+ *,+ url: str,+ values: dict[str, str],+ get_or_create: AsyncMock,+ get_workflow_run: AsyncMock | None = None,+) -> tuple[object, AsyncMock, LoginBlock]:+ context = WorkflowRunContext(+ workflow_title="Workflow",+ workflow_id="wf_test",+ workflow_permanent_id="wpid",+ workflow_run_id="wr_test",+ aws_client=MagicMock(),+ )+ context.values.update(values)+ for key in values:+ context.parameters[key] = _string_parameter(key)+ # The stub app auto-mocks WORKFLOW_CONTEXT_MANAGER attributes as AsyncMocks, so the real manager is+ # installed here to exercise the actual context lookup the renderer does.+ context_manager = WorkflowContextManager()+ context_manager.workflow_run_contexts["wr_test"] = context+ monkeypatch.setattr(app, "WORKFLOW_CONTEXT_MANAGER", context_manager)++ svc = WorkflowService()+ monkeypatch.setattr(svc, "_apply_login_block_credential_proxy_pin", AsyncMock())+ monkeypatch.setattr(svc, "_resolve_login_block_browser_profile_id", AsyncMock(return_value="cred_profile_x"))+ monkeypatch.setattr(+ svc,+ "_evaluate_debug_session_profile_decision",+ AsyncMock(return_value=SimpleNamespace(incompatible_reason=None, attach_browser_session_id=None)),+ )+ monkeypatch.setattr(app.BROWSER_MANAGER, "get_for_workflow_run", lambda *a, **k: None)+ monkeypatch.setattr(app.BROWSER_MANAGER, "get_or_create_for_workflow_run", get_or_create)+ update = AsyncMock()+ monkeypatch.setattr(app.DATABASE.workflow_runs, "update_workflow_run", update)+ monkeypatch.setattr(+ app.DATABASE.workflow_runs,+ "get_workflow_run",+ get_workflow_run or AsyncMock(return_value=None),+ )++ block = _login_block("login", _credential_parameter("login", "cred_1"), url=url)+ run = SimpleNamespace(+ browser_seed_source=BrowserSeedSource.credential,+ start_fresh_browser=False,+ workflow_permanent_id="wpid",+ )+ result = await svc._prepare_login_block_browser_profile(+ block=block,+ workflow_run=run, # type: ignore[arg-type]+ workflow_run_id="wr_test",+ organization_id="o_test",+ browser_session_id=None,+ )+ return result, update, block+++ ("raw_url", "values", "expected_url"),+ [+ pytest.param(+ "{{ some_url_parameter }}",+ {"some_url_parameter": "login.example/session"},+ "https://login.example/session",+ id="templated",+ ),+ pytest.param(+ "https://login.example/session",+ {},+ "https://login.example/session",+ id="literal",+ ),+ pytest.param(+ "some_url_parameter",+ {"some_url_parameter": "https://login.example/session"},+ "https://login.example/session",+ id="direct_parameter_key",+ ),+ pytest.param(+ # Pins the resolution ORDER: the direct key must be substituted BEFORE jinja runs, or the+ # nested template survives unrendered.+ "url_param",+ {"url_param": "{{ host }}/login", "host": "login.example"},+ "https://login.example/login",+ id="direct_parameter_key_holding_a_template",+ ),+ pytest.param(+ "login.example/session",+ {},+ "https://login.example/session",+ id="schemeless_literal_is_normalized",+ ),+ pytest.param(+ "https://www.www.login.example/session",+ {},+ "https://www.www.login.example/session",+ id="schemed_literal_is_passed_through_uncanonicalized",+ ),+ pytest.param(+ "not a url",+ {},+ "not a url",+ id="schemeless_literal_that_fails_validation_keeps_raw",+ ),+ pytest.param(+ "{{ some_url_parameter }}",+ {"some_url_parameter": "not a url"},+ "{{ some_url_parameter }}",+ id="renders_to_invalid_url_keeps_raw",+ ),+ ],+)+async def test_login_block_profile_boot_resolves_url(+ monkeypatch: pytest.MonkeyPatch,+ raw_url: str,+ values: dict[str, str],+ expected_url: str,+) -> None:+ page = SimpleNamespace(url=expected_url, wait_for_load_state=AsyncMock())+ get_or_create = AsyncMock(return_value=SimpleNamespace(get_working_page=AsyncMock(return_value=page)))++ _, _, block = await _prepare_login_block_profile_boot(+ monkeypatch,+ url=raw_url,+ values=values,+ get_or_create=get_or_create,+ )++ assert get_or_create.await_args.kwargs["url"] == expected_url+ assert block.url == raw_url+++async def test_login_block_profile_boot_unresolved_url_degrades_without_raising(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ get_or_create = AsyncMock(side_effect=RuntimeError("profile boot failed"))+ degraded_run = SimpleNamespace(+ browser_seed_source=BrowserSeedSource.degraded_fresh,+ browser_profile_id=None,+ )++ raw_url = "{{ missing_url_parameter }}"+ result, update, _ = await _prepare_login_block_profile_boot(+ monkeypatch,+ url=raw_url,+ values={},+ get_or_create=get_or_create,+ get_workflow_run=AsyncMock(side_effect=[None, degraded_run]),+ )++ assert get_or_create.await_args.kwargs["url"] == raw_url+ assert update.await_args_list[-1].kwargs == {+ "workflow_run_id": "wr_test",+ "browser_profile_id": None,+ "browser_seed_source": BrowserSeedSource.degraded_fresh,+ }+ assert result is degraded_run