Skip to content

Automate browser based workflows with AI

perf(SKY-10319): replace fixed upload-file sleeps with page readiness wait (#6305)

LawyZheng6af2e02e449d
diff --git a/skyvern/webeye/actions/handler.py b/skyvern/webeye/actions/handler.py
index cedc77171..bc6e95455 100644
--- a/skyvern/webeye/actions/handler.py
+++ b/skyvern/webeye/actions/handler.py
@@ -2162,6 +2162,30 @@ def _find_similar_url_in_text(candidate_url: str, text: str) -> str | None:
return matched
+async def _wait_for_upload_processing(page: Page) -> None:
+ """Wait for page readiness signals after a file upload.
+
+ Covers upload-processing UI (spinners, progress bars, DOM updates) beyond
+ bare networkidle by reusing SkyvernFrame.wait_for_page_ready with
+ upload-tuned timeouts that keep worst-case well below the old 10-15 s sleep.
+ """
+ try:
+ # Settle delay: let the page react to the file-input change and mount
+ # upload UI (spinner, progress bar, XHR) before polling for readiness.
+ await asyncio.sleep(0.5)
+ skyvern_frame = await SkyvernFrame.create_instance(page)
+ await skyvern_frame.wait_for_page_ready(
+ loading_indicator_timeout_ms=3000,
+ network_idle_timeout_ms=3000,
+ dom_stable_ms=300,
+ dom_stability_timeout_ms=2000,
+ )
+ except (TimeoutError, asyncio.TimeoutError):
+ LOG.info("Upload processing page-ready wait timed out, continuing")
+ except PlaywrightError:
+ LOG.warning("Upload processing page-ready wait interrupted by Playwright error, continuing", exc_info=True)
+
+
@traced(name="skyvern.agent.action.upload_file")
async def handle_upload_file_action(
action: actions.UploadFileAction,
@@ -2237,8 +2261,7 @@ async def handle_upload_file_action(
timeout=settings.BROWSER_ACTION_TIMEOUT_MS,
)
- # Sleep for 10 seconds after uploading a file to let the page process it
- await asyncio.sleep(10)
+ await _wait_for_upload_processing(page)
return [ActionSuccess()]
else:
@@ -3468,14 +3491,13 @@ async def chain_click(
return action_results
finally:
- # FIXME: use 'page.wait_for_event("filechooser", timeout)' to wait for the file to be uploaded instead of hardcoding sleeping time
click_succeeded = any(isinstance(r, ActionSuccess) for r in action_results)
if is_filechooser_trigger:
# File chooser opened during this click — upload completed normally
LOG.info("File chooser triggered during this click", action=action)
if file:
- await asyncio.sleep(15)
+ await _wait_for_upload_processing(page)
if not has_pending:
page.remove_listener("filechooser", fc_func)
if context is not None and context.pending_file_chooser is not None:
@@ -3511,7 +3533,7 @@ async def chain_click(
):
# A previous UPLOAD_FILE's deferred listener was consumed by this click
LOG.info("Pending file chooser from previous UPLOAD_FILE was consumed by this click", action=action)
- await asyncio.sleep(15)
+ await _wait_for_upload_processing(page)
context.cleanup_pending_file_chooser()
else:
diff --git a/tests/unit/test_upload_file_wait.py b/tests/unit/test_upload_file_wait.py
new file mode 100644
index 000000000..26c33c6d8
--- /dev/null
+++ b/tests/unit/test_upload_file_wait.py
@@ -0,0 +1,119 @@
+"""Tests for _wait_for_upload_processing helper and regression guards."""
+
+from __future__ import annotations
+
+import ast
+import asyncio
+import inspect
+import textwrap
+from unittest.mock import AsyncMock, patch
+
+import pytest
+from playwright._impl._errors import Error as PlaywrightError
+from playwright.async_api import TimeoutError as PlaywrightTimeoutError
+
+from skyvern.webeye.actions import handler as handler_module
+from skyvern.webeye.actions.handler import _wait_for_upload_processing
+
+# ---------------------------------------------------------------------------
+# Helper behaviour tests
+# ---------------------------------------------------------------------------
+
+
+async def test_calls_wait_for_page_ready_with_settle_delay() -> None:
+ mock_frame = AsyncMock()
+ with (
+ patch("skyvern.webeye.actions.handler.SkyvernFrame.create_instance", new_callable=AsyncMock) as mock_create,
+ patch("skyvern.webeye.actions.handler.asyncio.sleep", new_callable=AsyncMock) as mock_sleep,
+ ):
+ mock_create.return_value = mock_frame
+ await _wait_for_upload_processing(AsyncMock())
+
+ # Settle delay before readiness polling
+ mock_sleep.assert_awaited_once_with(0.5)
+ mock_frame.wait_for_page_ready.assert_awaited_once_with(
+ loading_indicator_timeout_ms=3000,
+ network_idle_timeout_ms=3000,
+ dom_stable_ms=300,
+ dom_stability_timeout_ms=2000,
+ )
+
+
+async def test_swallows_playwright_timeout() -> None:
+ mock_frame = AsyncMock()
+ mock_frame.wait_for_page_ready.side_effect = PlaywrightTimeoutError("Timeout 3000ms exceeded")
+ with patch("skyvern.webeye.actions.handler.SkyvernFrame.create_instance", new_callable=AsyncMock) as mock_create:
+ mock_create.return_value = mock_frame
+ await _wait_for_upload_processing(AsyncMock())
+
+
+async def test_swallows_asyncio_timeout() -> None:
+ mock_frame = AsyncMock()
+ mock_frame.wait_for_page_ready.side_effect = asyncio.TimeoutError()
+ with patch("skyvern.webeye.actions.handler.SkyvernFrame.create_instance", new_callable=AsyncMock) as mock_create:
+ mock_create.return_value = mock_frame
+ await _wait_for_upload_processing(AsyncMock())
+
+
+async def test_swallows_playwright_error() -> None:
+ mock_frame = AsyncMock()
+ mock_frame.wait_for_page_ready.side_effect = PlaywrightError("Target page, context or browser has been closed")
+ with patch("skyvern.webeye.actions.handler.SkyvernFrame.create_instance", new_callable=AsyncMock) as mock_create:
+ mock_create.return_value = mock_frame
+ await _wait_for_upload_processing(AsyncMock())
+
+
+async def test_propagates_non_playwright_error() -> None:
+ mock_frame = AsyncMock()
+ mock_frame.wait_for_page_ready.side_effect = RuntimeError("unexpected bug")
+ with patch("skyvern.webeye.actions.handler.SkyvernFrame.create_instance", new_callable=AsyncMock) as mock_create:
+ mock_create.return_value = mock_frame
+ with pytest.raises(RuntimeError, match="unexpected bug"):
+ await _wait_for_upload_processing(AsyncMock())
+
+
+# ---------------------------------------------------------------------------
+# Static regression guards
+# ---------------------------------------------------------------------------
+
+
+def _get_all_sleep_calls(source: str) -> list[tuple[int, float | str]]:
+ results = []
+ tree = ast.parse(textwrap.dedent(source))
+ for node in ast.walk(tree):
+ if not isinstance(node, ast.Await):
+ continue
+ call = node.value
+ if not isinstance(call, ast.Call):
+ continue
+ func = call.func
+ is_sleep = (
+ isinstance(func, ast.Attribute)
+ and func.attr == "sleep"
+ and isinstance(func.value, ast.Name)
+ and func.value.id == "asyncio"
+ )
+ if is_sleep and call.args:
+ arg = call.args[0]
+ if isinstance(arg, ast.Constant):
+ results.append((node.lineno, arg.value))
+ return results
+
+
+def test_no_fixed_sleep_10_in_handle_upload_file_action() -> None:
+ # Regression guard: this function previously blocked the agent for 10s unconditionally.
+ source = inspect.getsource(handler_module.handle_upload_file_action)
+ for lineno, value in _get_all_sleep_calls(source):
+ assert value != 10, f"Found asyncio.sleep(10) at line {lineno}"
+
+
+def test_no_fixed_sleep_15_in_chain_click() -> None:
+ # Regression guard: this function previously blocked the agent for 15s unconditionally.
+ source = inspect.getsource(handler_module.chain_click)
+ for lineno, value in _get_all_sleep_calls(source):
+ assert value != 15, f"Found asyncio.sleep(15) at line {lineno}"