Skip to content

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

Patch api key

Magnus Müller8d3f30da8012
diff --git a/tests/ci/test_cloud_browser.py b/tests/ci/test_cloud_browser.py
index fa66d7ade..e6110ba7c 100644
--- a/tests/ci/test_cloud_browser.py
+++ b/tests/ci/test_cloud_browser.py
@@ -42,9 +42,12 @@ def mock_auth_config(temp_config_dir):
class TestCloudBrowserClient:
"""Test CloudBrowserClient class."""
- async def test_create_browser_success(self, mock_auth_config):
+ async def test_create_browser_success(self, mock_auth_config, monkeypatch):
"""Test successful cloud browser creation."""
+ # Clear environment variable so test uses mock_auth_config
+ monkeypatch.delenv('BROWSER_USE_API_KEY', raising=False)
+
# Mock response data matching the API
mock_response_data = {
'id': 'test-browser-id',
@@ -99,9 +102,12 @@ class TestCloudBrowserClient:
assert 'BROWSER_USE_API_KEY environment variable' in str(exc_info.value)
- async def test_create_browser_http_401(self, mock_auth_config):
+ async def test_create_browser_http_401(self, mock_auth_config, monkeypatch):
"""Test cloud browser creation with HTTP 401 response."""
+ # Clear environment variable so test uses mock_auth_config
+ monkeypatch.delenv('BROWSER_USE_API_KEY', raising=False)
+
with patch('httpx.AsyncClient') as mock_client_class:
mock_response = AsyncMock()
mock_response.status_code = 401
@@ -161,9 +167,12 @@ class TestCloudBrowserClient:
assert 'X-Browser-Use-API-Key' in call_args.kwargs['headers']
assert call_args.kwargs['headers']['X-Browser-Use-API-Key'] == 'env-test-token'
- async def test_stop_browser_success(self, mock_auth_config):
+ async def test_stop_browser_success(self, mock_auth_config, monkeypatch):
"""Test successful cloud browser session stop."""
+ # Clear environment variable so test uses mock_auth_config
+ monkeypatch.delenv('BROWSER_USE_API_KEY', raising=False)
+
# Mock response data for stop
mock_response_data = {
'id': 'test-browser-id',
@@ -202,9 +211,12 @@ class TestCloudBrowserClient:
assert call_args.kwargs['json'] == {'action': 'stop'}
assert 'X-Browser-Use-API-Key' in call_args.kwargs['headers']
- async def test_stop_browser_session_not_found(self, mock_auth_config):
+ async def test_stop_browser_session_not_found(self, mock_auth_config, monkeypatch):
"""Test stopping a browser session that doesn't exist."""
+ # Clear environment variable so test uses mock_auth_config
+ monkeypatch.delenv('BROWSER_USE_API_KEY', raising=False)
+
with patch('httpx.AsyncClient') as mock_client_class:
mock_response = AsyncMock()
mock_response.status_code = 404
@@ -235,9 +247,12 @@ class TestBrowserSessionCloudIntegration:
assert session.cloud_browser is True
assert session.browser_profile.use_cloud is True
- async def test_browser_session_cloud_browser_logic(self, mock_auth_config):
+ async def test_browser_session_cloud_browser_logic(self, mock_auth_config, monkeypatch):
"""Test that cloud browser profile settings work correctly."""
+ # Clear environment variable so test uses mock_auth_config
+ monkeypatch.delenv('BROWSER_USE_API_KEY', raising=False)
+
# Test cloud browser profile creation
profile = BrowserProfile(use_cloud=True)
assert profile.use_cloud is True
@@ -255,9 +270,12 @@ class TestBrowserSessionCloudIntegration:
mock_get_cdp_url.assert_called_once()
-async def test_get_cloud_browser_cdp_url_function(mock_auth_config):
+async def test_get_cloud_browser_cdp_url_function(mock_auth_config, monkeypatch):
"""Test the get_cloud_browser_cdp_url convenience function."""
+ # Clear environment variable so test uses mock_auth_config
+ monkeypatch.delenv('BROWSER_USE_API_KEY', raising=False)
+
mock_response_data = {
'id': 'test-browser-id',
'status': 'active',
@@ -304,9 +322,12 @@ async def test_cloud_browser_auth_error_no_fallback(temp_config_dir):
assert profile.is_local is False
-async def test_stop_cloud_browser_session_function(mock_auth_config):
+async def test_stop_cloud_browser_session_function(mock_auth_config, monkeypatch):
"""Test the stop_cloud_browser_session convenience function."""
+ # Clear environment variable so test uses mock_auth_config
+ monkeypatch.delenv('BROWSER_USE_API_KEY', raising=False)
+
mock_response_data = {
'id': 'test-browser-id',
'status': 'stopped',