Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Read adopted-session blob downloads from every open browser tab (SKY-12621) (#7838)
Shuchang Zheng0c243ea631f7
diff --git a/skyvern/webeye/actions/handler.py b/skyvern/webeye/actions/handler.pyindex 2e630958c..d95411029 100644--- a/skyvern/webeye/actions/handler.py+++ b/skyvern/webeye/actions/handler.py@@ -733,6 +733,33 @@ def _download_target_path(download_dir: Path, suggested_filename: str | None) ->return download_dir / f"{uuid.uuid4()}-{stem or 'download'}{suffix}"+def _blob_download_candidate_pages(download: Download, page: Page) -> list[Page]:+ """Pages to try when reading a blob: download's bytes, owner-first and deduped.++ A blob: URL only resolves inside the document that minted it, and a download-triggering+ click frequently opens that document in a new tab, so the owning page may not be the one+ the action ran on. Fan out over every open page the way the CDP download monitor does.+ """+ candidates: list[Page] = []+ seen: set[int] = set()++ def _add(candidate: Page | None) -> None:+ if candidate is None or id(candidate) in seen:+ return+ seen.add(id(candidate))+ candidates.append(candidate)++ _add(download.page)+ _add(page)+ try:+ context_pages = list(page.context.pages)+ except Exception:+ context_pages = []+ for context_page in context_pages:+ _add(context_page)+ return candidates++async def _save_adopted_session_download(download: Download,page: Page,@@ -766,12 +793,25 @@ async def _save_adopted_session_download(# Ordering: ``save_as`` above has already run and failed (empty or raised).# ``blob:`` URLs cannot be fetched via APIRequestContext (Playwright rejects- # the scheme), so route them through an in-page fetch from a same-origin frame.+ # the scheme), so route them through an in-page fetch from the document that+ # owns the blob. That document may be a different tab than the one clicked, so+ # probe every open page (owner first) rather than reading from ``page`` alone.if download.url.startswith("blob:"):- blob_bytes = await SkyvernFrame.read_blob_url_bytes(- page=page, blob_url=download.url, workflow_run_id=workflow_run_id- )+ candidate_pages = _blob_download_candidate_pages(download, page)+ blob_bytes: bytes | None = None+ for candidate in candidate_pages:+ blob_bytes = await SkyvernFrame.read_blob_url_bytes(+ page=candidate, blob_url=download.url, workflow_run_id=workflow_run_id, probe=True+ )+ if blob_bytes is not None:+ breakif blob_bytes is None:+ LOG.warning(+ "Adopted-session blob download could not be read from any open page",+ download_dir=str(download_dir),+ workflow_run_id=workflow_run_id,+ candidate_page_count=len(candidate_pages),+ )return Nonedownload_target.write_bytes(blob_bytes)return download_targetdiff --git a/tests/unit/test_adopted_session_download_save.py b/tests/unit/test_adopted_session_download_save.pyindex 06d9dfbc9..849c7ea0e 100644--- a/tests/unit/test_adopted_session_download_save.py+++ b/tests/unit/test_adopted_session_download_save.py@@ -26,6 +26,9 @@ def _download(suggested: str = "153743777.pdf", url: str = "https://example.com/download.suggested_filename = suggesteddownload.url = urldownload.save_as = AsyncMock()+ # Playwright reports the owning page here; default to unknown so tests exercise the+ # click-page + context fan-out unless they set it explicitly.+ download.page = Nonereturn download@@ -190,6 +193,9 @@ def _blob_capable_page(*frames: MagicMock, main_frame_url: str = OTHER_ORIGIN_FRside_effect=Exception("page.evaluate must not be called when matched frame is a sub-frame"))page.frames = list(frames)+ # A lone adopted-session page is the only page in its context by default; multi-tab+ # tests override this to add the blob's true owner.+ page.context.pages = [page]return page@@ -361,6 +367,73 @@ async def test_blob_url_main_frame_routes_through_main_world_prefix_when_configuclear_main_world_prefix(context)+# ---------------------------------------------------------------------------+# Multi-tab blobs: "View Document" opens the statement in a new tab, so the blob is+# owned by a page other than the one clicked. The helper must fan out over every open+# page in the context instead of reading from the click page alone (SKY-12621).+# ---------------------------------------------------------------------------+++async def test_blob_url_reads_from_other_context_page_when_click_page_lacks_blob(tmp_path) -> None:+ download = _download(url=BLOB_URL)+ download.save_as.side_effect = Exception("closed")++ click_page = _blob_capable_page(_frame(OTHER_ORIGIN_FRAME_URL))+ owner_frame = _frame(+ BLOB_ORIGIN_FRAME_URL,+ evaluate_return={"ok": True, "base64": base64.b64encode(PDF_BODY).decode("ascii")},+ )+ owner_page = _blob_capable_page(owner_frame)+ click_page.context.pages = [click_page, owner_page]++ saved = await _save_adopted_session_download(download, click_page, tmp_path, workflow_run_id="wr")++ assert saved is not None and saved.exists(), "blob bytes from the owning tab must be persisted"+ assert saved.read_bytes() == PDF_BODY+ owner_frame.evaluate.assert_awaited_once()+ assert sorted(p.name for p in tmp_path.iterdir()) == [saved.name]+++async def test_blob_url_prefers_download_owner_page(tmp_path) -> None:+ download = _download(url=BLOB_URL)+ download.save_as.side_effect = Exception("closed")++ owner_frame = _frame(+ BLOB_ORIGIN_FRAME_URL,+ evaluate_return={"ok": True, "base64": base64.b64encode(PDF_BODY).decode("ascii")},+ )+ owner_page = _blob_capable_page(owner_frame)+ click_frame = _frame(+ BLOB_ORIGIN_FRAME_URL,+ evaluate_return={"ok": True, "base64": base64.b64encode(b"WRONG").decode("ascii")},+ )+ click_page = _blob_capable_page(click_frame)+ click_page.context.pages = [click_page, owner_page]+ download.page = owner_page++ saved = await _save_adopted_session_download(download, click_page, tmp_path, workflow_run_id="wr")++ assert saved is not None and saved.read_bytes() == PDF_BODY+ owner_frame.evaluate.assert_awaited_once()+ click_frame.evaluate.assert_not_awaited()+++async def test_blob_url_no_page_owns_blob_returns_none(tmp_path) -> None:+ download = _download(url=BLOB_URL)+ download.save_as.side_effect = Exception("closed")+ click_page = _blob_capable_page(_frame(OTHER_ORIGIN_FRAME_URL))+ other_page = _blob_capable_page(_frame(OTHER_ORIGIN_FRAME_URL))+ click_page.context.pages = [click_page, other_page]++ saved = await _save_adopted_session_download(download, click_page, tmp_path, workflow_run_id="wr")++ assert saved is None+ assert list(tmp_path.iterdir()) == []++@pytest.mark.asyncioasync def test_blob_url_matches_frame_whose_url_is_itself_blob(tmp_path) -> None:"""A frame whose own ``url`` is a ``blob:`` URL with the same origin is a valid