Skip to content

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

formatting

Nick Sweeting7a0889cf20df
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.py
index b23194a54..d820d671e 100644
--- a/browser_use/agent/service.py
+++ b/browser_use/agent/service.py
@@ -709,7 +709,7 @@ class Agent(Generic[Context, AgentStructuredOutput]):
include_screenshot=True, # always capture even if use_vision=False so that cloud sync is useful (it's fast now anyway)
cached=use_cache,
include_recent_events=self.include_recent_events,
- )
+ )
if browser_state_summary.screenshot:
self.logger.debug(f'πŸ“Έ Got browser state WITH screenshot, length: {len(browser_state_summary.screenshot)}')
else:
diff --git a/browser_use/browser/crash_watchdog.py b/browser_use/browser/crash_watchdog.py
index 12581c768..b63510aed 100644
--- a/browser_use/browser/crash_watchdog.py
+++ b/browser_use/browser/crash_watchdog.py
@@ -171,7 +171,7 @@ class CrashWatchdog(BaseWatchdog):
if session := self.browser_session._cdp_session_pool.pop(target_id, None):
await session.disconnect()
self.logger.info(f'[CrashWatchdog] Removed crashed session from pool: {target_id}')
-
+
# Get target info
cdp_client = self.browser_session.cdp_client
targets = await cdp_client.send.Target.getTargets()
@@ -239,7 +239,7 @@ class CrashWatchdog(BaseWatchdog):
async def _monitoring_loop(self) -> None:
"""Main monitoring loop."""
- await asyncio.sleep(10) # give browser time to start up and load the first page after first LLM call
+ await asyncio.sleep(10) # give browser time to start up and load the first page after first LLM call
while True:
try:
await self._check_network_timeouts()
@@ -296,14 +296,34 @@ class CrashWatchdog(BaseWatchdog):
"""Check if browser and targets are still responsive."""
try:
- cdp_session = await self.browser_session.get_or_create_cdp_session()
+ try:
+ self.logger.debug(f'[CrashWatchdog] Checking browser health for target {self.browser_session.agent_focus}')
+ cdp_session = await self.browser_session.get_or_create_cdp_session()
+ except Exception as e:
+ self.logger.debug(f'[CrashWatchdog] Checking browser health for target {self.browser_session.agent_focus} error: {type(e).__name__}: {e}')
+ self.agent_focus = cdp_session = await self.browser_session.get_or_create_cdp_session(target_id=self.agent_focus.target_id, new_socket=True, focus=True)
+
+ for target in (await self.browser_session.cdp_client.send.Target.getTargets()).get('targetInfos', []):
+ if target.get('type') == 'page':
+ cdp_session = await self.browser_session.get_or_create_cdp_session(target_id=target.get('targetId'))
+ if self._is_new_tab_page(target.get('url')) and target.get('url') != 'about:blank':
+ self.logger.debug(f'[CrashWatchdog] Redirecting chrome://new-tab-page/ to about:blank {target.get("url")}')
+ await cdp_session.cdp_client.send.Page.navigate(params={'url': 'about:blank'}, session_id=cdp_session.session_id)
+
# Quick ping to check if session is alive
+ self.logger.debug(f'[CrashWatchdog] Attempting to run simple JS test expression in session {cdp_session} 1+1')
await asyncio.wait_for(
- cdp_session.cdp_client.send.Runtime.evaluate(params={'expression': '1'}, session_id=cdp_session.session_id),
+ cdp_session.cdp_client.send.Runtime.evaluate(params={'expression': '1+1'}, session_id=cdp_session.session_id),
timeout=1.0,
)
+ self.logger.debug(f'[CrashWatchdog] Browser health check passed for target {self.browser_session.agent_focus}')
except Exception as e:
- self.logger.error(f'[CrashWatchdog] ❌ Crashed session detected for target {self.browser_session.agent_focus}')
+ self.logger.error(f'[CrashWatchdog] ❌ Crashed session detected for target {self.browser_session.agent_focus} error: {type(e).__name__}: {e}')
+ # Remove crashed session from pool
+ if self.browser_session.agent_focus and (target_id := self.browser_session.agent_focus.target_id):
+ if session := self.browser_session._cdp_session_pool.pop(target_id, None):
+ await session.disconnect()
+ self.logger.info(f'[CrashWatchdog] Removed crashed session from pool: {target_id}')
self.browser_session.agent_focus.target_id = None # type: ignore
# Check browser process if we have PID
@@ -316,7 +336,7 @@ class CrashWatchdog(BaseWatchdog):
await session.disconnect()
self.browser_session._cdp_session_pool.clear()
self.logger.info('[CrashWatchdog] Cleared all sessions from pool due to browser crash')
-
+
self.event_bus.dispatch(
BrowserErrorEvent(
error_type='BrowserProcessCrashed',
diff --git a/browser_use/browser/default_action_watchdog.py b/browser_use/browser/default_action_watchdog.py
index dd42e8631..7d4de8daf 100644
--- a/browser_use/browser/default_action_watchdog.py
+++ b/browser_use/browser/default_action_watchdog.py
@@ -85,7 +85,7 @@ class DefaultActionWatchdog(BaseWatchdog):
new_tab_msg = 'New tab opened - switching to it'
msg += f' - {new_tab_msg}'
self.logger.info(f'πŸ”— {new_tab_msg}')
-
+
# Optional:Switch to the newly created tab
# Not recommended usually in order to match normal behavior with Cmd/Ctrl+Click
# If agent wanted to focus on the new page they would've clicked without new_tab
@@ -397,10 +397,10 @@ class DefaultActionWatchdog(BaseWatchdog):
},
session_id=session_id,
),
- timeout=1.0 # 1 second timeout for mousePressed
+ timeout=1.0, # 1 second timeout for mousePressed
)
await asyncio.sleep(0.145)
- except asyncio.TimeoutError:
+ except TimeoutError:
self.logger.debug('⏱️ Mouse down timed out (likely due to dialog), continuing...')
# Don't sleep if we timed out
@@ -418,9 +418,9 @@ class DefaultActionWatchdog(BaseWatchdog):
},
session_id=session_id,
),
- timeout=1.0 # 1 second timeout for mouseReleased
+ timeout=1.0, # 1 second timeout for mouseReleased
)
- except asyncio.TimeoutError:
+ except TimeoutError:
self.logger.debug('⏱️ Mouse up timed out (likely due to dialog), continuing...')
self.logger.debug('πŸ–±οΈ Clicked successfully using x,y coordinates')
@@ -1123,8 +1123,8 @@ class DefaultActionWatchdog(BaseWatchdog):
if not found:
# Fallback: Try JavaScript search
js_result = await cdp_client.send.Runtime.evaluate(
- params={
- 'expression': f'''
+ params={
+ 'expression': f'''
(() => {{
const walker = document.createTreeWalker(
document.body,
@@ -1142,9 +1142,9 @@ class DefaultActionWatchdog(BaseWatchdog):
return false;
}})()
'''
- },
- session_id=session_id,
- )
+ },
+ session_id=session_id,
+ )
if js_result.get('result', {}).get('value'):
self.logger.info(f'πŸ“œ Scrolled to text: "{event.text}" (via JS)')
@@ -1152,7 +1152,7 @@ class DefaultActionWatchdog(BaseWatchdog):
else:
self.logger.warning(f'⚠️ Text not found: "{event.text}"')
raise BrowserError(f'Text not found: "{event.text}"', details={'text': event.text})
-
+
# If we got here and found is True, return None (success)
if found:
return None
diff --git a/browser_use/browser/dom_watchdog.py b/browser_use/browser/dom_watchdog.py
index 684c68977..605cb7fe8 100644
--- a/browser_use/browser/dom_watchdog.py
+++ b/browser_use/browser/dom_watchdog.py
@@ -166,7 +166,9 @@ class DOMWatchdog(BaseWatchdog):
await self._wait_for_stable_network()
self.logger.debug('πŸ” DOMWatchdog.on_BrowserStateRequestEvent: βœ… Page stability complete')
except Exception as e:
- self.logger.warning(f'πŸ” DOMWatchdog.on_BrowserStateRequestEvent: Network waiting failed: {e}, continuing anyway...')
+ self.logger.warning(
+ f'πŸ” DOMWatchdog.on_BrowserStateRequestEvent: Network waiting failed: {e}, continuing anyway...'
+ )
# Get tabs info once at the beginning for all paths
self.logger.debug('πŸ” DOMWatchdog.on_BrowserStateRequestEvent: Getting tabs info...')
@@ -250,7 +252,9 @@ class DOMWatchdog(BaseWatchdog):
# Get screenshot if requested
screenshot_b64 = None
if event.include_screenshot:
- self.logger.debug(f'πŸ” DOMWatchdog.on_BrowserStateRequestEvent: πŸ“Έ DOM watchdog requesting screenshot, include_screenshot={event.include_screenshot}')
+ self.logger.debug(
+ f'πŸ” DOMWatchdog.on_BrowserStateRequestEvent: πŸ“Έ DOM watchdog requesting screenshot, include_screenshot={event.include_screenshot}'
+ )
try:
# Check if handler is registered
handlers = self.event_bus.handlers.get('ScreenshotEvent', [])
@@ -265,6 +269,7 @@ class DOMWatchdog(BaseWatchdog):
if screenshot_bytes:
# Convert bytes back to base64 string for BrowserStateSummary
import base64
+
screenshot_b64 = base64.b64encode(screenshot_bytes).decode('utf-8')
self.logger.debug(f'πŸ“Έ Got screenshot: {len(screenshot_bytes)} bytes')
else:
@@ -294,7 +299,9 @@ class DOMWatchdog(BaseWatchdog):
page_info = await self._get_page_info()
self.logger.debug(f'πŸ” DOMWatchdog.on_BrowserStateRequestEvent: Got page info from CDP: {page_info}')
except Exception as e:
- self.logger.debug(f'πŸ” DOMWatchdog.on_BrowserStateRequestEvent: Failed to get page info from CDP: {e}, using fallback')
+ self.logger.debug(
+ f'πŸ” DOMWatchdog.on_BrowserStateRequestEvent: Failed to get page info from CDP: {e}, using fallback'
+ )
# Fallback to default viewport dimensions
viewport = self.browser_session.browser_profile.viewport or {'width': 1280, 'height': 720}
page_info = PageInfo(
@@ -315,9 +322,13 @@ class DOMWatchdog(BaseWatchdog):
# Build and cache the browser state summary
if screenshot_b64:
- self.logger.debug(f'πŸ” DOMWatchdog.on_BrowserStateRequestEvent: πŸ“Έ Creating BrowserStateSummary with screenshot, length: {len(screenshot_b64)}')
+ self.logger.debug(
+ f'πŸ” DOMWatchdog.on_BrowserStateRequestEvent: πŸ“Έ Creating BrowserStateSummary with screenshot, length: {len(screenshot_b64)}'
+ )
else:
- self.logger.debug('πŸ” DOMWatchdog.on_BrowserStateRequestEvent: πŸ“Έ Creating BrowserStateSummary WITHOUT screenshot')
+ self.logger.debug(
+ 'πŸ” DOMWatchdog.on_BrowserStateRequestEvent: πŸ“Έ Creating BrowserStateSummary WITHOUT screenshot'
+ )
browser_state = BrowserStateSummary(
dom_state=content,
@@ -422,7 +433,9 @@ class DOMWatchdog(BaseWatchdog):
from browser_use.dom.debug.highlights import inject_highlighting_script
await inject_highlighting_script(self._dom_service, self.selector_map)
- self.logger.debug(f'πŸ” DOMWatchdog._build_dom_tree: βœ… Injected highlighting for {len(self.selector_map)} elements')
+ self.logger.debug(
+ f'πŸ” DOMWatchdog._build_dom_tree: βœ… Injected highlighting for {len(self.selector_map)} elements'
+ )
except Exception as e:
self.logger.debug(f'πŸ” DOMWatchdog._build_dom_tree: Failed to inject highlighting: {e}')
diff --git a/browser_use/browser/events.py b/browser_use/browser/events.py
index 8b706bb0d..c676b9345 100644
--- a/browser_use/browser/events.py
+++ b/browser_use/browser/events.py
@@ -5,11 +5,10 @@ from typing import Any, Literal
from bubus import BaseEvent
from bubus.models import T_EventResultType
-from pydantic import Field, field_validator
+from pydantic import BaseModel, Field, field_validator
from browser_use.browser.views import BrowserStateSummary
from browser_use.dom.views import EnhancedDOMTreeNode
-from pydantic import BaseModel
# ============================================================================
# Agent/Controller -> BrowserSession Events (High-level browser actions)
@@ -50,6 +49,7 @@ class ElementSelectedEvent(BaseEvent[T_EventResultType]):
snapshot_node=None,
)
+
# TODO: add page handle to events
# class PageHandle(share a base with browser.session.CDPSession?):
# url: str
@@ -81,11 +81,13 @@ class NavigateToUrlEvent(BaseEvent[None]):
url: str
wait_until: Literal['load', 'domcontentloaded', 'networkidle', 'commit'] = 'load'
timeout_ms: int | None = None
- new_tab: bool = Field(default=False, description='Set True to leave the current tab alone and open a new tab in the foreground for the new URL')
+ new_tab: bool = Field(
+ default=False, description='Set True to leave the current tab alone and open a new tab in the foreground for the new URL'
+ )
# existing_tab: PageHandle | None = None # TODO
# limit enforced by bubus, not exposed to LLM:
- event_timeout: float | None = 15.0 # seconds
+ event_timeout: float | None = 15.0 # seconds
class ClickElementEvent(ElementSelectedEvent[None]):
@@ -93,7 +95,10 @@ class ClickElementEvent(ElementSelectedEvent[None]):
node: 'EnhancedDOMTreeNode'
button: Literal['left', 'right', 'middle'] = 'left'
- new_tab: bool = Field(default=False, description='Set True to open any link clicked in a new tab in the background, can use switch_tab(page_id=-1) after to focus it')
+ new_tab: bool = Field(
+ default=False,
+ description='Set True to open any link clicked in a new tab in the background, can use switch_tab(page_id=-1) after to focus it',
+ )
# click_count: int = 1 # TODO
# expect_download: bool = False # moved to downloads_watchdog.py
@@ -133,20 +138,18 @@ class CloseTabEvent(BaseEvent[None]):
tab_index: int
-
event_timeout: float | None = 10.0 # seconds
+
class ScreenshotEvent(BaseEvent[bytes]):
"""Request to take a screenshot. Returns screenshot as bytes."""
full_page: bool = False
clip: dict[str, float] | None = None # {x, y, width, height}
-
event_timeout: float | None = 15.0 # seconds
-
class BrowserStateRequestEvent(BaseEvent[BrowserStateSummary]):
"""Request current browser state."""
@@ -242,6 +245,7 @@ class BrowserStopEvent(BaseEvent):
class BrowserLaunchResult(BaseModel):
"""Result of launching a browser."""
+
# TODO: add browser executable_path, pid, version, latency, user_data_dir, X11 $DISPLAY, host IP address, etc.
cdp_url: str
@@ -285,7 +289,9 @@ class SetCookiesEvent(BaseEvent):
cookies: list[dict[str, Any]]
- event_timeout: float | None = 30.0 # only long to support the edge case of restoring a big localStorage / on many origins (has to O(n) visit each origin to restore)
+ event_timeout: float | None = (
+ 30.0 # only long to support the edge case of restoring a big localStorage / on many origins (has to O(n) visit each origin to restore)
+ )
class GetCookiesEvent(BaseEvent):
@@ -334,7 +340,7 @@ class TabClosedEvent(BaseEvent):
# TODO:
# new_focus_tab_index: int | None = None
- # new_focus_target_id: str | None = None
+ # new_focus_target_id: str | None = None
# new_focus_url: str | None = None
event_timeout: float | None = 10.0 # seconds
@@ -476,14 +482,13 @@ class AboutBlankDVDScreensaverShownEvent(BaseEvent):
class DialogOpenedEvent(BaseEvent):
"""Event dispatched when a JavaScript dialog is opened and handled."""
-
+
dialog_type: str # 'alert', 'confirm', 'prompt', or 'beforeunload'
message: str
url: str
frame_id: str
-
# Note: Model rebuilding for forward references is handled in the importing modules
# Events with 'EnhancedDOMTreeNode' forward references (ClickElementEvent, TypeTextEvent,
# ScrollEvent, UploadFileEvent) need model_rebuild() called after imports are complete
diff --git a/browser_use/browser/permissions_watchdog.py b/browser_use/browser/permissions_watchdog.py
index 9e2ff7982..9f4694d47 100644
--- a/browser_use/browser/permissions_watchdog.py
+++ b/browser_use/browser/permissions_watchdog.py
@@ -3,8 +3,6 @@
from typing import TYPE_CHECKING, ClassVar
from bubus import BaseEvent
-from cdp_use.cdp import browser as cdp_browser
-from cdp_use.cdp.browser import GrantPermissionsParameters
from browser_use.browser.events import BrowserConnectedEvent
from browser_use.browser.watchdog_base import BaseWatchdog
@@ -25,13 +23,13 @@ class PermissionsWatchdog(BaseWatchdog):
async def on_BrowserConnectedEvent(self, event: BrowserConnectedEvent) -> None:
"""Grant permissions when browser connects."""
permissions = self.browser_session.browser_profile.permissions
-
+
if not permissions:
self.logger.debug('No permissions to grant')
return
-
+
self.logger.info(f'πŸ”“ Granting browser permissions: {permissions}')
-
+
try:
# Grant permissions using CDP Browser.grantPermissions
# origin=None means grant to all origins
@@ -42,4 +40,4 @@ class PermissionsWatchdog(BaseWatchdog):
self.logger.debug(f'βœ… Successfully granted permissions: {permissions}')
except Exception as e:
self.logger.error(f'❌ Failed to grant permissions: {str(e)}')
- # Don't raise - permissions are not critical to browser operation
\ No newline at end of file
+ # Don't raise - permissions are not critical to browser operation
diff --git a/browser_use/browser/popups_watchdog.py b/browser_use/browser/popups_watchdog.py
index ebdef9e00..366b9366a 100644
--- a/browser_use/browser/popups_watchdog.py
+++ b/browser_use/browser/popups_watchdog.py
@@ -6,7 +6,7 @@ from typing import ClassVar
from bubus import BaseEvent
from pydantic import PrivateAttr
-from browser_use.browser.events import AgentFocusChangedEvent, DialogOpenedEvent, TabCreatedEvent
+from browser_use.browser.events import DialogOpenedEvent, TabCreatedEvent
from browser_use.browser.watchdog_base import BaseWatchdog
@@ -19,72 +19,79 @@ class PopupsWatchdog(BaseWatchdog):
# Track which targets have dialog handlers registered
_dialog_listeners_registered: set[str] = PrivateAttr(default_factory=set)
-
+
def __init__(self, **kwargs):
super().__init__(**kwargs)
- self.logger.info(f"πŸš€ PopupsWatchdog initialized with browser_session={self.browser_session}, ID={id(self)}")
+ self.logger.info(f'πŸš€ PopupsWatchdog initialized with browser_session={self.browser_session}, ID={id(self)}')
async def on_TabCreatedEvent(self, event: TabCreatedEvent) -> None:
"""Set up JavaScript dialog handling when a new tab is created."""
target_id = event.target_id
- self.logger.info(f"🎯 PopupsWatchdog received TabCreatedEvent for target {target_id}")
-
+ self.logger.info(f'🎯 PopupsWatchdog received TabCreatedEvent for target {target_id}')
+
# Skip if we've already registered for this target
if target_id in self._dialog_listeners_registered:
- self.logger.debug(f"Already registered dialog handlers for target {target_id}")
+ self.logger.debug(f'Already registered dialog handlers for target {target_id}')
return
-
- self.logger.info(f"πŸ“Œ Starting dialog handler setup for target {target_id}")
+
+ self.logger.info(f'πŸ“Œ Starting dialog handler setup for target {target_id}')
try:
-
# Set up handler for JavaScript dialogs
# CDP handlers are sync but we need to handle dialogs immediately to unblock the browser
- def handle_dialog(event_data, _session_id: str | None=None):
- self.browser_session.event_bus.dispatch(DialogOpenedEvent(
- frame_id=event_data.get('frameId'),
- dialog_type=event_data.get('type'),
- message=event_data.get('message'),
- url=event_data.get('url'),
- ))
-
+ def handle_dialog(event_data, _session_id: str | None = None):
+ self.browser_session.event_bus.dispatch(
+ DialogOpenedEvent(
+ frame_id=event_data.get('frameId'),
+ dialog_type=event_data.get('type'),
+ message=event_data.get('message'),
+ url=event_data.get('url'),
+ )
+ )
+
cdp_session = await self.browser_session.get_or_create_cdp_session(target_id, focus=False)
cdp_session.cdp_client.register.Page.javascriptDialogOpening(handle_dialog)
- self.logger.info(f"βœ… Successfully registered Page.javascriptDialogOpening handler for session {cdp_session.session_id}")
-
+ self.logger.info(
+ f'βœ… Successfully registered Page.javascriptDialogOpening handler for session {cdp_session.session_id}'
+ )
+
# Mark this target as having dialog handling set up
self._dialog_listeners_registered.add(target_id)
-
- self.logger.info(f"βœ… Set up JavaScript dialog handling for tab {target_id}")
-
+
+ self.logger.info(f'βœ… Set up JavaScript dialog handling for tab {target_id}')
+
except Exception as e:
- self.logger.warning(f"Failed to set up dialog handling for tab {target_id}: {e}")
+ self.logger.warning(f'Failed to set up dialog handling for tab {target_id}: {e}')
async def on_DialogOpenedEvent(self, event: DialogOpenedEvent) -> None:
"""Handle the async closing of JavaScript dialogs."""
- self.logger.info(f"πŸ“‹ on_DialogOpenedEvent called with frame_id={event.frame_id} url={event.url} message={event.message}")
+ self.logger.info(f'πŸ“‹ on_DialogOpenedEvent called with frame_id={event.frame_id} url={event.url} message={event.message}')
- assert self.browser_session.agent_focus is not None, "Agent focus not set when handling DialogOpenedEvent"
+ assert self.browser_session.agent_focus is not None, 'Agent focus not set when handling DialogOpenedEvent'
cdp_session = await self.browser_session.cdp_client_for_frame(event.frame_id)
try:
# delay to look more human
await asyncio.sleep(0.25)
assert self.browser_session._cdp_client_root
- await asyncio.wait_for(self.browser_session._cdp_client_root.send.Page.handleJavaScriptDialog(
- params={'accept': True},
- session_id=cdp_session.session_id,
- ), timeout=5.0)
+ await asyncio.wait_for(
+ self.browser_session._cdp_client_root.send.Page.handleJavaScriptDialog(
+ params={'accept': True},
+ session_id=cdp_session.session_id,
+ ),
+ timeout=5.0,
+ )
# CRITICAL: you must either wait for Page.javascriptDialogClosed or wait for some fixed time here
await asyncio.sleep(0.2)
- assert (await cdp_session.cdp_client.send.Runtime.evaluate(
- params={'expression': '1+1'},
- session_id=cdp_session.session_id
- ))['result'].get('value') == 2, "Browser crashed after handling JS dialog popup"
- self.logger.info("βœ… JS dialog popup handled successfully")
+ assert (
+ await cdp_session.cdp_client.send.Runtime.evaluate(
+ params={'expression': '1+1'}, session_id=cdp_session.session_id
+ )
+ )['result'].get('value') == 2, 'Browser crashed after handling JS dialog popup'
+ self.logger.info('βœ… JS dialog popup handled successfully')
# self.browser_session.agent_focus = await self.browser_session.get_or_create_cdp_session(current_focus.target_id, focus=True, new_socket=True)
# assert await self.browser_session.agent_focus.cdp_client.send.Page.getFrameTree(session_id=self.browser_session.agent_focus.session_id) is not None, "Agent focus not set after handling dialog"
except Exception as e:
- self.logger.error(f"Failed to handle JavaScript dialog: {e}")
+ self.logger.error(f'Failed to handle JavaScript dialog: {e}')
# finally:
# self.event_bus.dispatch(AgentFocusChangedEvent(
# tab_index=await self.browser_session.get_tab_index(current_focus.target_id),
diff --git a/browser_use/browser/profile.py b/browser_use/browser/profile.py
index 21c9e0163..4241fb213 100644
--- a/browser_use/browser/profile.py
+++ b/browser_use/browser/profile.py
@@ -603,12 +603,9 @@ class BrowserProfile(BrowserConnectArgs, BrowserLaunchPersistentContextArgs, Bro
include_dynamic_attributes: bool = Field(default=True, description='Include dynamic attributes in selectors.')
highlight_elements: bool = Field(default=True, description='Highlight interactive elements on the page.')
viewport_expansion: int = Field(default=500, description='Viewport expansion in pixels for LLM context.')
-
+
# --- Downloads ---
- auto_download_pdfs: bool = Field(
- default=True,
- description='Automatically download PDFs when navigating to PDF viewer pages.'
- )
+ auto_download_pdfs: bool = Field(default=True, description='Automatically download PDFs when navigating to PDF viewer pages.')
profile_directory: str = 'Default' # e.g. 'Profile 1', 'Profile 2', 'Custom Profile', etc.
diff --git a/browser_use/browser/screenshot_watchdog.py b/browser_use/browser/screenshot_watchdog.py
index e498d1258..dd7e3e0cc 100644
--- a/browser_use/browser/screenshot_watchdog.py
+++ b/browser_use/browser/screenshot_watchdog.py
@@ -37,7 +37,7 @@ class ScreenshotWatchdog(BaseWatchdog):
try:
# Get CDP client and session for current target
cdp_session = await self.browser_session.get_or_create_cdp_session()
-
+
# Activate the target to ensure it's focused
self.logger.debug(f'[ScreenshotWatchdog] Activating target: {cdp_session.target_id}')
try:
@@ -45,9 +45,10 @@ class ScreenshotWatchdog(BaseWatchdog):
await self.browser_session.cdp_client.send.Target.activateTarget(params={'targetId': cdp_session.target_id})
except Exception as e:
self.logger.debug(f'[ScreenshotWatchdog] Could not activate target: {e}')
-
+
# Small delay to ensure the tab switch is complete
import asyncio
+
await asyncio.sleep(0.2)
# Prepare screenshot parameters
@@ -60,16 +61,17 @@ class ScreenshotWatchdog(BaseWatchdog):
# Return base64-encoded screenshot data as bytes
if result and 'data' in result:
self.logger.debug('[ScreenshotWatchdog] Screenshot captured successfully')
-
+
# Remove highlights after screenshot to clean up the page
try:
await self.browser_session.remove_highlights()
self.logger.debug('[ScreenshotWatchdog] Removed element highlights after screenshot')
except Exception as e:
self.logger.debug(f'[ScreenshotWatchdog] Failed to remove highlights: {e}')
-
+
# Convert base64 string to bytes
import base64
+
return base64.b64decode(result['data'])
else:
self.logger.warning('[ScreenshotWatchdog] Screenshot result missing data')
diff --git a/browser_use/browser/security_watchdog.py b/browser_use/browser/security_watchdog.py
index 4f51b4f3e..c4140602c 100644
--- a/browser_use/browser/security_watchdog.py
+++ b/browser_use/browser/security_watchdog.py
@@ -1,10 +1,8 @@
"""Security watchdog for enforcing URL access policies."""
-import asyncio
-from typing import TYPE_CHECKING, Any, ClassVar
+from typing import TYPE_CHECKING, ClassVar
from bubus import BaseEvent
-from pydantic import PrivateAttr
from browser_use.browser.events import (
BrowserErrorEvent,
@@ -54,7 +52,7 @@ class SecurityWatchdog(BaseWatchdog):
# Check if the navigated URL is allowed (in case of redirects)
if not self._is_url_allowed(event.url):
self.logger.warning(f'⛔️ Navigation to non-allowed URL detected: {event.url}')
-
+
# Dispatch browser error
self.event_bus.dispatch(
BrowserErrorEvent(
@@ -63,7 +61,7 @@ class SecurityWatchdog(BaseWatchdog):
details={'url': event.url, 'tab_index': event.tab_index},
)
)
-
+
# Close the target that navigated to the disallowed URL
try:
targets = await self.browser_session._cdp_get_all_pages()
@@ -78,7 +76,7 @@ class SecurityWatchdog(BaseWatchdog):
"""Check if new tab URL is allowed."""
if not self._is_url_allowed(event.url):
self.logger.warning(f'⛔️ New tab created with disallowed URL: {event.url}')
-
+
# Dispatch error and try to close the tab
self.event_bus.dispatch(
BrowserErrorEvent(
@@ -87,10 +85,10 @@ class SecurityWatchdog(BaseWatchdog):
details={'url': event.url, 'tab_index': event.tab_index, 'target_id': event.target_id},
)
)
-
+
# Try to close the offending tab
try:
- await self.browser_session._cdp_close_page(event.target_id)
+ await self.browser_session._cdp_close_page(event.target_id)
self.logger.info(f'⛔️ Closed new tab with non-allowed URL: {event.url}')
except Exception as e:
self.logger.error(f'⛔️ Failed to close new tab with non-allowed URL: {str(e)}')
diff --git a/browser_use/browser/session.py b/browser_use/browser/session.py
index becf83289..b4556ccdd 100644
--- a/browser_use/browser/session.py
+++ b/browser_use/browser/session.py
@@ -61,7 +61,14 @@ class CDPSession(BaseModel):
owns_cdp_client: bool = False
@classmethod
- async def for_target(cls, cdp_client: CDPClient, target_id: str, new_socket: bool = False, cdp_url: str | None = None, domains: list[str] | None = None):
+ async def for_target(
+ cls,
+ cdp_client: CDPClient,
+ target_id: str,
+ new_socket: bool = False,
+ cdp_url: str | None = None,
+ domains: list[str] | None = None,
+ ):
"""Create a CDP session for a target.
Args:
@@ -114,7 +121,7 @@ class CDPSession(BaseModel):
# Use specified domains or default domains
domains = domains or ['Page', 'DOM', 'DOMSnapshot', 'Accessibility', 'Runtime', 'Inspector']
-
+
# Enable all domains in parallel
enable_tasks = []
for domain in domains:
@@ -122,9 +129,11 @@ class CDPSession(BaseModel):
domain_api = getattr(self.cdp_client.send, domain, None)
# Browser and Target domains don't use session_id, dont pass it for those
enable_kwargs = {} if domain in ['Browser', 'Target'] else {'session_id': self.session_id}
- assert domain_api and hasattr(domain_api, 'enable'), f'{domain_api} is not a recognized CDP domain with a .enable() method'
+ assert domain_api and hasattr(domain_api, 'enable'), (
+ f'{domain_api} is not a recognized CDP domain with a .enable() method'
+ )
enable_tasks.append(domain_api.enable(**enable_kwargs))
-
+
results = await asyncio.gather(*enable_tasks, return_exceptions=True)
if any(isinstance(result, Exception) for result in results):
raise RuntimeError(f'Failed to enable requested CDP domain: {results}')
@@ -290,9 +299,10 @@ class BrowserSession(BaseModel):
"""Kill the browser session and reset all state."""
# First save storage state while CDP is still connected
from browser_use.browser.events import SaveStorageStateEvent
+
save_event = self.event_bus.dispatch(SaveStorageStateEvent())
await save_event
-
+
# Dispatch stop event to kill the browser
await self.event_bus.dispatch(BrowserStopEvent(force=True))
# Stop the event bus
@@ -310,12 +320,13 @@ class BrowserSession(BaseModel):
"""
# First save storage state while CDP is still connected
from browser_use.browser.events import SaveStorageStateEvent
+
save_event = self.event_bus.dispatch(SaveStorageStateEvent())
await save_event
-
+
# Now dispatch BrowserStopEvent to notify watchdogs
await self.event_bus.dispatch(BrowserStopEvent(force=False))
-
+
# Stop the event bus
await self.event_bus.stop(clear=True, timeout=5)
# Reset all state
@@ -344,7 +355,9 @@ class BrowserSession(BaseModel):
await launch_event
# Get the CDP URL from LocalBrowserWatchdog handler result
- launch_result: BrowserLaunchResult = cast(BrowserLaunchResult, await launch_event.event_result(raise_if_none=True, raise_if_any=True))
+ launch_result: BrowserLaunchResult = cast(
+ BrowserLaunchResult, await launch_event.event_result(raise_if_none=True, raise_if_any=True)
+ )
self.cdp_url = launch_result.cdp_url
else:
raise ValueError('Got BrowserSession(is_local=False) but no cdp_url was provided to connect to!')
@@ -396,7 +409,9 @@ class BrowserSession(BaseModel):
self.logger.info(f'[on_NavigateToUrlEvent] Current target_id: {current_target_id}')
for idx, target in enumerate(targets):
- self.logger.debug(f'[on_NavigateToUrlEvent] Tab {idx}: url={target.get("url")}, targetId={target["targetId"]}')
+ self.logger.debug(
+ f'[on_NavigateToUrlEvent] Tab {idx}: url={target.get("url")}, targetId={target["targetId"]}'
+ )
if target.get('url') == 'about:blank' and target['targetId'] != current_target_id:
target_id = target['targetId']
tab_index = idx
@@ -413,7 +428,9 @@ class BrowserSession(BaseModel):
tab_index = len(targets) - 1
self.logger.info(f'Created new tab at index {tab_index}')
# Dispatch TabCreatedEvent for new tab
- await self.event_bus.dispatch(TabCreatedEvent(tab_index=tab_index, url='about:blank', target_id=target_id))
+ await self.event_bus.dispatch(
+ TabCreatedEvent(tab_index=tab_index, url='about:blank', target_id=target_id)
+ )
except Exception as e:
self.logger.error(f'[on_NavigateToUrlEvent] Failed to create new tab: {e}')
# Fall back to using current tab
@@ -441,7 +458,7 @@ class BrowserSession(BaseModel):
'transitionType': 'address_bar',
# 'referrer': 'https://www.google.com',
},
- session_id=self.agent_focus.session_id
+ session_id=self.agent_focus.session_id,
)
# Wait a bit to ensure page starts loading
@@ -534,19 +551,19 @@ class BrowserSession(BaseModel):
async def on_AgentFocusChangedEvent(self, event: AgentFocusChangedEvent) -> None:
"""Handle agent focus change - update focus and clear cache."""
self.logger.debug(f'πŸ”„ AgentFocusChangedEvent received: tab_index={event.tab_index}, url={event.url}')
-
+
# Clear cached DOM state since focus changed
self.logger.debug('πŸ”„ Clearing DOM cache...')
if self._dom_watchdog:
self._dom_watchdog.clear_cache()
self.logger.debug('πŸ”„ Cleared DOM cache after focus change')
-
+
# Clear cached browser state
self.logger.debug('πŸ”„ Clearing cached browser state...')
self._cached_browser_state_summary = None
self._cached_selector_map.clear()
self.logger.debug('πŸ”„ Cached browser state cleared')
-
+
# Update agent focus if tab_index is provided
if event.tab_index is not None:
self.logger.debug(f'πŸ”„ Getting all pages for tab_index={event.tab_index}...')
@@ -557,15 +574,17 @@ class BrowserSession(BaseModel):
self.logger.debug(f'πŸ”„ Getting CDP session for target {target_id}...')
self.agent_focus = await self.get_or_create_cdp_session(target_id, focus=True)
self.logger.debug(f'πŸ”„ Updated agent focus to tab {event.tab_index} (target {target_id})')
-
+
# Test that the browser is responsive by evaluating a simple expression
if self.agent_focus:
self.logger.debug('πŸ”„ Testing browser responsiveness...')
try:
- test_result = await asyncio.wait_for(self.agent_focus.cdp_client.send.Runtime.evaluate(
- params={'expression': '1 + 1', 'returnByValue': True},
- session_id=self.agent_focus.session_id
- ), timeout=2.0)
+ test_result = await asyncio.wait_for(
+ self.agent_focus.cdp_client.send.Runtime.evaluate(
+ params={'expression': '1 + 1', 'returnByValue': True}, session_id=self.agent_focus.session_id
+ ),
+ timeout=2.0,
+ )
if test_result.get('result', {}).get('value') == 2:
self.logger.debug('πŸ”„ βœ… Browser is responsive after focus change')
else:
@@ -573,7 +592,7 @@ class BrowserSession(BaseModel):
except Exception as e:
self.logger.error(f'πŸ”„ ❌ Page appears crashed after focus change: {e}')
raise
-
+
self.logger.debug('πŸ”„ AgentFocusChangedEvent handler completed successfully')
async def on_FileDownloadedEvent(self, event: FileDownloadedEvent) -> None:
@@ -630,7 +649,7 @@ class BrowserSession(BaseModel):
"""Get or create a CDP session for a target.
Args:
- target_id: Target ID to get session for. If None, uses current agent focus.
+ target_id: Target ID to get session for. If None, uses current agent focus.
If 'main', finds any about:blank target to use.
focus: If True, switches agent focus to this target. If False, just returns session without changing focus.
new_socket: If True, create a dedicated WebSocket connection. If None (default), creates new socket for new targets only.
@@ -649,7 +668,11 @@ class BrowserSession(BaseModel):
target_id = self.agent_focus.target_id
elif target_id == 'main' and not self.agent_focus.target_id:
all_targets = await self._cdp_client_root.send.Target.getTargets()
- spare_bg_target = [target for target in all_targets.get('targetInfos', []) if target.get('type') == 'page' and target.get('url') == 'about:blank']
+ spare_bg_target = [
+ target
+ for target in all_targets.get('targetInfos', [])
+ if target.get('type') == 'page' and target.get('url') == 'about:blank'
+ ]
if spare_bg_target:
target_id = spare_bg_target[0]['targetId']
@@ -657,7 +680,9 @@ class BrowserSession(BaseModel):
assert self._cdp_client_root is not None, 'Root CDP client not initialized - browser may not be connected yet'
result = await self._cdp_client_root.send.Target.createTarget(params={'url': 'about:blank'})
target_id = result['targetId']
- self.logger.debug(f"[get_or_create_cdp_session] Created new target {target_id} for 'main' request with domains={domains}")
+ self.logger.debug(
+ f"[get_or_create_cdp_session] Created new target {target_id} for 'main' request with domains={domains}"
+ )
assert target_id is not None, 'No target_id provided and no about:blank tab or open page found'
@@ -692,7 +717,6 @@ class BrowserSession(BaseModel):
return session
-
@property
def current_target_id(self) -> str | None:
return self.agent_focus.target_id if self.agent_focus else None
@@ -749,6 +773,7 @@ class BrowserSession(BaseModel):
return
from browser_use.browser.aboutblank_watchdog import AboutBlankWatchdog
+
# from browser_use.browser.crash_watchdog import CrashWatchdog
from browser_use.browser.default_action_watchdog import DefaultActionWatchdog
from browser_use.browser.dom_watchdog import DOMWatchdog
@@ -1279,14 +1304,18 @@ class BrowserSession(BaseModel):
)
]
- async def _cdp_create_new_page(self, url: str = 'about:blank', background: bool=False, new_window: bool=False) -> str:
+ async def _cdp_create_new_page(self, url: str = 'about:blank', background: bool = False, new_window: bool = False) -> str:
"""Create a new page/tab using CDP Target.createTarget. Returns target ID."""
# Use the root CDP client to create tabs at the browser level
if self._cdp_client_root:
- result = await self._cdp_client_root.send.Target.createTarget(params={'url': url, 'newWindow': new_window, 'background': background})
+ result = await self._cdp_client_root.send.Target.createTarget(
+ params={'url': url, 'newWindow': new_window, 'background': background}
+ )
else:
# Fallback to using cdp_client if root is not available
- result = await self.cdp_client.send.Target.createTarget(params={'url': url, 'newWindow': new_window, 'background': background})
+ result = await self.cdp_client.send.Target.createTarget(
+ params={'url': url, 'newWindow': new_window, 'background': background}
+ )
return result['targetId']
async def _cdp_close_page(self, target_id: str) -> None:
diff --git a/browser_use/browser/storage_state_watchdog.py b/browser_use/browser/storage_state_watchdog.py
index 902e93c19..2dfee3f84 100644
--- a/browser_use/browser/storage_state_watchdog.py
+++ b/browser_use/browser/storage_state_watchdog.py
@@ -53,7 +53,6 @@ class StorageStateWatchdog(BaseWatchdog):
# Automatically load storage state after browser start
self.event_bus.dispatch(LoadStorageStateEvent())
-
async def on_SaveStorageStateEvent(self, event: SaveStorageStateEvent) -> None:
"""Handle storage state save request."""
# Use provided path or fall back to profile default
diff --git a/browser_use/browser/views.py b/browser_use/browser/views.py
index 700369738..84045feac 100644
--- a/browser_use/browser/views.py
+++ b/browser_use/browser/views.py
@@ -1,10 +1,10 @@
from dataclasses import dataclass, field
from typing import Any
+from bubus import BaseEvent
from pydantic import BaseModel
from browser_use.dom.views import DOMInteractedElement, SerializedDOMState
-from bubus import BaseEvent
# Known placeholder image data for about:blank pages - a 4x4 white PNG
PLACEHOLDER_4PX_SCREENSHOT = (
diff --git a/browser_use/cli.py b/browser_use/cli.py
index deba5dbbf..1328f71aa 100644
--- a/browser_use/cli.py
+++ b/browser_use/cli.py
@@ -1448,7 +1448,7 @@ async def textual_interface(config: dict[str, Any]):
"""Run the Textual interface."""
# Prevent browser_use from setting up logging at import time
os.environ['BROWSER_USE_SETUP_LOGGING'] = 'false'
-
+
logger = logging.getLogger('browser_use.startup')
# Set up logging for Textual UI - prevent any logging to stdout
@@ -1488,10 +1488,11 @@ async def textual_interface(config: dict[str, Any]):
browser_profile=profile,
)
logger.debug('BrowserSession initialized successfully')
-
+
# Set up FIFO logging pipes for streaming logs to UI
try:
from browser_use.logging_config import setup_log_pipes
+
setup_log_pipes(session_id=browser_session.id)
logger.debug(f'FIFO logging pipes set up for session {browser_session.id[-4:]}')
except Exception as e:
diff --git a/browser_use/controller/service.py b/browser_use/controller/service.py
index 413c34605..56abec1ef 100644
--- a/browser_use/controller/service.py
+++ b/browser_use/controller/service.py
@@ -288,7 +288,9 @@ class Controller(Generic[Context]):
return ActionResult(error=error_msg)
@self.registry.action('Upload file to interactive element with file path', param_model=UploadFileAction)
- async def upload_file_to_element(params: UploadFileAction, browser_session: BrowserSession, available_file_paths: list[str], file_system: FileSystem):
+ async def upload_file_to_element(
+ params: UploadFileAction, browser_session: BrowserSession, available_file_paths: list[str], file_system: FileSystem
+ ):
# Check if file is in available_file_paths (user-provided or downloaded files)
if params.path not in available_file_paths:
# Also check if it's a recently downloaded file that might not be in available_file_paths yet
@@ -304,9 +306,13 @@ class Controller(Generic[Context]):
file_system_path = str(file_system.get_dir() / params.path)
params = UploadFileAction(index=params.index, path=file_system_path)
else:
- raise BrowserError(f'File path {params.path} is not available. Must be in available_file_paths, downloaded_files, or a file managed by file_system.')
+ raise BrowserError(
+ f'File path {params.path} is not available. Must be in available_file_paths, downloaded_files, or a file managed by file_system.'
+ )
else:
- raise BrowserError(f'File path {params.path} is not available. Must be in available_file_paths or downloaded_files.')
+ raise BrowserError(
+ f'File path {params.path} is not available. Must be in available_file_paths or downloaded_files.'
+ )
if not os.path.exists(params.path):
raise BrowserError(f'File {params.path} does not exist')
@@ -329,7 +335,7 @@ class Controller(Generic[Context]):
return None
if browser_session.is_file_input(n):
return n
- for child in (n.children_nodes or []):
+ for child in n.children_nodes or []:
result = find_file_input_in_descendants(child, depth - 1)
if result:
return result
@@ -346,7 +352,7 @@ class Controller(Generic[Context]):
return result
# Check all siblings and their descendants
if current.parent_node:
- for sibling in (current.parent_node.children_nodes or []):
+ for sibling in current.parent_node.children_nodes or []:
if sibling is current:
continue
if browser_session.is_file_input(sibling):
@@ -588,7 +594,7 @@ Provide the extracted information in a clear, structured format."""
await event
await event.event_result(raise_if_any=True, raise_if_none=False)
direction = 'down' if params.down else 'up'
-
+
# If index is 0 or None, we're scrolling the page
target = 'the page' if params.index is None or params.index == 0 else f'element {params.index}'
@@ -629,7 +635,7 @@ Provide the extracted information in a clear, structured format."""
async def scroll_to_text(text: str, browser_session: BrowserSession): # type: ignore
# Dispatch scroll to text event
event = browser_session.event_bus.dispatch(ScrollToTextEvent(text=text))
-
+
try:
# The handler returns None on success or raises an exception if text not found
await event.event_result(raise_if_any=True, raise_if_none=False)
diff --git a/browser_use/logging_config.py b/browser_use/logging_config.py
index 831388b71..2587a0dd2 100644
--- a/browser_use/logging_config.py
+++ b/browser_use/logging_config.py
@@ -131,11 +131,12 @@ def setup_logging(stream=None, log_level=None, force_setup=False):
# This enables the formatted CDP output at the same level as browser_use
try:
from cdp_use.logging import setup_cdp_logging # type: ignore
+
# Use the same stream and level as browser_use
setup_cdp_logging(
level=log_level,
stream=stream or sys.stdout,
- format_string='%(levelname)-8s [%(name)s] %(message)s' if log_type != 'result' else '%(message)s'
+ format_string='%(levelname)-8s [%(name)s] %(message)s' if log_type != 'result' else '%(message)s',
)
except ImportError:
# If cdp_use doesn't have the new logging module, fall back to manual config
@@ -187,19 +188,19 @@ def setup_logging(stream=None, log_level=None, force_setup=False):
class FIFOHandler(logging.Handler):
"""Non-blocking handler that writes to a named pipe."""
-
+
def __init__(self, fifo_path: str):
super().__init__()
self.fifo_path = fifo_path
Path(fifo_path).parent.mkdir(parents=True, exist_ok=True)
-
+
# Create FIFO if it doesn't exist
if not os.path.exists(fifo_path):
os.mkfifo(fifo_path)
-
+
# Don't open the FIFO yet - will open on first write
self.fd = None
-
+
def emit(self, record):
try:
# Open FIFO on first write if not already open
@@ -209,8 +210,8 @@ class FIFOHandler(logging.Handler):
except OSError:
# No reader connected yet, skip this message
return
-
- msg = f"{self.format(record)}\n".encode()
+
+ msg = f'{self.format(record)}\n'.encode()
os.write(self.fd, msg)
except (OSError, BrokenPipeError):
# Reader disconnected, close and reset
@@ -220,7 +221,7 @@ class FIFOHandler(logging.Handler):
except:
pass
self.fd = None
-
+
def close(self):
if hasattr(self, 'fd') and self.fd is not None:
try:
@@ -232,24 +233,24 @@ class FIFOHandler(logging.Handler):
def setup_log_pipes(session_id: str, base_dir: str = None):
"""Setup named pipes for log streaming.
-
+
Usage:
# In browser-use:
setup_log_pipes(session_id="abc123")
-
+
# In consumer process:
tail -f {temp_dir}/buagent.c123/agent.pipe
"""
import tempfile
-
+
if base_dir is None:
base_dir = tempfile.gettempdir()
-
+
suffix = session_id[-4:]
- pipe_dir = Path(base_dir) / f"buagent.{suffix}"
-
+ pipe_dir = Path(base_dir) / f'buagent.{suffix}'
+
# Agent logs
- agent_handler = FIFOHandler(str(pipe_dir / "agent.pipe"))
+ agent_handler = FIFOHandler(str(pipe_dir / 'agent.pipe'))
agent_handler.setLevel(logging.DEBUG)
agent_handler.setFormatter(logging.Formatter('%(levelname)-8s [%(name)s] %(message)s'))
for name in ['browser_use.agent', 'browser_use.controller']:
@@ -257,9 +258,9 @@ def setup_log_pipes(session_id: str, base_dir: str = None):
logger.addHandler(agent_handler)
logger.setLevel(logging.DEBUG)
logger.propagate = True
-
- # CDP logs
- cdp_handler = FIFOHandler(str(pipe_dir / "cdp.pipe"))
+
+ # CDP logs
+ cdp_handler = FIFOHandler(str(pipe_dir / 'cdp.pipe'))
cdp_handler.setLevel(logging.DEBUG)
cdp_handler.setFormatter(logging.Formatter('%(levelname)-8s [%(name)s] %(message)s'))
for name in ['websockets.client', 'cdp_use.client']:
@@ -267,9 +268,9 @@ def setup_log_pipes(session_id: str, base_dir: str = None):
logger.addHandler(cdp_handler)
logger.setLevel(logging.DEBUG)
logger.propagate = True
-
+
# Event logs
- event_handler = FIFOHandler(str(pipe_dir / "events.pipe"))
+ event_handler = FIFOHandler(str(pipe_dir / 'events.pipe'))
event_handler.setLevel(logging.INFO)
event_handler.setFormatter(logging.Formatter('%(levelname)-8s [%(name)s] %(message)s'))
for name in ['bubus', 'browser_use.browser.session']:
diff --git a/examples/custom-functions/cua.py b/examples/custom-functions/cua.py
index 415e8701c..6a77ffea0 100644
--- a/examples/custom-functions/cua.py
+++ b/examples/custom-functions/cua.py
@@ -63,7 +63,7 @@ async def handle_model_action(browser_session: BrowserSession, action) -> Action
# Not handling things like middle click, etc.
if button != 'left' and button != 'right':
button = 'left'
-
+
# Use CDP to click
await browser_session.agent_focus.cdp_client.send.Input.dispatchMouseEvent(
params={
@@ -91,7 +91,7 @@ async def handle_model_action(browser_session: BrowserSession, action) -> Action
x, y = action.x, action.y
scroll_x, scroll_y = action.scroll_x, action.scroll_y
print(f'Action: scroll at ({x}, {y}) with offsets (scroll_x={scroll_x}, scroll_y={scroll_y})')
-
+
# Move mouse to position first
await browser_session.agent_focus.cdp_client.send.Input.dispatchMouseEvent(
params={
@@ -101,7 +101,7 @@ async def handle_model_action(browser_session: BrowserSession, action) -> Action
},
session_id=browser_session.agent_focus.session_id,
)
-
+
# Execute scroll using JavaScript
await browser_session.agent_focus.cdp_client.send.Runtime.evaluate(
params={
@@ -122,7 +122,7 @@ async def handle_model_action(browser_session: BrowserSession, action) -> Action
key_code = 'Enter'
elif k.lower() == 'space':
key_code = 'Space'
-
+
# Use CDP to send key
await browser_session.agent_focus.cdp_client.send.Input.dispatchKeyEvent(
params={
@@ -144,7 +144,7 @@ async def handle_model_action(browser_session: BrowserSession, action) -> Action
case 'type':
text = action.text
print(f'Action: type text: {text}')
-
+
# Type text character by character
for char in text:
await browser_session.agent_focus.cdp_client.send.Input.dispatchKeyEvent(
diff --git a/examples/custom-functions/file_upload.py b/examples/custom-functions/file_upload.py
index 139d59f1b..a4a076c6b 100644
--- a/examples/custom-functions/file_upload.py
+++ b/examples/custom-functions/file_upload.py
@@ -38,28 +38,26 @@ async def upload_file(index: int, path: str, browser_session: BrowserSession, av
try:
# Get the DOM element by index
dom_element = await browser_session.get_dom_element_by_index(index)
-
+
if dom_element is None:
msg = f'No element found at index {index}'
logger.info(msg)
return ActionResult(error=msg)
-
+
# Check if it's a file input element
if dom_element.tag_name.lower() != 'input' or dom_element.attributes.get('type') != 'file':
msg = f'Element at index {index} is not a file input element'
logger.info(msg)
return ActionResult(error=msg)
-
+
# Dispatch the upload file event
- event = browser_session.event_bus.dispatch(
- UploadFileEvent(node=dom_element, file_path=path)
- )
+ event = browser_session.event_bus.dispatch(UploadFileEvent(node=dom_element, file_path=path))
await event
-
+
msg = f'Successfully uploaded file to index {index}'
logger.info(msg)
return ActionResult(extracted_content=msg, include_in_memory=True)
-
+
except Exception as e:
msg = f'Failed to upload file to index {index}: {str(e)}'
logger.info(msg)
@@ -113,4 +111,4 @@ async def main():
if __name__ == '__main__':
- asyncio.run(main())
\ No newline at end of file
+ asyncio.run(main())
diff --git a/examples/custom-functions/solve_amazon_captcha.py b/examples/custom-functions/solve_amazon_captcha.py
index f7156c3fd..36ae819a0 100644
--- a/examples/custom-functions/solve_amazon_captcha.py
+++ b/examples/custom-functions/solve_amazon_captcha.py
@@ -86,7 +86,7 @@ async def solve_amazon_captcha(browser_session: BrowserSession):
},
session_id=browser_session.agent_focus.session_id,
)
-
+
# Click submit button using CDP
await browser_session.agent_focus.cdp_client.send.Runtime.evaluate(
params={
diff --git a/examples/use-cases/find_and_apply_to_jobs.py b/examples/use-cases/find_and_apply_to_jobs.py
index 7852d9440..31f698099 100644
--- a/examples/use-cases/find_and_apply_to_jobs.py
+++ b/examples/use-cases/find_and_apply_to_jobs.py
@@ -80,14 +80,14 @@ def read_cv():
)
async def upload_cv(index: int, browser_session: BrowserSession):
path = str(CV.absolute())
-
+
# Get the element by index
dom_element = await browser_session.get_element_by_index(index)
-
+
if dom_element is None:
logger.info(f'No element found at index {index}')
return ActionResult(error=f'No element found at index {index}')
-
+
# Check if it's a file input element
if not browser_session.is_file_input(dom_element):
logger.info(f'Element at index {index} is not a file upload element')
diff --git a/examples/use-cases/google_sheets.py b/examples/use-cases/google_sheets.py
index cebc3a63b..f93076ed0 100644
--- a/examples/use-cases/google_sheets.py
+++ b/examples/use-cases/google_sheets.py
@@ -81,27 +81,27 @@ async def main():
await browser_session.stop()
# improvised_continuer = Agent(
- # task="""
- # Read the Google Sheet https://docs.google.com/spreadsheets/d/1INaIcfpYXlMRWO__de61SHFCaqt1lfHlcvtXZPItlpI/edit
- # Add 3 more rows to the bottom continuing the existing pattern, make sure any data you add is sourced correctly.
- # """,
- # llm=model,
- # browser_session=browser_session,
- # controller=controller,
- # )
- # await improvised_continuer.run()
-
- # final_fact_checker = Agent(
- # task="""
- # Read the Google Sheet https://docs.google.com/spreadsheets/d/1INaIcfpYXlMRWO__de61SHFCaqt1lfHlcvtXZPItlpI/edit
- # Fact-check every entry, add a new column F with your findings for each row.
- # Make sure to check the source URL for each row, and make sure the information is correct.
- # """,
- # llm=model,
- # browser_session=browser_session,
- # controller=controller,
- # )
- # await final_fact_checker.run()
+ # task="""
+ # Read the Google Sheet https://docs.google.com/spreadsheets/d/1INaIcfpYXlMRWO__de61SHFCaqt1lfHlcvtXZPItlpI/edit
+ # Add 3 more rows to the bottom continuing the existing pattern, make sure any data you add is sourced correctly.
+ # """,
+ # llm=model,
+ # browser_session=browser_session,
+ # controller=controller,
+ # )
+ # await improvised_continuer.run()
+
+ # final_fact_checker = Agent(
+ # task="""
+ # Read the Google Sheet https://docs.google.com/spreadsheets/d/1INaIcfpYXlMRWO__de61SHFCaqt1lfHlcvtXZPItlpI/edit
+ # Fact-check every entry, add a new column F with your findings for each row.
+ # Make sure to check the source URL for each row, and make sure the information is correct.
+ # """,
+ # llm=model,
+ # browser_session=browser_session,
+ # controller=controller,
+ # )
+ # await final_fact_checker.run()
if __name__ == '__main__':
diff --git a/examples/use-cases/play_chess.py b/examples/use-cases/play_chess.py
index 6253c2dfc..19b54ad72 100755
--- a/examples/use-cases/play_chess.py
+++ b/examples/use-cases/play_chess.py
@@ -102,7 +102,7 @@ async def calculate_square_size(browser: BrowserSession) -> float | None:
try:
if not browser.agent_focus:
return None
-
+
# Get board HTML using CDP
result = await browser.agent_focus.cdp_client.send.Runtime.evaluate(
params={
@@ -174,7 +174,7 @@ async def get_current_board_info(browser: BrowserSession) -> tuple[str | None, f
try:
if not browser.agent_focus:
return None, None
-
+
# Get board HTML using CDP
result = await browser.agent_focus.cdp_client.send.Runtime.evaluate(
params={
@@ -227,7 +227,7 @@ async def read_board(browser: BrowserSession):
# Get the current page's CDP session
if not browser.agent_focus:
return ActionResult(extracted_content='No active page to read board from.')
-
+
full_fen, _ = await get_current_board_info(browser)
if not full_fen:
@@ -319,7 +319,7 @@ async def play_move(params: PlayMoveParams, browser: BrowserSession):
try:
if not browser.agent_focus:
return ActionResult(extracted_content='No active page to play move on.')
-
+
click_offset = square_size / 2
start_click_x = start_x + click_offset
start_click_y = start_y + click_offset
@@ -327,7 +327,7 @@ async def play_move(params: PlayMoveParams, browser: BrowserSession):
end_click_y = end_y + click_offset
logger.debug(f"DEBUG: Playing SAN '{san_move}' (UCI: {uci_move}).")
-
+
# Get board element bounds first
result = await browser.agent_focus.cdp_client.send.Runtime.evaluate(
params={
@@ -345,10 +345,10 @@ async def play_move(params: PlayMoveParams, browser: BrowserSession):
session_id=browser.agent_focus.session_id,
)
board_rect = result.get('result', {}).get('value')
-
+
if not board_rect:
return ActionResult(extracted_content='Could not find chess board element.')
-
+
# Click start position
await browser.agent_focus.cdp_client.send.Input.dispatchMouseEvent(
params={
@@ -369,9 +369,9 @@ async def play_move(params: PlayMoveParams, browser: BrowserSession):
},
session_id=browser.agent_focus.session_id,
)
-
+
await asyncio.sleep(0.5)
-
+
# Click end position
await browser.agent_focus.cdp_client.send.Input.dispatchMouseEvent(
params={
diff --git a/tests/ci/test_browser_event_ClickElementEvent.py b/tests/ci/test_browser_event_ClickElementEvent.py
index e06befa1e..c3585f382 100644
--- a/tests/ci/test_browser_event_ClickElementEvent.py
+++ b/tests/ci/test_browser_event_ClickElementEvent.py
@@ -270,7 +270,7 @@ class TestClickElementEvent:
# Verify we're still on the original tab (not switched) - matches browser Cmd/Ctrl+click behavior
current_url = await browser_session.get_current_page_url()
assert f'{base_url}/newTab' in current_url, f'Should still be on original tab, but got {current_url}'
-
+
# Wait for the new tab to finish navigating to the target URL
# New tabs initially open at the current URL then navigate to the target
max_wait = 5
@@ -280,7 +280,7 @@ class TestClickElementEvent:
new_tab = tabs[-1] # Last tab is the newly opened one
if f'{base_url}/page1' in new_tab.url:
break
-
+
# Verify the new tab has the correct URL
assert f'{base_url}/page1' in new_tab.url, f'New tab should have page1 URL, but got {new_tab.url}'
@@ -730,13 +730,8 @@ class TestClickElementEvent:
async def test_click_triggers_alert_popup(self, browser_session, base_url, http_server):
"""Test that clicking a button triggers an alert dialog that is auto-accepted."""
- from browser_use.browser.events import (
- NavigateToUrlEvent,
- BrowserStateRequestEvent,
- ClickElementEvent,
- DialogOpenedEvent
- )
-
+ from browser_use.browser.events import BrowserStateRequestEvent, ClickElementEvent, DialogOpenedEvent, NavigateToUrlEvent
+
# Add route with alert dialog
http_server.expect_request('/alert_test').respond_with_data(
"""
@@ -758,9 +753,7 @@ class TestClickElementEvent:
)
# Navigate to the alert test page using events
- nav_event = browser_session.event_bus.dispatch(
- NavigateToUrlEvent(url=f'{base_url}/alert_test')
- )
+ nav_event = browser_session.event_bus.dispatch(NavigateToUrlEvent(url=f'{base_url}/alert_test'))
await nav_event
await asyncio.sleep(0.5)
@@ -781,16 +774,14 @@ class TestClickElementEvent:
dialog_event_future = browser_session.event_bus.expect(DialogOpenedEvent)
# Click the alert button using ClickElementEvent
- click_event = browser_session.event_bus.dispatch(
- ClickElementEvent(node=alert_button)
- )
+ click_event = browser_session.event_bus.dispatch(ClickElementEvent(node=alert_button))
await click_event
# Wait for and verify DialogOpenedEvent was dispatched
dialog_event = await asyncio.wait_for(dialog_event_future, timeout=2.0)
assert dialog_event.dialog_type == 'alert'
assert 'This is an alert!' in dialog_event.message
-
+
# Verify the page updated after alert was accepted
cdp_session = await browser_session.get_or_create_cdp_session()
result_js = await cdp_session.cdp_client.send.Runtime.evaluate(
@@ -801,13 +792,8 @@ class TestClickElementEvent:
async def test_click_triggers_confirm_popup(self, browser_session, base_url, http_server):
"""Test that clicking a button triggers a confirm dialog that is auto-accepted."""
- from browser_use.browser.events import (
- NavigateToUrlEvent,
- BrowserStateRequestEvent,
- ClickElementEvent,
- DialogOpenedEvent
- )
-
+ from browser_use.browser.events import BrowserStateRequestEvent, ClickElementEvent, DialogOpenedEvent, NavigateToUrlEvent
+
# Add route with confirm dialog
http_server.expect_request('/confirm_test').respond_with_data(
"""
@@ -829,16 +815,14 @@ class TestClickElementEvent:
)
# Navigate to the confirm test page
- nav_event = browser_session.event_bus.dispatch(
- NavigateToUrlEvent(url=f'{base_url}/confirm_test')
- )
+ nav_event = browser_session.event_bus.dispatch(NavigateToUrlEvent(url=f'{base_url}/confirm_test'))
await nav_event
await asyncio.sleep(0.5)
# Get the browser state
state_event = browser_session.event_bus.dispatch(BrowserStateRequestEvent())
browser_state = await state_event.event_result(raise_if_none=True, raise_if_any=True)
-
+
# Find the confirm button
confirm_button = None
for element in browser_state.dom_state.selector_map.values():
@@ -852,9 +836,7 @@ class TestClickElementEvent:
dialog_event_future = browser_session.event_bus.expect(DialogOpenedEvent)
# Click the confirm button
- click_event = browser_session.event_bus.dispatch(
- ClickElementEvent(node=confirm_button)
- )
+ click_event = browser_session.event_bus.dispatch(ClickElementEvent(node=confirm_button))
await click_event
# Wait for and verify DialogOpenedEvent was dispatched
@@ -872,13 +854,8 @@ class TestClickElementEvent:
async def test_page_usable_after_popup_confirm(self, browser_session, base_url, http_server):
"""Test that the page remains usable after handling confirm dialogs."""
- from browser_use.browser.events import (
- NavigateToUrlEvent,
- BrowserStateRequestEvent,
- ClickElementEvent,
- DialogOpenedEvent
- )
-
+ from browser_use.browser.events import BrowserStateRequestEvent, ClickElementEvent, DialogOpenedEvent, NavigateToUrlEvent
+
# Add route with confirm dialog and navigation
http_server.expect_request('/popup_nav_test').respond_with_data(
"""
@@ -901,16 +878,14 @@ class TestClickElementEvent:
)
# Navigate to the test page
- nav_event = browser_session.event_bus.dispatch(
- NavigateToUrlEvent(url=f'{base_url}/popup_nav_test')
- )
+ nav_event = browser_session.event_bus.dispatch(NavigateToUrlEvent(url=f'{base_url}/popup_nav_test'))
await nav_event
await asyncio.sleep(0.5)
# Get browser state
state_event = browser_session.event_bus.dispatch(BrowserStateRequestEvent())
browser_state = await state_event
-
+
# Find and click the confirm button
confirm_button = None
for element in browser_state.dom_state.selector_map.values():
@@ -924,9 +899,7 @@ class TestClickElementEvent:
dialog_event_future = browser_session.event_bus.expect(DialogOpenedEvent)
# Click confirm button
- click_event = browser_session.event_bus.dispatch(
- ClickElementEvent(node=confirm_button)
- )
+ click_event = browser_session.event_bus.dispatch(ClickElementEvent(node=confirm_button))
await click_event
# Wait for dialog event
@@ -955,9 +928,7 @@ class TestClickElementEvent:
assert nav_link is not None, 'Could not find navigation link'
# Click the navigation link
- click_event = browser_session.event_bus.dispatch(
- ClickElementEvent(node=nav_link)
- )
+ click_event = browser_session.event_bus.dispatch(ClickElementEvent(node=nav_link))
await click_event
await asyncio.sleep(1)
@@ -971,13 +942,8 @@ class TestClickElementEvent:
async def test_click_triggers_onbeforeunload_popup(self, browser_session, base_url, http_server):
"""Test that navigating away from a page with onbeforeunload triggers a dialog."""
- from browser_use.browser.events import (
- NavigateToUrlEvent,
- BrowserStateRequestEvent,
- ClickElementEvent,
- DialogOpenedEvent
- )
-
+ from browser_use.browser.events import BrowserStateRequestEvent, ClickElementEvent, DialogOpenedEvent, NavigateToUrlEvent
+
# Add route with onbeforeunload handler
http_server.expect_request('/beforeunload_test').respond_with_data(
"""
@@ -1005,9 +971,7 @@ class TestClickElementEvent:
)
# Navigate to the beforeunload test page
- nav_event = browser_session.event_bus.dispatch(
- NavigateToUrlEvent(url=f'{base_url}/beforeunload_test')
- )
+ nav_event = browser_session.event_bus.dispatch(NavigateToUrlEvent(url=f'{base_url}/beforeunload_test'))
await nav_event
await asyncio.sleep(0.5)
@@ -1028,9 +992,7 @@ class TestClickElementEvent:
dialog_event_future = browser_session.event_bus.expect(DialogOpenedEvent)
# Click the navigation link - should trigger beforeunload popup
- click_event = browser_session.event_bus.dispatch(
- ClickElementEvent(node=nav_link)
- )
+ click_event = browser_session.event_bus.dispatch(ClickElementEvent(node=nav_link))
await click_event
# Wait for and verify DialogOpenedEvent was dispatched
@@ -1043,16 +1005,19 @@ class TestClickElementEvent:
# Verify navigation succeeded after beforeunload was accepted
current_url = await browser_session.get_current_page_url()
- assert f'{base_url}/page1' in current_url, f'Navigation should have succeeded after beforeunload was accepted, current URL: {current_url}'
+ assert f'{base_url}/page1' in current_url, (
+ f'Navigation should have succeeded after beforeunload was accepted, current URL: {current_url}'
+ )
async def test_file_upload_click_and_verify(self, controller, browser_session, base_url, http_server):
"""Test that clicking a file upload element and uploading a file works correctly."""
# Create a temporary test file
import tempfile as temp_module
+
with temp_module.NamedTemporaryFile(mode='w', suffix='.txt', delete=False) as temp_file:
temp_file.write('Test file content for upload')
temp_file_path = temp_file.name
-
+
try:
# Add route for file upload test page
http_server.expect_request('/fileupload').respond_with_data(
@@ -1157,11 +1122,12 @@ class TestClickElementEvent:
# Create a temporary FileSystem for the test
import tempfile
+
from browser_use.filesystem.file_system import FileSystem
-
+
with tempfile.TemporaryDirectory() as temp_dir:
file_system = FileSystem(base_dir=temp_dir)
-
+
# Upload the file using the label index (should find the associated file input)
result = await controller.act(
UploadFileActionModel(upload_file_to_element=UploadFileAction(index=label_index, path=temp_file_path)),
@@ -1180,7 +1146,7 @@ class TestClickElementEvent:
# Verify the file was actually selected using CDP Runtime.evaluate
cdp_session = await browser_session.get_or_create_cdp_session()
-
+
# Check if the file input has a file selected
file_check_js = await browser_session.cdp_client.send.Runtime.evaluate(
params={
@@ -1203,12 +1169,12 @@ class TestClickElementEvent:
},
session_id=cdp_session.session_id,
)
-
+
file_info = file_check_js.get('result', {}).get('value', {})
-
+
# Verify file was selected
assert file_info.get('hasFile') is True, 'File was not properly selected in the input element'
- assert file_info.get('fileName', '').endswith('.txt'), f"Expected .txt file, got: {file_info.get('fileName')}"
+ assert file_info.get('fileName', '').endswith('.txt'), f'Expected .txt file, got: {file_info.get("fileName")}'
assert file_info.get('fileSize', 0) > 0, 'File size should be greater than 0'
# Also verify the UI was updated (the file info div)
@@ -1229,13 +1195,13 @@ class TestClickElementEvent:
},
session_id=cdp_session.session_id,
)
-
+
ui_info = ui_check_js.get('result', {}).get('value', {})
-
+
# Verify UI was updated
assert ui_info.get('hasFileInfo') is True, 'UI was not updated with file information'
- assert '.txt' in ui_info.get('fileNameText', ''), f"File name not shown in UI: {ui_info.get('fileNameText')}"
- assert 'bytes' in ui_info.get('fileSizeText', ''), f"File size not shown in UI: {ui_info.get('fileSizeText')}"
+ assert '.txt' in ui_info.get('fileNameText', ''), f'File name not shown in UI: {ui_info.get("fileNameText")}'
+ assert 'bytes' in ui_info.get('fileSizeText', ''), f'File size not shown in UI: {ui_info.get("fileSizeText")}'
finally:
# Clean up the temporary file
@@ -1243,11 +1209,11 @@ class TestClickElementEvent:
async def test_file_upload_path_validation(self, controller, browser_session, base_url, http_server):
"""Test that file upload validates paths correctly with available_file_paths, downloaded_files, and FileSystem."""
- import tempfile
from pathlib import Path
+
from browser_use.browser.views import BrowserError
- from browser_use.filesystem.file_system import FileSystem
from browser_use.controller.views import UploadFileAction
+ from browser_use.filesystem.file_system import FileSystem
# Create a temporary test file that's NOT in available_file_paths
with tempfile.NamedTemporaryFile(mode='w', suffix='.txt', delete=False) as temp_file:
@@ -1280,6 +1246,7 @@ class TestClickElementEvent:
# Get browser state to populate selector map
from browser_use.browser.events import BrowserStateRequestEvent
+
event = browser_session.event_bus.dispatch(BrowserStateRequestEvent())
state = await event
@@ -1292,58 +1259,56 @@ class TestClickElementEvent:
# Create a temporary FileSystem for all tests
with tempfile.TemporaryDirectory() as temp_dir:
file_system = FileSystem(base_dir=temp_dir)
-
+
try:
# This should fail because the file is not in available_file_paths
result = await controller.act(
- upload_action,
+ upload_action,
browser_session,
available_file_paths=[], # Empty available_file_paths
- file_system=file_system
+ file_system=file_system,
)
- assert result.error is not None, "Upload should have failed for file not in available_file_paths"
- assert "not available" in result.error, f"Error message should mention file not available: {result.error}"
+ assert result.error is not None, 'Upload should have failed for file not in available_file_paths'
+ assert 'not available' in result.error, f'Error message should mention file not available: {result.error}'
except BrowserError as e:
- assert "not available" in str(e), f"Error should mention file not available: {e}"
+ assert 'not available' in str(e), f'Error should mention file not available: {e}'
# Test 2: Add file to available_file_paths - should succeed
result = await controller.act(
upload_action,
browser_session,
available_file_paths=[test_file_path], # File is now in available_file_paths
- file_system=file_system
+ file_system=file_system,
)
- assert result.error is None, f"Upload should have succeeded with file in available_file_paths: {result.error}"
+ assert result.error is None, f'Upload should have succeeded with file in available_file_paths: {result.error}'
# Test 3: Test with FileSystem integration - write a test file to the FileSystem
await file_system.write_file('test.txt', 'FileSystem test content')
fs_file_path = str(file_system.get_dir() / 'test.txt')
-
+
# Try to upload using just the filename (should check FileSystem)
- upload_action_fs = UploadActionModel(
- upload_file_to_element=UploadFileAction(index=1, path='test.txt')
- )
-
+ upload_action_fs = UploadActionModel(upload_file_to_element=UploadFileAction(index=1, path='test.txt'))
+
result = await controller.act(
upload_action_fs,
browser_session,
available_file_paths=[], # Empty available_file_paths
- file_system=file_system # But FileSystem is provided
+ file_system=file_system, # But FileSystem is provided
)
- assert result.error is None, f"Upload should have succeeded with file in FileSystem: {result.error}"
+ assert result.error is None, f'Upload should have succeeded with file in FileSystem: {result.error}'
# Test 4: Simulate a downloaded file
# Manually add a file to browser_session._downloaded_files to simulate a download
browser_session._downloaded_files.append(test_file_path)
-
+
# Try uploading with the file only in downloaded_files
result = await controller.act(
upload_action,
browser_session,
available_file_paths=[], # Empty available_file_paths, but file is in downloaded_files
- file_system=file_system
+ file_system=file_system,
)
- assert result.error is None, f"Upload should have succeeded with file in downloaded_files: {result.error}"
+ assert result.error is None, f'Upload should have succeeded with file in downloaded_files: {result.error}'
finally:
# Clean up the temporary file
diff --git a/tests/ci/test_browser_watchdog_aboutblank.py b/tests/ci/test_browser_watchdog_aboutblank.py
index 1de62e46c..5900c8d87 100644
--- a/tests/ci/test_browser_watchdog_aboutblank.py
+++ b/tests/ci/test_browser_watchdog_aboutblank.py
@@ -210,6 +210,7 @@ async def test_aboutblank_watchdog_javascript_execution():
# Test 2: Close the tab and verify watchdog creates new about:blank tab with animation
from browser_use.browser.events import CloseTabEvent
+
event = session.event_bus.dispatch(CloseTabEvent(tab_index=dvd_event1.tab_index))
await event
diff --git a/tests/ci/test_browser_watchdog_permissions.py b/tests/ci/test_browser_watchdog_permissions.py
index d6b909358..7635f8554 100644
--- a/tests/ci/test_browser_watchdog_permissions.py
+++ b/tests/ci/test_browser_watchdog_permissions.py
@@ -1,10 +1,12 @@
"""Test that verifies permissions watchdog is working correctly."""
import asyncio
+
import pytest
+
from browser_use import Agent
-from browser_use.browser.session import BrowserSession
from browser_use.browser.profile import BrowserProfile
+from browser_use.browser.session import BrowserSession
from tests.ci.conftest import create_mock_llm
@@ -12,7 +14,7 @@ from tests.ci.conftest import create_mock_llm
async def test_permissions_are_granted_on_connect(httpserver, caplog):
"""Test that permissions are granted when browser connects."""
# Set up test HTML that requests geolocation
- httpserver.expect_request("/geo").respond_with_data(
+ httpserver.expect_request('/geo').respond_with_data(
"""
<html>
<body>
@@ -40,16 +42,14 @@ async def test_permissions_are_granted_on_connect(httpserver, caplog):
</html>
"""
)
-
+
# Create a profile with geolocation permissions
- profile = BrowserProfile(
- permissions=['geolocation'],
- headless=True
- )
-
+ profile = BrowserProfile(permissions=['geolocation'], headless=True)
+
# Create a mock LLM that will navigate and click the button
- mock_llm = create_mock_llm([
- """
+ mock_llm = create_mock_llm(
+ [
+ """
{
"thinking": "Navigating to the geolocation test page",
"evaluation_previous_goal": "Starting",
@@ -63,8 +63,9 @@ async def test_permissions_are_granted_on_connect(httpserver, caplog):
}
]
}
- """ % httpserver.url_for('/geo'),
"""
+ % httpserver.url_for('/geo'),
+ """
{
"thinking": "Clicking the get location button",
"evaluation_previous_goal": "Navigated successfully",
@@ -78,24 +79,33 @@ async def test_permissions_are_granted_on_connect(httpserver, caplog):
}
]
}
- """
- ])
-
+ """,
+ ]
+ )
+
# Create browser and agent
browser = BrowserSession(browser_profile=profile)
- agent = Agent(task="Test geolocation permissions", browser=browser, llm=mock_llm)
-
+ agent = Agent(task='Test geolocation permissions', browser=browser, llm=mock_llm)
+
# Run task
await agent.run(max_steps=3)
-
+
# Check that permissions were granted
- grant_logs = [record for record in caplog.records if "Granting browser permissions" in record.message or "Browser.grantPermissions" in record.message]
- assert len(grant_logs) > 0, "Expected Browser.grantPermissions to be called"
-
+ grant_logs = [
+ record
+ for record in caplog.records
+ if 'Granting browser permissions' in record.message or 'Browser.grantPermissions' in record.message
+ ]
+ assert len(grant_logs) > 0, 'Expected Browser.grantPermissions to be called'
+
# Check that the correct permission was granted
- geo_permission_logs = [record for record in caplog.records if "geolocation" in record.message and ("granting" in record.message.lower() or "granted" in record.message.lower())]
- assert len(geo_permission_logs) > 0, "Expected geolocation permission to be granted"
-
+ geo_permission_logs = [
+ record
+ for record in caplog.records
+ if 'geolocation' in record.message and ('granting' in record.message.lower() or 'granted' in record.message.lower())
+ ]
+ assert len(geo_permission_logs) > 0, 'Expected geolocation permission to be granted'
+
# Clean up
await browser.stop()
@@ -105,30 +115,31 @@ async def test_multiple_permissions_are_granted(caplog):
"""Test that multiple permissions are granted correctly."""
# Create a profile with multiple permissions
# Use CDP permission names directly
- profile = BrowserProfile(
- permissions=['geolocation', 'clipboardReadWrite', 'notifications'],
- headless=True
- )
-
+ profile = BrowserProfile(permissions=['geolocation', 'clipboardReadWrite', 'notifications'], headless=True)
+
# Create browser
browser = BrowserSession(browser_profile=profile)
-
+
# Start browser (this will trigger PermissionsWatchdog)
await browser.start()
-
+
# Give it time to initialize
await asyncio.sleep(0.5)
-
+
# Check that permissions were granted
- grant_logs = [record for record in caplog.records if "Granting browser permissions" in record.message or "Successfully granted permissions" in record.message]
- assert len(grant_logs) > 0, "Expected permissions to be granted"
-
+ grant_logs = [
+ record
+ for record in caplog.records
+ if 'Granting browser permissions' in record.message or 'Successfully granted permissions' in record.message
+ ]
+ assert len(grant_logs) > 0, 'Expected permissions to be granted'
+
# Check that all permissions were mentioned
permissions_to_check = ['geolocation', 'clipboardReadWrite', 'notifications']
for perm in permissions_to_check:
perm_logs = [record for record in caplog.records if perm in record.message]
- assert len(perm_logs) > 0, f"Expected {perm} permission to be granted"
-
+ assert len(perm_logs) > 0, f'Expected {perm} permission to be granted'
+
# Clean up
await browser.stop()
@@ -137,24 +148,25 @@ async def test_multiple_permissions_are_granted(caplog):
async def test_no_permissions_when_empty_list(caplog):
"""Test that no permissions are granted when list is empty."""
# Create a profile with no permissions
- profile = BrowserProfile(
- permissions=[],
- headless=True
- )
-
+ profile = BrowserProfile(permissions=[], headless=True)
+
# Create browser
browser = BrowserSession(browser_profile=profile)
-
+
# Start browser
await browser.start()
-
+
# Give it time to initialize
await asyncio.sleep(0.5)
-
+
# Check that permissions were not granted
- grant_logs = [record for record in caplog.records if "Granting browser permissions" in record.message or "Successfully granted permissions" in record.message]
- assert len(grant_logs) == 0, "Expected no permissions to be granted when list is empty"
-
+ grant_logs = [
+ record
+ for record in caplog.records
+ if 'Granting browser permissions' in record.message or 'Successfully granted permissions' in record.message
+ ]
+ assert len(grant_logs) == 0, 'Expected no permissions to be granted when list is empty'
+
# Clean up
await browser.stop()
@@ -163,27 +175,30 @@ async def test_no_permissions_when_empty_list(caplog):
async def test_permissions_watchdog_handles_invalid_permissions(caplog):
"""Test that invalid permissions are handled gracefully."""
# Create a profile with an invalid permission
- profile = BrowserProfile(
- permissions=['geolocation', 'invalid-permission', 'clipboardReadWrite'],
- headless=True
- )
-
+ profile = BrowserProfile(permissions=['geolocation', 'invalid-permission', 'clipboardReadWrite'], headless=True)
+
# Create browser
browser = BrowserSession(browser_profile=profile)
-
+
# Start browser (should not crash despite invalid permission)
await browser.start()
-
+
# Give it time to initialize
await asyncio.sleep(0.5)
-
+
# Check that permissions were still granted (despite invalid permission)
- grant_logs = [record for record in caplog.records if "Granting browser permissions" in record.message or "Successfully granted permissions" in record.message]
- assert len(grant_logs) > 0, "Expected permissions to be granted"
-
+ grant_logs = [
+ record
+ for record in caplog.records
+ if 'Granting browser permissions' in record.message or 'Successfully granted permissions' in record.message
+ ]
+ assert len(grant_logs) > 0, 'Expected permissions to be granted'
+
# Check for warning about invalid permission
- warning_logs = [record for record in caplog.records if "invalid-permission" in record.message or "Unknown permission" in record.message]
+ warning_logs = [
+ record for record in caplog.records if 'invalid-permission' in record.message or 'Unknown permission' in record.message
+ ]
# This is OK if there's no warning - CDP might just ignore it
-
+
# Clean up
- await browser.stop()
\ No newline at end of file
+ await browser.stop()
diff --git a/tests/ci/test_browser_watchdog_storage_state.py b/tests/ci/test_browser_watchdog_storage_state.py
index a650e487f..3247ca5d5 100644
--- a/tests/ci/test_browser_watchdog_storage_state.py
+++ b/tests/ci/test_browser_watchdog_storage_state.py
@@ -81,6 +81,7 @@ async def test_storage_state_watchdog_save_event():
# Navigate to create some context
from browser_use.browser.events import NavigateToUrlEvent
+
nav_event = session.event_bus.dispatch(NavigateToUrlEvent(url='data:text/html,<h1>Test Page</h1>'))
await nav_event
@@ -201,7 +202,10 @@ async def test_storage_state_watchdog_auto_save():
# Navigate to create some context that might change storage
from browser_use.browser.events import NavigateToUrlEvent
- event = session.event_bus.dispatch(NavigateToUrlEvent(url='data:text/html,<script>document.cookie="auto_test=value"</script>'))
+
+ event = session.event_bus.dispatch(
+ NavigateToUrlEvent(url='data:text/html,<script>document.cookie="auto_test=value"</script>')
+ )
await event
# Wait a bit longer than auto-save interval
diff --git a/tests/ci/test_cdp_new_tab_session.py b/tests/ci/test_cdp_new_tab_session.py
index d8b40656e..c86c1bafa 100644
--- a/tests/ci/test_cdp_new_tab_session.py
+++ b/tests/ci/test_cdp_new_tab_session.py
@@ -3,15 +3,16 @@
import asyncio
import pytest
+
from browser_use.browser.events import NavigateToUrlEvent, TabCreatedEvent
-from browser_use.browser.session import BrowserSession
from browser_use.browser.profile import BrowserProfile
+from browser_use.browser.session import BrowserSession
@pytest.fixture
async def httpserver_url(httpserver):
"""Create a local HTTP server for testing."""
- httpserver.expect_request("/").respond_with_data(
+ httpserver.expect_request('/').respond_with_data(
"""
<html>
<head><title>Test Page</title></head>
@@ -21,112 +22,99 @@ async def httpserver_url(httpserver):
</body>
</html>
""",
- content_type="text/html",
+ content_type='text/html',
)
- return httpserver.url_for("/")
+ return httpserver.url_for('/')
async def test_new_tab_cdp_session_attachment(httpserver_url):
"""Test that CDP session is properly attached when creating new tabs."""
- browser = BrowserSession(browser_profile=BrowserProfile(
- headless=True,
- viewport={'width': 800, 'height': 600}
- ))
-
+ browser = BrowserSession(browser_profile=BrowserProfile(headless=True, viewport={'width': 800, 'height': 600}))
+
tab_created_events = []
-
+
# Track TabCreatedEvent to verify it's dispatched correctly
browser.event_bus.on(TabCreatedEvent, lambda event: tab_created_events.append(event))
-
+
try:
await browser.start()
-
+
# Navigate to initial page
nav_event = browser.event_bus.dispatch(NavigateToUrlEvent(url=httpserver_url))
await nav_event
-
+
# Clear any initial tab created events
tab_created_events.clear()
-
+
# Now create a new tab - this should trigger the CDP error if not fixed
- new_tab_event = browser.event_bus.dispatch(NavigateToUrlEvent(
- url=httpserver_url,
- new_tab=True
- ))
+ new_tab_event = browser.event_bus.dispatch(NavigateToUrlEvent(url=httpserver_url, new_tab=True))
await new_tab_event
-
+
# Wait a bit for all events to process
await asyncio.sleep(1)
-
+
# Verify that TabCreatedEvent was dispatched
- assert len(tab_created_events) == 1, f"Expected 1 TabCreatedEvent, got {len(tab_created_events)}"
+ assert len(tab_created_events) == 1, f'Expected 1 TabCreatedEvent, got {len(tab_created_events)}'
assert tab_created_events[0].url == httpserver_url
-
+
# Verify we have 2 tabs now
tabs = await browser.get_tabs()
- assert len(tabs) == 2, f"Expected 2 tabs, got {len(tabs)}"
-
+ assert len(tabs) == 2, f'Expected 2 tabs, got {len(tabs)}'
+
# Verify the CDP session is attached to the new tab
assert browser.agent_focus is not None
assert browser.agent_focus.target_id is not None
-
+
# Try to execute a CDP command on the new tab to verify it works
cdp_session = await browser.get_or_create_cdp_session()
result = await cdp_session.cdp_client.send.Runtime.evaluate(
- params={'expression': 'document.title'},
- session_id=cdp_session.session_id
+ params={'expression': 'document.title'}, session_id=cdp_session.session_id
)
assert result['result']['value'] == 'Test Page'
-
+
# Get browser state to verify DOM can be built on new tab
from browser_use.browser.events import BrowserStateRequestEvent
+
state_event = browser.event_bus.dispatch(BrowserStateRequestEvent())
state = await state_event.event_result()
-
+
# Verify state was retrieved without errors
assert state is not None
assert state.dom_state is not None
-
+
finally:
await browser.stop()
async def test_multiple_new_tabs_cdp_session(httpserver_url):
"""Test creating multiple new tabs in succession."""
- browser = BrowserSession(browser_profile=BrowserProfile(
- headless=True,
- viewport={'width': 800, 'height': 600}
- ))
-
+ browser = BrowserSession(browser_profile=BrowserProfile(headless=True, viewport={'width': 800, 'height': 600}))
+
try:
await browser.start()
-
+
# Navigate to initial page
nav_event = browser.event_bus.dispatch(NavigateToUrlEvent(url=httpserver_url))
await nav_event
-
+
# Create multiple new tabs quickly
for i in range(3):
- new_tab_event = browser.event_bus.dispatch(NavigateToUrlEvent(
- url=f"{httpserver_url}?tab={i}",
- new_tab=True
- ))
+ new_tab_event = browser.event_bus.dispatch(NavigateToUrlEvent(url=f'{httpserver_url}?tab={i}', new_tab=True))
await new_tab_event
-
+
# Wait for events to process
await asyncio.sleep(1)
-
+
# Verify we have 4 tabs total (1 initial + 3 new)
tabs = await browser.get_tabs()
- assert len(tabs) == 4, f"Expected 4 tabs, got {len(tabs)}"
-
+ assert len(tabs) == 4, f'Expected 4 tabs, got {len(tabs)}'
+
# Verify CDP commands work on the current tab
cdp_session = await browser.get_or_create_cdp_session()
result = await cdp_session.cdp_client.send.Runtime.evaluate(
- params={'expression': 'window.location.href'},
- session_id=cdp_session.session_id
+ params={'expression': 'window.location.href'}, session_id=cdp_session.session_id
)
- assert 'tab=2' in result['result']['value'], "Should be on the last created tab"
-
+ assert 'tab=2' in result['result']['value'], 'Should be on the last created tab'
+
finally:
await browser.stop()
diff --git a/tests/ci/test_download_upload_full_circle.py b/tests/ci/test_download_upload_full_circle.py
index d6c0dde82..131f57586 100644
--- a/tests/ci/test_download_upload_full_circle.py
+++ b/tests/ci/test_download_upload_full_circle.py
@@ -8,12 +8,12 @@ from pathlib import Path
import pytest
from pytest_httpserver import HTTPServer
-from browser_use.agent.views import ActionModel, ActionResult
+from browser_use.agent.views import ActionModel
from browser_use.browser import BrowserSession
-from browser_use.browser.events import FileDownloadedEvent, BrowserStateRequestEvent
+from browser_use.browser.events import BrowserStateRequestEvent, FileDownloadedEvent
from browser_use.browser.profile import BrowserProfile
from browser_use.controller.service import Controller
-from browser_use.controller.views import GoToUrlAction, ClickElementAction, UploadFileAction
+from browser_use.controller.views import ClickElementAction, GoToUrlAction, UploadFileAction
from browser_use.filesystem.file_system import FileSystem
@@ -22,21 +22,19 @@ def download_upload_server():
"""Create a test HTTP server with download and upload endpoints."""
server = HTTPServer()
server.start()
-
+
# Test file content and hash
test_content = b'This is a test file for download-upload verification. Random: 12345'
test_hash = hashlib.sha256(test_content).hexdigest()
-
+
# Store uploaded files data
uploaded_files = []
-
+
# Add download endpoint
server.expect_request('/download/test-file.txt').respond_with_data(
- test_content,
- content_type='text/plain',
- headers={'Content-Disposition': 'attachment; filename="test-file.txt"'}
+ test_content, content_type='text/plain', headers={'Content-Disposition': 'attachment; filename="test-file.txt"'}
)
-
+
# Add upload page
upload_page_html = """
<!DOCTYPE html>
@@ -88,26 +86,25 @@ def download_upload_server():
</body>
</html>
"""
-
- server.expect_request('/upload-page').respond_with_data(
- upload_page_html,
- content_type='text/html'
- )
-
+
+ server.expect_request('/upload-page').respond_with_data(upload_page_html, content_type='text/html')
+
# Handle upload POST request (for server-side verification)
def handle_upload(request):
# Store uploaded file info for verification
if request.files and 'file' in request.files:
file_data = request.files['file'][0]
- uploaded_files.append({
- 'filename': file_data['filename'],
- 'content': file_data['body'],
- 'hash': hashlib.sha256(file_data['body']).hexdigest()
- })
+ uploaded_files.append(
+ {
+ 'filename': file_data['filename'],
+ 'content': file_data['body'],
+ 'hash': hashlib.sha256(file_data['body']).hexdigest(),
+ }
+ )
return request.make_response({'status': 'ok'})
-
+
server.expect_request('/upload', method='POST').respond_with_handler(handle_upload)
-
+
# Add download page with link
download_page_html = f"""
<!DOCTYPE html>
@@ -122,30 +119,27 @@ def download_upload_server():
</body>
</html>
"""
-
- server.expect_request('/download-page').respond_with_data(
- download_page_html,
- content_type='text/html'
- )
-
+
+ server.expect_request('/download-page').respond_with_data(download_page_html, content_type='text/html')
+
server.test_hash = test_hash
server.uploaded_files = uploaded_files
-
+
yield server
server.stop()
class TestDownloadUploadFullCircle:
"""Test full circle: download a file and then upload it back"""
-
+
async def test_download_then_upload_with_hash_verification(self, download_upload_server):
"""Download a file, then upload it to another page, verify hash matches"""
-
+
# Create temporary directory for downloads
with tempfile.TemporaryDirectory() as tmpdir:
downloads_path = Path(tmpdir) / 'downloads'
downloads_path.mkdir()
-
+
# Create browser session with downloads enabled
browser_session = BrowserSession(
browser_profile=BrowserProfile(
@@ -154,108 +148,106 @@ class TestDownloadUploadFullCircle:
user_data_dir=None,
)
)
-
+
await browser_session.start()
-
+
# Create controller and file system
controller = Controller()
file_system = FileSystem(base_dir=tmpdir)
-
+
try:
base_url = f'http://{download_upload_server.host}:{download_upload_server.port}'
-
+
# Step 1: Navigate to download page
class GoToUrlActionModel(ActionModel):
go_to_url: GoToUrlAction | None = None
-
+
result = await controller.act(
- GoToUrlActionModel(go_to_url=GoToUrlAction(url=f'{base_url}/download-page', new_tab=False)),
- browser_session
+ GoToUrlActionModel(go_to_url=GoToUrlAction(url=f'{base_url}/download-page', new_tab=False)), browser_session
)
- assert result.error is None, f"Navigation to download page failed: {result.error}"
-
+ assert result.error is None, f'Navigation to download page failed: {result.error}'
+
await asyncio.sleep(0.5)
-
+
# Get browser state to find download link
event = browser_session.event_bus.dispatch(BrowserStateRequestEvent())
state_result = await event.event_result()
-
+
# Find download link
download_link_index = None
for idx, element in state_result.dom_state.selector_map.items():
if element.attributes and element.attributes.get('id') == 'downloadLink':
download_link_index = idx
break
-
- assert download_link_index is not None, "Download link not found"
-
+
+ assert download_link_index is not None, 'Download link not found'
+
# Step 2: Click download link and wait for download
class ClickActionModel(ActionModel):
click_element_by_index: ClickElementAction | None = None
-
+
# Click the download link
result = await controller.act(
- ClickActionModel(click_element_by_index=ClickElementAction(index=download_link_index)),
- browser_session
+ ClickActionModel(click_element_by_index=ClickElementAction(index=download_link_index)), browser_session
)
- assert result.error is None, f"Click on download link failed: {result.error}"
-
+ assert result.error is None, f'Click on download link failed: {result.error}'
+
# Wait for the download event
try:
download_event = await browser_session.event_bus.expect(FileDownloadedEvent, timeout=10.0)
downloaded_file_path = download_event.path
- except asyncio.TimeoutError:
- pytest.fail("Download did not complete within timeout")
-
- assert downloaded_file_path is not None, "Downloaded file path is None"
- assert Path(downloaded_file_path).exists(), f"Downloaded file does not exist: {downloaded_file_path}"
-
+ except TimeoutError:
+ pytest.fail('Download did not complete within timeout')
+
+ assert downloaded_file_path is not None, 'Downloaded file path is None'
+ assert Path(downloaded_file_path).exists(), f'Downloaded file does not exist: {downloaded_file_path}'
+
# Verify download is tracked by browser_session
- assert downloaded_file_path in browser_session.downloaded_files, \
- f"Downloaded file not tracked by browser_session: {downloaded_file_path}"
-
+ assert downloaded_file_path in browser_session.downloaded_files, (
+ f'Downloaded file not tracked by browser_session: {downloaded_file_path}'
+ )
+
# Calculate hash of downloaded file
downloaded_content = Path(downloaded_file_path).read_bytes()
downloaded_hash = hashlib.sha256(downloaded_content).hexdigest()
-
- print(f"βœ… File downloaded: {downloaded_file_path}")
- print(f" Original hash: {download_upload_server.test_hash}")
- print(f" Downloaded hash: {downloaded_hash}")
+
+ print(f'βœ… File downloaded: {downloaded_file_path}')
+ print(f' Original hash: {download_upload_server.test_hash}')
+ print(f' Downloaded hash: {downloaded_hash}')
assert downloaded_hash == download_upload_server.test_hash, "Downloaded file hash doesn't match original"
-
+
# Step 3: Navigate to upload page in a new tab
- print(f"\nπŸ”„ Opening upload page in new tab: {base_url}/upload-page")
-
+ print(f'\nπŸ”„ Opening upload page in new tab: {base_url}/upload-page')
+
# Debug: Check how many tabs we have before navigation
tabs_before = await browser_session.get_tabs()
- print(f"πŸ“‘ Tabs before navigation: {len(tabs_before)} tabs")
+ print(f'πŸ“‘ Tabs before navigation: {len(tabs_before)} tabs')
for i, tab in enumerate(tabs_before):
- print(f" Tab {i}: {tab.url}")
+ print(f' Tab {i}: {tab.url}')
result = await controller.act(
- GoToUrlActionModel(go_to_url=GoToUrlAction(url=f'{base_url}/upload-page', new_tab=True)),
- browser_session
+ GoToUrlActionModel(go_to_url=GoToUrlAction(url=f'{base_url}/upload-page', new_tab=True)), browser_session
)
- assert result.error is None, f"Navigation to upload page failed: {result.error}"
- print(f"βœ… Navigation result: {result.extracted_content}")
-
+ assert result.error is None, f'Navigation to upload page failed: {result.error}'
+ print(f'βœ… Navigation result: {result.extracted_content}')
+
# The new tab should be automatically focused after opening
await asyncio.sleep(2.0) # Give more time for the new tab to load and focus
-
+
# Debug: Get all tabs
tabs = await browser_session.get_tabs()
- print(f"\nπŸ“‘ All tabs after opening upload page:")
+ print('\nπŸ“‘ All tabs after opening upload page:')
for i, tab in enumerate(tabs):
- print(f" Tab {i}: {tab.url} - {tab.title}")
-
+ print(f' Tab {i}: {tab.url} - {tab.title}')
+
# Get browser state to find file input
event = browser_session.event_bus.dispatch(BrowserStateRequestEvent())
state_result = await event.event_result()
-
+
# Debug: print page URL and title
- print(f"\nπŸ” Getting DOM state:")
- print(f" Current page URL: {state_result.url}")
- print(f" Current page title: {state_result.title}")
-
+ print('\nπŸ” Getting DOM state:')
+ print(f' Current page URL: {state_result.url}')
+ print(f' Current page title: {state_result.title}')
+
# Find file input
file_input_index = None
input_elements = []
@@ -265,53 +257,54 @@ class TestDownloadUploadFullCircle:
if element.attributes and element.attributes.get('type') == 'file':
file_input_index = idx
break
-
- print(f"Found {len(input_elements)} input elements: {input_elements}")
- assert file_input_index is not None, "File input not found"
-
+
+ print(f'Found {len(input_elements)} input elements: {input_elements}')
+ assert file_input_index is not None, 'File input not found'
+
# Step 4: Upload the downloaded file
class UploadActionModel(ActionModel):
upload_file_to_element: UploadFileAction | None = None
-
+
# The downloaded file should be automatically available for upload
result = await controller.act(
- UploadActionModel(upload_file_to_element=UploadFileAction(
- index=file_input_index,
- path=downloaded_file_path
- )),
+ UploadActionModel(upload_file_to_element=UploadFileAction(index=file_input_index, path=downloaded_file_path)),
browser_session,
available_file_paths=[], # Empty, but file is in downloaded_files
- file_system=file_system
+ file_system=file_system,
)
- assert result.error is None, f"File upload failed: {result.error}"
-
+ assert result.error is None, f'File upload failed: {result.error}'
+
# Step 4b: Click the submit button to trigger the form submission
# Get browser state to find submit button
event = browser_session.event_bus.dispatch(BrowserStateRequestEvent())
state_result = await event.event_result()
-
+
# Find submit button
submit_button_index = None
for idx, element in state_result.dom_state.selector_map.items():
- if element.tag_name and element.tag_name.lower() == 'button' and element.attributes and element.attributes.get('id') == 'submitButton':
+ if (
+ element.tag_name
+ and element.tag_name.lower() == 'button'
+ and element.attributes
+ and element.attributes.get('id') == 'submitButton'
+ ):
submit_button_index = idx
break
-
- assert submit_button_index is not None, "Submit button not found"
-
+
+ assert submit_button_index is not None, 'Submit button not found'
+
# Click the submit button
result = await controller.act(
- ClickActionModel(click_element_by_index=ClickElementAction(index=submit_button_index)),
- browser_session
+ ClickActionModel(click_element_by_index=ClickElementAction(index=submit_button_index)), browser_session
)
- assert result.error is None, f"Click on submit button failed: {result.error}"
-
+ assert result.error is None, f'Click on submit button failed: {result.error}'
+
# Wait for JavaScript to process the upload
await asyncio.sleep(1.0)
-
+
# Step 5: Verify upload via JavaScript (client-side hash)
cdp_session = await browser_session.get_or_create_cdp_session()
-
+
# Get uploaded file details from the page
upload_verification = await browser_session.cdp_client.send.Runtime.evaluate(
params={
@@ -329,32 +322,36 @@ class TestDownloadUploadFullCircle:
};
})()
""",
- 'returnByValue': True
+ 'returnByValue': True,
},
- session_id=cdp_session.session_id
+ session_id=cdp_session.session_id,
)
-
+
upload_info = upload_verification.get('result', {}).get('value', {})
-
+
# Verify upload was successful
- assert upload_info.get('hasResult') is True, "Upload result not displayed"
- assert upload_info.get('fileName') == 'test-file.txt', f"Uploaded filename mismatch: {upload_info.get('fileName')}"
- assert upload_info.get('fileHash') == download_upload_server.test_hash, \
- f"Uploaded file hash mismatch. Expected: {download_upload_server.test_hash}, Got: {upload_info.get('fileHash')}"
-
- print(f"βœ… File uploaded successfully!")
- print(f" Uploaded filename: {upload_info.get('fileName')}")
- print(f" Uploaded hash: {upload_info.get('fileHash')}")
- print(f" Hash matches original: {upload_info.get('fileHash') == download_upload_server.test_hash}")
-
+ assert upload_info.get('hasResult') is True, 'Upload result not displayed'
+ assert upload_info.get('fileName') == 'test-file.txt', (
+ f'Uploaded filename mismatch: {upload_info.get("fileName")}'
+ )
+ assert upload_info.get('fileHash') == download_upload_server.test_hash, (
+ f'Uploaded file hash mismatch. Expected: {download_upload_server.test_hash}, Got: {upload_info.get("fileHash")}'
+ )
+
+ print('βœ… File uploaded successfully!')
+ print(f' Uploaded filename: {upload_info.get("fileName")}')
+ print(f' Uploaded hash: {upload_info.get("fileHash")}')
+ print(f' Hash matches original: {upload_info.get("fileHash") == download_upload_server.test_hash}')
+
# Step 6: Verify server-side upload (if needed)
if download_upload_server.uploaded_files:
server_file = download_upload_server.uploaded_files[0]
- assert server_file['hash'] == download_upload_server.test_hash, \
- f"Server-side hash mismatch. Expected: {download_upload_server.test_hash}, Got: {server_file['hash']}"
- print(f"βœ… Server-side verification passed!")
-
- print("\nπŸŽ‰ Full circle test passed: Download β†’ Upload with hash verification!")
-
+ assert server_file['hash'] == download_upload_server.test_hash, (
+ f'Server-side hash mismatch. Expected: {download_upload_server.test_hash}, Got: {server_file["hash"]}'
+ )
+ print('βœ… Server-side verification passed!')
+
+ print('\nπŸŽ‰ Full circle test passed: Download β†’ Upload with hash verification!')
+
finally:
- await browser_session.stop()
\ No newline at end of file
+ await browser_session.stop()
diff --git a/tests/ci/test_multi_websocket.py b/tests/ci/test_multi_websocket.py
index fe733c390..8730697f9 100644
--- a/tests/ci/test_multi_websocket.py
+++ b/tests/ci/test_multi_websocket.py
@@ -1,6 +1,5 @@
"""Test multi-WebSocket CDP connections."""
-import asyncio
import logging
import pytest
@@ -28,7 +27,7 @@ def test_html(httpserver: HTTPServer):
</body>
</html>
"""
-
+
# Second page HTML
page2_html = """
<!DOCTYPE html>
@@ -40,38 +39,38 @@ def test_html(httpserver: HTTPServer):
</body>
</html>
"""
-
- httpserver.expect_request("/").respond_with_data(main_html, content_type="text/html")
- httpserver.expect_request("/page2.html").respond_with_data(page2_html, content_type="text/html")
-
- return httpserver.url_for("/")
+
+ httpserver.expect_request('/').respond_with_data(main_html, content_type='text/html')
+ httpserver.expect_request('/page2.html').respond_with_data(page2_html, content_type='text/html')
+
+ return httpserver.url_for('/')
@pytest.mark.asyncio
async def test_shared_websocket_default(test_html):
"""Test that default behavior uses shared WebSocket."""
browser_session = BrowserSession()
-
+
try:
await browser_session.start()
-
+
# Navigate to test page
await browser_session._cdp_navigate(test_html)
-
+
# Create new tab
- target_id = await browser_session._cdp_create_new_page(test_html + "page2.html")
-
+ target_id = await browser_session._cdp_create_new_page(test_html + 'page2.html')
+
# Get sessions for both targets (explicitly request shared WebSocket)
session1 = browser_session.agent_focus
session2 = await browser_session.get_or_create_cdp_session(target_id, focus=False, new_socket=False)
-
+
# Both should use the same CDP client
assert session1.cdp_client is session2.cdp_client
assert not session1.owns_cdp_client
assert not session2.owns_cdp_client
-
- logger.info("βœ… Default behavior: Both sessions share the same WebSocket connection")
-
+
+ logger.info('βœ… Default behavior: Both sessions share the same WebSocket connection')
+
finally:
await browser_session.kill()
@@ -80,48 +79,42 @@ async def test_shared_websocket_default(test_html):
async def test_dedicated_websocket_per_target(test_html):
"""Test that new_socket=True creates separate WebSocket connections."""
browser_session = BrowserSession(headless=True, user_data_dir=None)
-
+
try:
await browser_session.start()
-
+
# Navigate to test page
await browser_session._cdp_navigate(test_html)
-
+
# Create new tab
- target_id = await browser_session._cdp_create_new_page(test_html + "page2.html")
-
+ target_id = await browser_session._cdp_create_new_page(test_html + 'page2.html')
+
# Get session for first target (uses shared WebSocket)
session1 = browser_session.agent_focus
-
+
# Get session for second target with dedicated WebSocket
- session2 = await browser_session.get_or_create_cdp_session(
- target_id,
- focus=False,
- new_socket=True
- )
-
+ session2 = await browser_session.get_or_create_cdp_session(target_id, focus=False, new_socket=True)
+
# Sessions should use different CDP clients
assert session1.cdp_client is not session2.cdp_client
assert not session1.owns_cdp_client
assert session2.owns_cdp_client
-
+
# Both sessions should work independently
# Test session1
result1 = await session1.cdp_client.send.Runtime.evaluate(
- params={'expression': 'document.title'},
- session_id=session1.session_id
+ params={'expression': 'document.title'}, session_id=session1.session_id
)
assert result1['result']['value'] == 'Main Page'
-
+
# Test session2
result2 = await session2.cdp_client.send.Runtime.evaluate(
- params={'expression': 'document.title'},
- session_id=session2.session_id
+ params={'expression': 'document.title'}, session_id=session2.session_id
)
assert result2['result']['value'] == 'Page 2'
-
- logger.info("βœ… Multi-WebSocket: Each target has its own WebSocket connection")
-
+
+ logger.info('βœ… Multi-WebSocket: Each target has its own WebSocket connection')
+
finally:
await browser_session.kill()
@@ -130,35 +123,31 @@ async def test_dedicated_websocket_per_target(test_html):
async def test_websocket_cleanup(test_html):
"""Test that WebSocket connections are properly cleaned up."""
browser_session = BrowserSession()
-
+
try:
await browser_session.start()
-
+
# Create multiple tabs with dedicated WebSockets
target_ids = []
for i in range(3):
- target_id = await browser_session._cdp_create_new_page(f"{test_html}#tab{i}")
+ target_id = await browser_session._cdp_create_new_page(f'{test_html}#tab{i}')
target_ids.append(target_id)
-
+
# Create session with dedicated WebSocket
- session = await browser_session.get_or_create_cdp_session(
- target_id,
- focus=False,
- new_socket=True
- )
+ session = await browser_session.get_or_create_cdp_session(target_id, focus=False, new_socket=True)
assert session.owns_cdp_client
-
+
# Should have 3 sessions in the pool (plus the initial one)
assert len(browser_session._cdp_session_pool) >= 3
-
+
# Reset should clean up all WebSocket connections
await browser_session.reset()
-
+
# Pool should be empty
assert len(browser_session._cdp_session_pool) == 0
-
- logger.info("βœ… Cleanup: All WebSocket connections properly disconnected")
-
+
+ logger.info('βœ… Cleanup: All WebSocket connections properly disconnected')
+
finally:
await browser_session.kill()
@@ -167,49 +156,41 @@ async def test_websocket_cleanup(test_html):
async def test_mixed_websocket_sessions(test_html):
"""Test mixing shared and dedicated WebSocket sessions."""
browser_session = BrowserSession()
-
+
try:
await browser_session.start()
-
+
# Create multiple tabs
target_ids = []
for i in range(4):
- target_id = await browser_session._cdp_create_new_page(f"{test_html}#tab{i}")
+ target_id = await browser_session._cdp_create_new_page(f'{test_html}#tab{i}')
target_ids.append(target_id)
-
+
# Create sessions with mixed WebSocket types
- session_shared1 = await browser_session.get_or_create_cdp_session(
- target_ids[0], focus=False, new_socket=False
- )
- session_dedicated1 = await browser_session.get_or_create_cdp_session(
- target_ids[1], focus=False, new_socket=True
- )
- session_shared2 = await browser_session.get_or_create_cdp_session(
- target_ids[2], focus=False, new_socket=False
- )
- session_dedicated2 = await browser_session.get_or_create_cdp_session(
- target_ids[3], focus=False, new_socket=True
- )
-
+ session_shared1 = await browser_session.get_or_create_cdp_session(target_ids[0], focus=False, new_socket=False)
+ session_dedicated1 = await browser_session.get_or_create_cdp_session(target_ids[1], focus=False, new_socket=True)
+ session_shared2 = await browser_session.get_or_create_cdp_session(target_ids[2], focus=False, new_socket=False)
+ session_dedicated2 = await browser_session.get_or_create_cdp_session(target_ids[3], focus=False, new_socket=True)
+
# Verify ownership
assert not session_shared1.owns_cdp_client
assert session_dedicated1.owns_cdp_client
assert not session_shared2.owns_cdp_client
assert session_dedicated2.owns_cdp_client
-
+
# Shared sessions should use the same client
assert session_shared1.cdp_client is session_shared2.cdp_client
-
+
# Dedicated sessions should use different clients
assert session_dedicated1.cdp_client is not session_dedicated2.cdp_client
assert session_dedicated1.cdp_client is not session_shared1.cdp_client
-
- logger.info("βœ… Mixed mode: Shared and dedicated WebSockets work together")
-
+
+ logger.info('βœ… Mixed mode: Shared and dedicated WebSockets work together')
+
finally:
await browser_session.kill()
-if __name__ == "__main__":
+if __name__ == '__main__':
# Run with: python -m pytest tests/ci/test_multi_websocket.py -v
- pytest.main([__file__, "-v", "-s"])
+ pytest.main([__file__, '-v', '-s'])
diff --git a/tests/ci/test_search_google_tab_focus.py b/tests/ci/test_search_google_tab_focus.py
index 6ea1db1b0..c5765ac66 100644
--- a/tests/ci/test_search_google_tab_focus.py
+++ b/tests/ci/test_search_google_tab_focus.py
@@ -3,7 +3,6 @@
import asyncio
import pytest
-from bubus import EventBus
from browser_use.browser import BrowserSession
from browser_use.browser.events import BrowserStateRequestEvent, NavigateToUrlEvent
@@ -23,125 +22,109 @@ async def test_search_google_creates_and_focuses_new_tab(browser_session: Browse
"""Test that search_google creates a new tab and properly switches focus to it."""
# Create controller to get the search_google action
controller = Controller()
-
+
# Get initial browser state
- initial_state_event = browser_session.event_bus.dispatch(
- BrowserStateRequestEvent(include_screenshot=False)
- )
+ initial_state_event = browser_session.event_bus.dispatch(BrowserStateRequestEvent(include_screenshot=False))
initial_state = await initial_state_event
initial_url = initial_state.url
initial_tabs_count = len(initial_state.tabs)
-
+
# Execute search_google action
action_result = await controller.registry.execute_action(
action_name='search_google',
params={'query': 'test search'},
browser_session=browser_session,
)
-
+
# Small delay to ensure navigation completes
await asyncio.sleep(1)
-
+
# Get browser state after search
- state_event = browser_session.event_bus.dispatch(
- BrowserStateRequestEvent(include_screenshot=False)
- )
+ state_event = browser_session.event_bus.dispatch(BrowserStateRequestEvent(include_screenshot=False))
state_after = await state_event
-
+
# Verify a new tab was created
- assert len(state_after.tabs) == initial_tabs_count + 1, f"Expected {initial_tabs_count + 1} tabs, got {len(state_after.tabs)}"
-
+ assert len(state_after.tabs) == initial_tabs_count + 1, f'Expected {initial_tabs_count + 1} tabs, got {len(state_after.tabs)}'
+
# Verify the current URL is Google search, not about:blank
- assert 'google.com/search' in state_after.url, f"Expected Google search URL, got {state_after.url}"
+ assert 'google.com/search' in state_after.url, f'Expected Google search URL, got {state_after.url}'
assert state_after.url != initial_url, f"URL didn't change from {initial_url}"
- assert 'about:blank' not in state_after.url, "Agent is still on about:blank after search_google"
-
+ assert 'about:blank' not in state_after.url, 'Agent is still on about:blank after search_google'
+
# Verify the search query is in the URL
- assert 'test+search' in state_after.url or 'test%20search' in state_after.url, f"Query not found in URL: {state_after.url}"
-
- print(f"βœ… Test passed! Agent correctly focused on Google tab: {state_after.url}")
+ assert 'test+search' in state_after.url or 'test%20search' in state_after.url, f'Query not found in URL: {state_after.url}'
+
+ print(f'βœ… Test passed! Agent correctly focused on Google tab: {state_after.url}')
async def test_navigate_with_new_tab_focuses_properly(browser_session: BrowserSession):
"""Test that NavigateToUrlEvent with new_tab=True properly switches focus."""
# Get initial state
- initial_state_event = browser_session.event_bus.dispatch(
- BrowserStateRequestEvent(include_screenshot=False)
- )
+ initial_state_event = browser_session.event_bus.dispatch(BrowserStateRequestEvent(include_screenshot=False))
initial_state = await initial_state_event
initial_tabs_count = len(initial_state.tabs)
-
+
# Navigate to a URL in a new tab
- nav_event = browser_session.event_bus.dispatch(
- NavigateToUrlEvent(url='https://example.com', new_tab=True)
- )
+ nav_event = browser_session.event_bus.dispatch(NavigateToUrlEvent(url='https://example.com', new_tab=True))
await nav_event
-
+
# Small delay to ensure navigation completes
await asyncio.sleep(1)
-
+
# Get browser state after navigation
- state_event = browser_session.event_bus.dispatch(
- BrowserStateRequestEvent(include_screenshot=False)
- )
+ state_event = browser_session.event_bus.dispatch(BrowserStateRequestEvent(include_screenshot=False))
state_after = await state_event
-
+
# Verify a new tab was created
- assert len(state_after.tabs) == initial_tabs_count + 1, f"Expected {initial_tabs_count + 1} tabs, got {len(state_after.tabs)}"
-
+ assert len(state_after.tabs) == initial_tabs_count + 1, f'Expected {initial_tabs_count + 1} tabs, got {len(state_after.tabs)}'
+
# Verify the current URL is the navigated URL
- assert 'example.com' in state_after.url, f"Expected example.com URL, got {state_after.url}"
- assert 'about:blank' not in state_after.url, "Agent is still on about:blank after new tab navigation"
-
- print(f"βœ… Test passed! Agent correctly focused on new tab: {state_after.url}")
+ assert 'example.com' in state_after.url, f'Expected example.com URL, got {state_after.url}'
+ assert 'about:blank' not in state_after.url, 'Agent is still on about:blank after new tab navigation'
+
+ print(f'βœ… Test passed! Agent correctly focused on new tab: {state_after.url}')
async def test_multiple_new_tabs_focus_on_latest(browser_session: BrowserSession):
"""Test that creating multiple new tabs focuses on the most recent one."""
# Navigate to first new tab
- nav1_event = browser_session.event_bus.dispatch(
- NavigateToUrlEvent(url='https://example.com', new_tab=True)
- )
+ nav1_event = browser_session.event_bus.dispatch(NavigateToUrlEvent(url='https://example.com', new_tab=True))
await nav1_event
await asyncio.sleep(0.5)
-
+
# Navigate to second new tab
- nav2_event = browser_session.event_bus.dispatch(
- NavigateToUrlEvent(url='https://github.com', new_tab=True)
- )
+ nav2_event = browser_session.event_bus.dispatch(NavigateToUrlEvent(url='https://github.com', new_tab=True))
await nav2_event
await asyncio.sleep(0.5)
-
+
# Get browser state
- state_event = browser_session.event_bus.dispatch(
- BrowserStateRequestEvent(include_screenshot=False)
- )
+ state_event = browser_session.event_bus.dispatch(BrowserStateRequestEvent(include_screenshot=False))
state = await state_event
-
+
# Should be focused on the most recent tab (github.com)
- assert 'github.com' in state.url, f"Expected github.com URL, got {state.url}"
- assert len(state.tabs) >= 3, f"Expected at least 3 tabs, got {len(state.tabs)}"
-
- print(f"βœ… Test passed! Agent correctly focused on latest tab: {state.url}")
+ assert 'github.com' in state.url, f'Expected github.com URL, got {state.url}'
+ assert len(state.tabs) >= 3, f'Expected at least 3 tabs, got {len(state.tabs)}'
+ print(f'βœ… Test passed! Agent correctly focused on latest tab: {state.url}')
-if __name__ == "__main__":
+
+if __name__ == '__main__':
# Run tests directly
async def run_all_tests():
session = BrowserSession()
await session.start()
try:
- print("Running test_search_google_creates_and_focuses_new_tab...")
+ print('Running test_search_google_creates_and_focuses_new_tab...')
await test_search_google_creates_and_focuses_new_tab(session)
-
- print("\nRunning test_navigate_with_new_tab_focuses_properly...")
+
+ print('\nRunning test_navigate_with_new_tab_focuses_properly...')
await test_navigate_with_new_tab_focuses_properly(session)
-
- print("\nRunning test_multiple_new_tabs_focus_on_latest...")
+
+ print('\nRunning test_multiple_new_tabs_focus_on_latest...')
await test_multiple_new_tabs_focus_on_latest(session)
-
- print("\nβœ… All tests passed!")
+
+ print('\nβœ… All tests passed!')
finally:
await session.close()
-
- asyncio.run(run_all_tests())
\ No newline at end of file
+
+ asyncio.run(run_all_tests())