Skip to content

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

Fix environment isolation to prevent flaky test behavior

revanthkumar969366738c1385
diff --git a/tests/ci/browser/test_navigation_slow_pages.py b/tests/ci/browser/test_navigation_slow_pages.py
index da685e519..a35ff1486 100644
--- a/tests/ci/browser/test_navigation_slow_pages.py
+++ b/tests/ci/browser/test_navigation_slow_pages.py
@@ -200,27 +200,34 @@ class TestHeavyPageNavigation:
This test verifies the fix by directly checking the timeout parameter passed to CDP,
avoiding reliance on agent navigation flow or timing assumptions.
+ Environment is properly isolated to prevent flaky behavior.
"""
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"]}'
- )
+ # Save and completely clear env var to ensure test isolation
+ old_value = os.environ.pop('TIMEOUT_NavigateToUrlEvent', None)
- # Test 2: Env var override behavior
- old_value = os.environ.get('TIMEOUT_NavigateToUrlEvent')
try:
+ # Test 1: Default behavior (30s timeout from env var default)
+ # Ensure no env var is set to test default behavior
+ if 'TIMEOUT_NavigateToUrlEvent' in os.environ:
+ del os.environ['TIMEOUT_NavigateToUrlEvent']
+
+ 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
os.environ['TIMEOUT_NavigateToUrlEvent'] = '40.0'
# Reset the mock for second test
@@ -237,7 +244,7 @@ class TestHeavyPageNavigation:
)
finally:
- # Restore original env var value
+ # Restore original env var value for test isolation
if old_value is None:
os.environ.pop('TIMEOUT_NavigateToUrlEvent', None)
else: