Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

Improve timeout env-var test to be more reliable and discriminating

revanthkumar960eaaf4599201
diff --git a/tests/ci/browser/test_navigation_slow_pages.py b/tests/ci/browser/test_navigation_slow_pages.py
index c62ec8419..d64db8f67 100644
--- a/tests/ci/browser/test_navigation_slow_pages.py
+++ b/tests/ci/browser/test_navigation_slow_pages.py
@@ -198,40 +198,40 @@ class TestHeavyPageNavigation:
async def test_timeout_env_var_controls_cdp_navigation(self, browser_session, heavy_base_url):
"""TIMEOUT_NavigateToUrlEvent env var should control the CDP Page.navigate() timeout.
- This test verifies the fix for: https://github.com/browser-use/browser-use/issues/XXX
- Previously, the env var only controlled the outer event timeout, but the CDP navigation
- call had a hardcoded 20s timeout that would fire first.
-
- Server delays 22 seconds (> default 20s, < env var 35s).
- Without the fix, CDP would timeout at 20s (before server responds).
- With the fix, CDP waits 35s, so server response at 22s succeeds.
+ This test verifies the fix by directly checking the timeout parameter passed to CDP,
+ avoiding reliance on agent navigation flow or timing assumptions.
"""
- # Set env var to 35 seconds (> 22s server delay, > 20s default, leaves room for lifecycle polling)
+ import unittest.mock
+ from browser_use.browser.session import NavigateToUrlEvent
+
+ # Test 1: Default behavior (30s timeout from env var default)
+ with unittest.mock.patch.object(browser_session, '_navigate_and_wait') as mock_nav:
+ event = NavigateToUrlEvent(url=f'{heavy_base_url}/quick')
+ # Set up the agent focus target that the method expects
+ browser_session.agent_focus_target_id = 'test'
+ await browser_session.on_NavigateToUrlEvent(event)
+
+ # Verify default 30s timeout is used when no env var is set
+ mock_nav.assert_called_once()
+ call_args = mock_nav.call_args
+ assert call_args.kwargs['nav_timeout'] == 30.0, f'Default nav_timeout should be 30.0s, got {call_args.kwargs["nav_timeout"]}'
+
+ # Test 2: Env var override behavior
old_value = os.environ.get('TIMEOUT_NavigateToUrlEvent')
try:
os.environ['TIMEOUT_NavigateToUrlEvent'] = '35.0'
-
- url = f'{heavy_base_url}/very-slow-22s'
- agent = Agent(
- task=f'Navigate to {url}',
- llm=create_mock_llm(actions=_nav_actions(url, 'Navigated successfully despite 22s delay')),
- browser_session=browser_session,
- )
-
- start = time.time()
- history = await asyncio.wait_for(agent.run(max_steps=3), timeout=90)
- elapsed = time.time() - start
-
- # Verify navigation succeeded
- assert len(history) > 0, 'Agent should have completed at least one step'
- assert history.final_result() is not None, 'Agent should return a final result'
-
- # The key test: navigation succeeded despite 22s delay (would fail at default 20s)
- # Agent navigates twice: once from URL in task, once from step 1
- # Each navigation takes ~22s, plus agent overhead
- assert elapsed >= 40, f'Should have navigated twice (~22s each), only took {elapsed:.1f}s'
- # Without the fix, this test would fail with "Page.navigate() timed out after 20.0s"
-
+
+ # Reset the mock for second test
+ with unittest.mock.patch.object(browser_session, '_navigate_and_wait') as mock_nav:
+ event = NavigateToUrlEvent(url=f'{heavy_base_url}/quick')
+ browser_session.agent_focus_target_id = 'test'
+ await browser_session.on_NavigateToUrlEvent(event)
+
+ # Verify env var value is passed as nav_timeout
+ mock_nav.assert_called_once()
+ call_args = mock_nav.call_args
+ assert call_args.kwargs['nav_timeout'] == 35.0, f'nav_timeout should be 35.0s from env var, got {call_args.kwargs["nav_timeout"]}'
+
finally:
# Restore original env var value
if old_value is None: