Skip to content

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

Added debug logging and improve smoke test

matt-greathouse4c57a37bd56c
diff --git a/browser_use/browser/session.py b/browser_use/browser/session.py
index 1b20af9f9..a1aec30e0 100644
--- a/browser_use/browser/session.py
+++ b/browser_use/browser/session.py
@@ -1052,24 +1052,26 @@ class BrowserSession(BaseModel):
username = getattr(self.browser_profile, 'proxy_username', None)
password = getattr(self.browser_profile, 'proxy_password', None)
if not username or not password:
+ self.logger.debug('Proxy credentials not provided; skipping proxy auth setup')
return
- # Enable Fetch domain with auth handling
- # Try enabling globally
+ # Enable Fetch domain with auth handling (do not pause all requests)
try:
- await self._cdp_client_root.send.Fetch.enable(params={'handleAuthRequests': True, 'patterns': [{'urlPattern': '*'}]})
- except Exception:
- pass
+ await self._cdp_client_root.send.Fetch.enable(params={'handleAuthRequests': True})
+ self.logger.debug('Fetch.enable(handleAuthRequests=True) enabled on root client')
+ except Exception as e:
+ self.logger.debug(f'Fetch.enable on root failed: {type(e).__name__}: {e}')
- # Also try enabling on current session if available
+ # Also enable on the focused session if available to ensure events are delivered
try:
if self.agent_focus:
await self.agent_focus.cdp_client.send.Fetch.enable(
- params={'handleAuthRequests': True, 'patterns': [{'urlPattern': '*'}]},
+ params={'handleAuthRequests': True},
session_id=self.agent_focus.session_id,
)
- except Exception:
- pass
+ self.logger.debug('Fetch.enable(handleAuthRequests=True) enabled on focused session')
+ except Exception as e:
+ self.logger.debug(f'Fetch.enable on focused session failed: {type(e).__name__}: {e}')
def _on_auth_required(event: dict, session_id: SessionID | None = None):
# event keys may be snake_case or camelCase depending on generator; handle both
@@ -1092,7 +1094,7 @@ class BrowserSession(BaseModel):
session_id=session_id,
)
except Exception as e:
- self.logger.debug(f'Proxy auth respond failed: {e}')
+ self.logger.debug(f'Proxy auth respond failed: {type(e).__name__}: {e}')
# schedule
asyncio.create_task(_respond())
else:
@@ -1103,37 +1105,19 @@ class BrowserSession(BaseModel):
params={'requestId': request_id, 'authChallengeResponse': {'response': 'Default'}},
session_id=session_id,
)
- except Exception:
- pass
+ except Exception as e:
+ self.logger.debug(f'Default auth respond failed: {type(e).__name__}: {e}')
if request_id:
asyncio.create_task(_default())
- def _on_request_paused(event: dict, session_id: SessionID | None = None):
- # Continue all paused requests to avoid stalling the network
- request_id = event.get('requestId') or event.get('request_id')
- if not request_id:
- return
-
- async def _continue():
- try:
- await self._cdp_client_root.send.Fetch.continueRequest(
- params={'requestId': request_id},
- session_id=session_id,
- )
- except Exception:
- pass
-
- asyncio.create_task(_continue())
-
- # Register event handler on root client
+ # Register event handler on root client (and focused session if present)
try:
self._cdp_client_root.register.Fetch.authRequired(_on_auth_required)
- self._cdp_client_root.register.Fetch.requestPaused(_on_request_paused)
if self.agent_focus:
self.agent_focus.cdp_client.register.Fetch.authRequired(_on_auth_required)
- self.agent_focus.cdp_client.register.Fetch.requestPaused(_on_request_paused)
- except Exception:
- pass
+ self.logger.debug('Registered Fetch.authRequired handlers')
+ except Exception as e:
+ self.logger.debug(f'Failed to register authRequired handlers: {type(e).__name__}: {e}')
# Auto-enable Fetch on every newly attached target to ensure auth callbacks fire
def _on_attached(event: dict, session_id: SessionID | None = None):
@@ -1144,31 +1128,23 @@ class BrowserSession(BaseModel):
async def _enable():
try:
await self._cdp_client_root.send.Fetch.enable(
- params={'handleAuthRequests': True, 'patterns': [{'urlPattern': '*'}]},
+ params={'handleAuthRequests': True},
session_id=sid,
)
- except Exception:
- pass
+ self.logger.debug(f'Fetch.enable(handleAuthRequests=True) enabled on attached session {sid}')
+ except Exception as e:
+ self.logger.debug(f'Fetch.enable on attached session failed: {type(e).__name__}: {e}')
asyncio.create_task(_enable())
try:
self._cdp_client_root.register.Target.attachedToTarget(_on_attached)
- except Exception:
- pass
-
- # Ensure Fetch is enabled for the current focused session, too
- try:
- if self.agent_focus:
- await self.agent_focus.cdp_client.send.Fetch.enable(
- params={'handleAuthRequests': True, 'patterns': [{'urlPattern': '*'}]},
- session_id=self.agent_focus.session_id,
- )
- except Exception:
- pass
+ self.logger.debug('Registered Target.attachedToTarget handler for Fetch.enable')
+ except Exception as e:
+ self.logger.debug(f'Failed to register attachedToTarget handler: {type(e).__name__}: {e}')
except Exception as e:
- self.logger.debug(f'Skipping proxy auth setup: {e}')
+ self.logger.debug(f'Skipping proxy auth setup: {type(e).__name__}: {e}')
async def get_tabs(self) -> list[TabInfo]:
"""Get information about all open tabs using CDP Target.getTargetInfo for speed."""
diff --git a/tests/ci/test_proxy_smoke.py b/tests/ci/test_proxy_smoke.py
index a8901b405..d766ff249 100644
--- a/tests/ci/test_proxy_smoke.py
+++ b/tests/ci/test_proxy_smoke.py
@@ -19,6 +19,26 @@ def test_chromium_args_include_proxy_flags():
assert any(a == '--proxy-bypass-list=localhost,127.0.0.1' for a in args), args
+def test_chromium_args_include_pac_url_flag_and_exclusivity():
+ # PAC URL should produce --proxy-pac-url and not --proxy-server
+ profile = BrowserProfile(
+ headless=True,
+ user_data_dir=str(CONFIG.BROWSER_USE_PROFILES_DIR / 'proxy-smoke-pac'),
+ proxy_pac_url='http://proxy.local/proxy.pac',
+ )
+ args = profile.get_args()
+ assert any(a == '--proxy-pac-url=http://proxy.local/proxy.pac' for a in args), args
+ assert all(not a.startswith('--proxy-server=') for a in args), args
+
+ # Setting both PAC URL and server should raise
+ with pytest.raises(ValueError):
+ BrowserProfile(
+ headless=True,
+ user_data_dir=str(CONFIG.BROWSER_USE_PROFILES_DIR / 'proxy-smoke-bad'),
+ proxy_server='http://proxy.local:8080',
+ proxy_pac_url='http://proxy.local/proxy.pac',
+ )
+
@pytest.mark.asyncio
async def test_cdp_proxy_auth_handler_registers_and_responds():
# Create profile with proxy auth credentials
@@ -35,7 +55,9 @@ async def test_cdp_proxy_auth_handler_registers_and_responds():
def __init__(self) -> None:
self.enabled = False
self.last_auth: dict[str, Any] | None = None
+ self.last_default: dict[str, Any] | None = None
self.auth_callback = None
+ self.request_paused_callback = None
class _FetchSend:
def __init__(self, outer: 'StubCDP') -> None:
@@ -47,6 +69,10 @@ async def test_cdp_proxy_auth_handler_registers_and_responds():
async def continueWithAuth(self, params: dict, session_id: str | None = None) -> None:
self._outer.last_auth = {'params': params, 'session_id': session_id}
+ async def continueRequest(self, params: dict, session_id: str | None = None) -> None:
+ # no-op; included to mirror CDP API surface used by impl
+ pass
+
class _Send:
def __init__(self, outer: 'StubCDP') -> None:
self.Fetch = _FetchSend(outer)
@@ -58,6 +84,9 @@ async def test_cdp_proxy_auth_handler_registers_and_responds():
def authRequired(self, callback) -> None:
self._outer.auth_callback = callback
+ def requestPaused(self, callback) -> None:
+ self._outer.request_paused_callback = callback
+
class _Register:
def __init__(self, outer: 'StubCDP') -> None:
self.Fetch = _FetchRegister(outer)
@@ -82,10 +111,21 @@ async def test_cdp_proxy_auth_handler_registers_and_responds():
root.auth_callback(ev, session_id='s1') # type: ignore[misc]
# Let scheduled task run
- await asyncio.sleep(0.01)
+ await asyncio.sleep(0.05)
assert root.last_auth is not None
params = root.last_auth['params']
assert params['authChallengeResponse']['response'] == 'ProvideCredentials'
assert params['authChallengeResponse']['username'] == 'user'
assert params['authChallengeResponse']['password'] == 'pass'
+ assert root.last_auth['session_id'] == 's1'
+
+ # Now simulate a non-proxy auth challenge and ensure default handling
+ ev2 = {'requestId': 'r2', 'authChallenge': {'source': 'Server'}}
+ root.auth_callback(ev2, session_id='s2') # type: ignore[misc]
+ await asyncio.sleep(0.05)
+ # After non-proxy challenge, last_auth should reflect Default response
+ assert root.last_auth is not None
+ params2 = root.last_auth['params']
+ assert params2['requestId'] == 'r2'
+ assert params2['authChallengeResponse']['response'] == 'Default'