Skip to content

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

fixes

Nick Sweetingf4b1987761e5
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.py
index fcdc30b1a..bc53e56fc 100644
--- a/browser_use/agent/service.py
+++ b/browser_use/agent/service.py
@@ -70,6 +70,7 @@ from browser_use.telemetry.service import ProductTelemetry
from browser_use.telemetry.views import AgentTelemetryEvent
from browser_use.utils import (
BROWSER_USE_CLOUD_SYNC,
+ BROWSER_USE_CONFIG_DIR,
SKIP_LLM_API_KEY_VERIFICATION,
_log_pretty_path,
get_browser_use_version,
@@ -437,10 +438,8 @@ class Agent(Generic[Context]):
self.telemetry = ProductTelemetry()
# Event bus with WAL persistence
- # Default to ~/.config/browseruse/events/{agent_task_id}.jsonl
- from browser_use.utils import BROWSER_USE_CONFIG_DIR
-
- wal_path = BROWSER_USE_CONFIG_DIR / 'events' / f'{self.task_id}.jsonl'
+ # Default to ~/.config/browseruse/events/{agent_session_id}.jsonl
+ wal_path = BROWSER_USE_CONFIG_DIR / 'events' / f'{self.session_id}.jsonl'
self.eventbus = EventBus(name='Agent', wal_path=wal_path)
# Cloud sync service
diff --git a/browser_use/dom/service.py b/browser_use/dom/service.py
index 03782691c..6100fe150 100644
--- a/browser_use/dom/service.py
+++ b/browser_use/dom/service.py
@@ -6,7 +6,6 @@ from urllib.parse import urlparse
if TYPE_CHECKING:
from browser_use.browser.types import Page
-from dataclasses import dataclass
from browser_use.dom.views import (
DOMBaseNode,
diff --git a/browser_use/sync/auth.py b/browser_use/sync/auth.py
index d92e31de2..9118220ee 100644
--- a/browser_use/sync/auth.py
+++ b/browser_use/sync/auth.py
@@ -124,8 +124,8 @@ class DeviceAuthClient:
async def poll_for_token(
self,
device_code: str,
- interval: int = 5,
- timeout: int = 1800,
+ interval: float = 3.0,
+ timeout: float = 1800.0,
) -> dict | None:
"""
Poll for the access token.
diff --git a/browser_use/sync/service.py b/browser_use/sync/service.py
index bf5ad33c7..e24e34bfd 100644
--- a/browser_use/sync/service.py
+++ b/browser_use/sync/service.py
@@ -88,7 +88,7 @@ class CloudSync:
async def _background_auth(self, agent_session_id: str) -> None:
"""Run authentication in background"""
assert self.auth_client, 'enable_auth=True must be set before calling CloudSync_background_auth()'
-
+ assert self.session_id, 'session_id must be set before calling CloudSync._background_auth() can fire'
try:
# Run authentication
success = await self.auth_client.authenticate(
diff --git a/browser_use/utils.py b/browser_use/utils.py
index 4ee178294..46500154d 100644
--- a/browser_use/utils.py
+++ b/browser_use/utils.py
@@ -36,8 +36,8 @@ except ImportError:
BROWSER_USE_LOGGING_LEVEL = os.getenv('BROWSER_USE_LOGGING_LEVEL', 'info').lower()
ANONYMIZED_TELEMETRY = os.getenv('ANONYMIZED_TELEMETRY', 'true').lower()[:1] in 'ty1'
BROWSER_USE_CLOUD_SYNC = os.getenv('BROWSER_USE_CLOUD_SYNC', str(ANONYMIZED_TELEMETRY)).lower()[0] in 'ty1'
-BROWSER_USE_CLOUD_URL = os.getenv('BROWSER_USE_CLOUD_URL', 'https://cloud.browseruse.com') # or http://localhost:8000
-BROWSER_USE_CLOUD_UI_URL = os.getenv('BROWSER_USE_CLOUD_UI_URL', 'https://cloud.browseruse.com') # or http://localhost:3000
+BROWSER_USE_CLOUD_URL = os.getenv('BROWSER_USE_CLOUD_URL', 'https://cloud.browser-use.com') # or http://localhost:8000
+BROWSER_USE_CLOUD_UI_URL = os.getenv('BROWSER_USE_CLOUD_UI_URL', '') # or http://localhost:3000
assert '://' in BROWSER_USE_CLOUD_URL and '://' in BROWSER_USE_CLOUD_UI_URL, (
'BROWSER_USE_CLOUD_URL and BROWSER_USE_CLOUD_UI_URL must be valid URLs'
)
diff --git a/tests/ci/test_sync_client.py b/tests/ci/test_sync_client.py
index f541e67e3..634a41e27 100644
--- a/tests/ci/test_sync_client.py
+++ b/tests/ci/test_sync_client.py
@@ -3,7 +3,6 @@
import os
import tempfile
from pathlib import Path
-from unittest.mock import patch
import httpx
import pytest
@@ -38,26 +37,23 @@ class TestCloudSyncInit:
async def test_init_with_auth_enabled(self, temp_config_dir):
"""Test CloudSync initialization with auth enabled."""
- # Set test environment variable
- with patch.dict(os.environ, {'BROWSER_USE_CLOUD_URL': 'http://localhost:8000'}):
- service = CloudSync(enable_auth=True)
+ service = CloudSync(enable_auth=True, base_url='http://localhost:8000')
- assert service.base_url == 'http://localhost:8000'
- assert service.enable_auth is True
- assert service.auth_client is not None
- assert isinstance(service.auth_client, DeviceAuthClient)
- assert service.pending_events == []
- assert service.session_id is None
+ assert service.base_url == 'http://localhost:8000'
+ assert service.enable_auth is True
+ assert service.auth_client is not None
+ assert isinstance(service.auth_client, DeviceAuthClient)
+ assert service.pending_events == []
+ assert service.session_id is None
async def test_init_with_auth_disabled(self, temp_config_dir):
"""Test CloudSync initialization with auth disabled."""
- with patch.dict(os.environ, {'BROWSER_USE_CLOUD_URL': 'http://localhost:8000'}):
- service = CloudSync(enable_auth=False)
+ service = CloudSync(enable_auth=False, base_url='http://localhost:8000')
- assert service.base_url == 'http://localhost:8000'
- assert service.enable_auth is False
- assert service.auth_client is None
- assert service.pending_events == []
+ assert service.base_url == 'http://localhost:8000'
+ assert service.enable_auth is False
+ assert service.auth_client is None
+ assert service.pending_events == []
class TestCloudSyncEventHandling:
@@ -121,9 +117,9 @@ class TestCloudSyncEventHandling:
# Event should be queued
assert len(unauthenticated_sync.pending_events) == 1
queued_event = unauthenticated_sync.pending_events[0]
- assert queued_event['event_type'] == 'CreateAgentTaskEvent'
- assert queued_event['user_id'] == TEMP_USER_ID
- assert queued_event['task'] == 'Queued task'
+ assert queued_event.event_type == 'CreateAgentTaskEvent'
+ assert queued_event.user_id == TEMP_USER_ID
+ assert queued_event.task == 'Queued task'
async def test_event_user_id_injection_pre_auth(self, httpserver: HTTPServer, unauthenticated_sync):
"""Test that temp user ID is injected for pre-auth events."""
@@ -176,16 +172,8 @@ class TestCloudSyncRetryLogic:
# Manually add pending events (simulating 401 scenario)
sync_with_auth.pending_events.extend(
[
- {
- 'event_type': 'CreateAgentTaskEvent',
- 'task': 'Pending task 1',
- 'user_id': TEMP_USER_ID,
- },
- {
- 'event_type': 'CreateAgentTaskEvent',
- 'task': 'Pending task 2',
- 'user_id': TEMP_USER_ID,
- },
+ BaseEvent(event_type='CreateAgentTaskEvent', task='Pending task 1', user_id=TEMP_USER_ID),
+ BaseEvent(event_type='CreateAgentTaskEvent', task='Pending task 2', user_id=TEMP_USER_ID),
]
)
@@ -272,7 +260,7 @@ class TestCloudSyncBackendCommunication:
assert len(data['events']) == 1
event = data['events'][0]
- required_fields = ['event_type', 'event_id', 'event_at', 'event_schema', 'data']
+ required_fields = ['event_type', 'event_id', 'event_created_at', 'event_schema', 'user_id']
for field in required_fields:
assert field in event, f'Missing required field: {field}'
diff --git a/tests/ci/test_sync_client_auth.py b/tests/ci/test_sync_client_auth.py
index 72d4dfaf8..c9851254d 100644
--- a/tests/ci/test_sync_client_auth.py
+++ b/tests/ci/test_sync_client_auth.py
@@ -59,23 +59,23 @@ async def http_client(httpserver: HTTPServer):
class TestDeviceAuthClient:
"""Test DeviceAuthClient class."""
- async def test_init_creates_config_dir(self, temp_config_dir, test_base_url):
+ async def test_init_creates_config_dir(self, temp_config_dir, httpserver):
"""Test that initialization creates config directory."""
- auth = DeviceAuthClient(base_url=test_base_url)
+ auth = DeviceAuthClient(base_url=httpserver.url_for(''))
assert temp_config_dir.exists()
assert (temp_config_dir / 'cloud_auth.json').exists() is False
- async def test_load_credentials_no_file(self, temp_config_dir, test_base_url):
+ async def test_load_credentials_no_file(self, temp_config_dir, httpserver):
"""Test loading credentials when file doesn't exist."""
- auth = DeviceAuthClient(base_url=test_base_url)
+ auth = DeviceAuthClient(base_url=httpserver.url_for(''))
# When no file exists, auth_config should have no token/user_id
assert auth.auth_config.api_token is None
assert auth.auth_config.user_id is None
assert not auth.is_authenticated
- async def test_save_and_load_credentials(self, temp_config_dir, test_base_url):
+ async def test_save_and_load_credentials(self, temp_config_dir, httpserver):
"""Test saving and loading credentials."""
- auth = DeviceAuthClient(base_url=test_base_url)
+ auth = DeviceAuthClient(base_url=httpserver.url_for(''))
# Update auth config and save
auth.auth_config.api_token = 'test-key-123'
@@ -84,7 +84,7 @@ class TestDeviceAuthClient:
auth.auth_config.save_to_file()
# Load in a new instance
- auth2 = DeviceAuthClient(base_url=test_base_url)
+ auth2 = DeviceAuthClient(base_url=httpserver.url_for(''))
assert auth2.auth_config.api_token == 'test-key-123'
assert auth2.auth_config.user_id == 'test-user-123'
assert auth2.is_authenticated
@@ -94,9 +94,9 @@ class TestDeviceAuthClient:
stat = (temp_config_dir / 'cloud_auth.json').stat()
assert oct(stat.st_mode)[-3:] == '600'
- async def test_is_authenticated(self, temp_config_dir, test_base_url):
+ async def test_is_authenticated(self, temp_config_dir, httpserver):
"""Test authentication status check."""
- auth = DeviceAuthClient(base_url=test_base_url)
+ auth = DeviceAuthClient(base_url=httpserver.url_for(''))
# Not authenticated initially
assert auth.is_authenticated is False
@@ -107,12 +107,12 @@ class TestDeviceAuthClient:
auth.auth_config.save_to_file()
# Reload to verify persistence
- auth2 = DeviceAuthClient(base_url=test_base_url)
+ auth2 = DeviceAuthClient(base_url=httpserver.url_for(''))
assert auth2.is_authenticated is True
- async def test_get_credentials(self, temp_config_dir, test_base_url):
+ async def test_get_credentials(self, temp_config_dir, httpserver):
"""Test getting credentials."""
- auth = DeviceAuthClient(base_url=test_base_url)
+ auth = DeviceAuthClient(base_url=httpserver.url_for(''))
# No credentials initially
assert auth.api_token is None
@@ -299,9 +299,9 @@ class TestDeviceAuthClient:
assert result is None # Should timeout and return None
assert not auth.is_authenticated
- async def test_logout(self, temp_config_dir, test_base_url):
+ async def test_logout(self, temp_config_dir, httpserver):
"""Test logout functionality."""
- auth = DeviceAuthClient(base_url=test_base_url)
+ auth = DeviceAuthClient(base_url=httpserver.url_for(''))
# Save credentials directly using auth_config
auth.auth_config.api_token = 'test-key'
@@ -319,7 +319,7 @@ class TestDeviceAuthClient:
assert (temp_config_dir / 'cloud_auth.json').exists()
# Verify the file contains empty credentials
- auth2 = DeviceAuthClient(base_url=test_base_url)
+ auth2 = DeviceAuthClient(base_url=httpserver.url_for(''))
assert auth2.auth_config.api_token is None
assert auth2.auth_config.user_id is None
@@ -327,14 +327,14 @@ class TestDeviceAuthClient:
class TestCloudSync:
"""Test CloudSync class."""
- async def test_init(self, temp_config_dir, test_base_url):
+ async def test_init(self, temp_config_dir, httpserver):
"""Test CloudSync initialization."""
service = CloudSync(
- base_url=test_base_url,
+ base_url=httpserver.url_for(''),
enable_auth=True,
)
- assert service.base_url == test_base_url
+ assert service.base_url == httpserver.url_for('')
assert service.enable_auth is True
assert service.auth_client is not None
assert isinstance(service.auth_client, DeviceAuthClient)
@@ -476,8 +476,8 @@ class TestCloudSync:
# Event should be in pending_events since we got 401
assert len(service.pending_events) == 1
- assert service.pending_events[0]['task'] == 'Pre-auth task'
- assert service.pending_events[0]['user_id'] == TEMP_USER_ID
+ assert service.pending_events[0].task == 'Pre-auth task'
+ assert service.pending_events[0].user_id == TEMP_USER_ID
# Now authenticate the auth client
auth.auth_config.api_token = 'test-api-key'