Skip to content

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

clean up logging setup and tests

Nick Sweeting66b21d634d62
diff --git a/browser_use/browser/session.py b/browser_use/browser/session.py
index 3c4b32db2..13098cbe7 100644
--- a/browser_use/browser/session.py
+++ b/browser_use/browser/session.py
@@ -2,11 +2,9 @@
import asyncio
import logging
-import os
from pathlib import Path
from typing import Any, Self, cast
-import cdp_use.client
import httpx
from bubus import EventBus
from cdp_use import CDPClient
@@ -16,7 +14,6 @@ from uuid_extensions import uuid7str
# CDP logging is now handled by setup_logging() in logging_config.py
# It automatically sets CDP logs to the same level as browser_use logs
-
from browser_use.browser.events import (
AgentFocusChangedEvent,
BrowserConnectedEvent,
@@ -46,7 +43,7 @@ MAX_SCREENSHOT_WIDTH = 1920
class CDPSession(BaseModel):
"""Info about a single CDP session bound to a specific target.
-
+
Can optionally use its own WebSocket connection for better isolation.
"""
@@ -58,14 +55,14 @@ class CDPSession(BaseModel):
session_id: str
title: str = 'Unknown title'
url: str = 'about:blank'
-
+
# Track if this session owns its CDP client (for cleanup)
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):
"""Create a CDP session for a target.
-
+
Args:
cdp_client: Existing CDP client to use (or just for reference if creating own)
target_id: Target ID to attach to
@@ -77,12 +74,13 @@ class CDPSession(BaseModel):
raise ValueError('cdp_url required when new_socket=True')
# Create a new CDP client with its own WebSocket connection
import logging
+
logger = logging.getLogger(f'browser_use.CDPSession.{target_id[-4:]}')
logger.info(f'🔌 Creating dedicated WebSocket connection for target {target_id}')
-
+
target_cdp_client = CDPClient(cdp_url)
await target_cdp_client.start()
-
+
cdp_session = cls(
cdp_client=target_cdp_client,
target_id=target_id,
@@ -128,7 +126,7 @@ class CDPSession(BaseModel):
self.title = target_info['title']
self.url = target_info['url']
return self
-
+
async def disconnect(self) -> None:
"""Disconnect and cleanup if this session owns its CDP client."""
if self.owns_cdp_client and self.cdp_client:
@@ -225,7 +223,7 @@ class BrowserSession(BaseModel):
# TODO: clear the event bus queue here, implement this helper
# await self.event_bus.wait_for_idle(timeout=5.0)
# await self.event_bus.clear()
-
+
# Disconnect sessions that own their WebSocket connections
for session in self._cdp_session_pool.values():
if hasattr(session, 'disconnect'):
@@ -285,10 +283,10 @@ class BrowserSession(BaseModel):
await self.reset()
# Create fresh event bus
self.event_bus = EventBus()
-
+
async def stop(self) -> None:
"""Stop the browser session without killing the browser process.
-
+
This clears event buses and cached state but keeps the browser alive.
Useful when you want to clean up resources but plan to reconnect later.
"""
@@ -351,30 +349,30 @@ class BrowserSession(BaseModel):
)
)
raise
-
+
async def on_NavigateToUrlEvent(self, event: NavigateToUrlEvent) -> None:
"""Handle navigation requests - core browser functionality."""
if not self.agent_focus:
self.logger.warning('Cannot navigate - browser not connected')
return
-
+
try:
# Find or create target for navigation
target_id = None
tab_index = 0
-
+
if event.new_tab:
# Look for existing about:blank tab that's not the current one
targets = await self._cdp_get_all_pages()
current_target_id = self.agent_focus.target_id if self.agent_focus else None
-
+
for idx, target in enumerate(targets):
if target.get('url') == 'about:blank' and target['targetId'] != current_target_id:
target_id = target['targetId']
tab_index = idx
self.logger.info(f'Reusing existing about:blank tab at index {tab_index}')
break
-
+
# Create new tab if no reusable one found
if not target_id:
target_id = await self._cdp_create_new_page('about:blank')
@@ -387,100 +385,109 @@ class BrowserSession(BaseModel):
# Use current tab
target_id = self.agent_focus.target_id
tab_index = await self.get_tab_index(target_id)
-
+
# Activate target (bring to foreground)
await self.cdp_client.send.Target.activateTarget(params={'targetId': target_id})
-
+
# Update agent focus to the target
self.agent_focus = await self.get_or_create_cdp_session(target_id)
-
+
# Clear any cached state since we're navigating
if self._dom_watchdog:
self._dom_watchdog.clear_cache()
-
+
# Dispatch focus changed event
- self.event_bus.dispatch(AgentFocusChangedEvent(
- tab_index=tab_index,
- url=event.url,
- ))
-
+ self.event_bus.dispatch(
+ AgentFocusChangedEvent(
+ tab_index=tab_index,
+ url=event.url,
+ )
+ )
+
# Dispatch navigation started
- self.event_bus.dispatch(NavigationStartedEvent(
- tab_index=tab_index,
- url=event.url,
- ))
-
+ self.event_bus.dispatch(
+ NavigationStartedEvent(
+ tab_index=tab_index,
+ url=event.url,
+ )
+ )
+
# Navigate to URL
await self.agent_focus.cdp_client.send.Page.navigate(
- params={'url': event.url},
- session_id=self.agent_focus.session_id
+ params={'url': event.url}, session_id=self.agent_focus.session_id
)
-
+
# Wait a bit to ensure page starts loading
await asyncio.sleep(0.5)
-
+
# Dispatch navigation complete
- self.event_bus.dispatch(NavigationCompleteEvent(
- tab_index=tab_index,
- url=event.url,
- status=None, # CDP doesn't provide status directly
- ))
-
+ self.event_bus.dispatch(
+ NavigationCompleteEvent(
+ tab_index=tab_index,
+ url=event.url,
+ status=None, # CDP doesn't provide status directly
+ )
+ )
+
# Note: These should be handled by dedicated watchdogs:
# - Security checks (security_watchdog)
# - Page health checks (crash_watchdog)
# - Dialog handling (dialog_watchdog)
# - Download handling (downloads_watchdog)
# - DOM rebuilding (dom_watchdog)
-
+
except Exception as e:
self.logger.error(f'Navigation failed: {e}')
- self.event_bus.dispatch(NavigationCompleteEvent(
- tab_index=tab_index if 'tab_index' in locals() else 0,
- url=event.url,
- status=None,
- error_message=str(e),
- ))
-
+ self.event_bus.dispatch(
+ NavigationCompleteEvent(
+ tab_index=tab_index if 'tab_index' in locals() else 0,
+ url=event.url,
+ status=None,
+ error_message=str(e),
+ )
+ )
+
async def on_SwitchTabEvent(self, event: SwitchTabEvent) -> None:
"""Handle tab switching - core browser functionality."""
if not self.agent_focus:
self.logger.warning('Cannot switch tabs - browser not connected')
return
-
+
targets = await self._cdp_get_all_pages()
if 0 <= event.tab_index < len(targets):
target_id = targets[event.tab_index]['targetId']
-
+
# Activate target
await self.cdp_client.send.Target.activateTarget(params={'targetId': target_id})
-
+
# Update agent focus
self.agent_focus = await self.get_or_create_cdp_session(target_id)
-
+
# Clear cached state when switching tabs
if self._dom_watchdog:
self._dom_watchdog.clear_cache()
-
+
# Dispatch focus changed event
target_url = targets[event.tab_index].get('url', '')
- self.event_bus.dispatch(AgentFocusChangedEvent(
- tab_index=event.tab_index,
- url=target_url,
- ))
-
+ self.event_bus.dispatch(
+ AgentFocusChangedEvent(
+ tab_index=event.tab_index,
+ url=target_url,
+ )
+ )
+
self.logger.info(f'Switched to tab {event.tab_index}: {target_url}')
else:
self.logger.warning(f'Invalid tab index: {event.tab_index}')
-
+
async def on_TabClosedEvent(self, event: TabClosedEvent) -> None:
"""Handle tab closure - update focus if needed."""
if not self.agent_focus:
return
-
+
# Get current tab index
current_tab_index = await self.get_tab_index(self.agent_focus.target_id)
-
+
# If the closed tab was the current one, find a new target
if current_tab_index == event.tab_index:
targets = await self._cdp_get_all_pages()
@@ -490,12 +497,14 @@ class BrowserSession(BaseModel):
if new_index >= 0:
new_target_id = targets[new_index]['targetId']
self.agent_focus = await self.get_or_create_cdp_session(new_target_id)
-
+
# Dispatch focus changed
- self.event_bus.dispatch(AgentFocusChangedEvent(
- tab_index=new_index,
- url=targets[new_index].get('url', ''),
- ))
+ self.event_bus.dispatch(
+ AgentFocusChangedEvent(
+ tab_index=new_index,
+ url=targets[new_index].get('url', ''),
+ )
+ )
self.logger.info(f'Moved focus to tab {new_index} after tab closure')
else:
self.agent_focus = None
@@ -538,7 +547,9 @@ class BrowserSession(BaseModel):
assert self._cdp_client_root is not None, 'CDP client not initialized - browser may not be connected yet'
return self._cdp_client_root
- async def get_or_create_cdp_session(self, target_id: str | None = None, focus: bool = True, new_socket: bool | None = None) -> CDPSession:
+ async def get_or_create_cdp_session(
+ self, target_id: str | None = None, focus: bool = True, new_socket: bool | None = None
+ ) -> CDPSession:
"""Get or create a CDP session for a target.
Args:
@@ -577,12 +588,14 @@ class BrowserSession(BaseModel):
# Create new session for this target
# Default to True for new sessions (each new target gets its own WebSocket)
should_use_new_socket = True if new_socket is None else new_socket
- self.logger.debug(f'[get_or_create_cdp_session] Creating new CDP session for target {target_id} (new_socket={should_use_new_socket})')
+ self.logger.debug(
+ f'[get_or_create_cdp_session] Creating new CDP session for target {target_id} (new_socket={should_use_new_socket})'
+ )
session = await CDPSession.for_target(
- self._cdp_client_root,
+ self._cdp_client_root,
target_id,
new_socket=should_use_new_socket,
- cdp_url=self.cdp_url if should_use_new_socket else None
+ cdp_url=self.cdp_url if should_use_new_socket else None,
)
self._cdp_session_pool[target_id] = session
@@ -610,7 +623,11 @@ class BrowserSession(BaseModel):
# ========== Helper Methods ==========
async def get_browser_state_summary(
- self, cache_clickable_elements_hashes: bool = True, include_screenshot: bool = True, cached: bool = False, include_recent_events: bool = False
+ self,
+ cache_clickable_elements_hashes: bool = True,
+ include_screenshot: bool = True,
+ cached: bool = False,
+ include_recent_events: bool = False,
) -> BrowserStateSummary:
if cached and self._cached_browser_state_summary is not None and self._cached_browser_state_summary.dom_state:
# Don't use cached state if it has 0 interactive elements
@@ -656,8 +673,8 @@ class BrowserSession(BaseModel):
from browser_use.browser.dom_watchdog import DOMWatchdog
from browser_use.browser.downloads_watchdog import DownloadsWatchdog
from browser_use.browser.local_browser_watchdog import LocalBrowserWatchdog
- from browser_use.browser.security_watchdog import SecurityWatchdog
from browser_use.browser.screenshot_watchdog import ScreenshotWatchdog
+ from browser_use.browser.security_watchdog import SecurityWatchdog
from browser_use.browser.storage_state_watchdog import StorageStateWatchdog
# Initialize CrashWatchdog
@@ -846,14 +863,14 @@ class BrowserSession(BaseModel):
except Exception as e:
self.logger.warning(f'Failed to create CDP session: {e}')
raise
-
+
# Dispatch TabCreatedEvent for all initial tabs (so watchdogs can initialize)
# This replaces the duplicated logic from navigation_watchdog's _initialize_agent_focus
for idx, target in enumerate(page_targets):
target_url = target.get('url', '')
self.logger.debug(f'Dispatching TabCreatedEvent for initial tab {idx}: {target_url}')
self.event_bus.dispatch(TabCreatedEvent(tab_index=idx, url=target_url))
-
+
# Dispatch initial focus event
if page_targets:
initial_url = page_targets[0].get('url', '')
@@ -875,7 +892,7 @@ class BrowserSession(BaseModel):
async def get_tabs(self) -> list[TabInfo]:
"""Get information about all open tabs using CDP Target.getTargetInfo for speed."""
tabs = []
-
+
# Safety check - return empty list if browser not connected yet
if not self._cdp_client_root:
return tabs
@@ -1274,6 +1291,7 @@ class BrowserSession(BaseModel):
# Always allow new tab pages (chrome://new-tab-page/, chrome://newtab/, about:blank)
# so they can be redirected to about:blank in connect()
from browser_use.utils import is_new_tab_page
+
if is_new_tab_page(url):
url_allowed = True
@@ -1334,7 +1352,7 @@ class BrowserSession(BaseModel):
# First pass: collect frame trees from ALL targets
for target in all_targets:
target_id = target['targetId']
-
+
# Skip iframe targets if cross-origin support is disabled
if not include_cross_origin and target.get('type') == 'iframe':
continue
@@ -1349,7 +1367,7 @@ class BrowserSession(BaseModel):
else:
# Get cached session for this target (don't change focus - iterating frames)
cdp_session = await self.get_or_create_cdp_session(target_id, focus=False)
-
+
if cdp_session:
target_sessions[target_id] = cdp_session.session_id
@@ -1397,7 +1415,7 @@ class BrowserSession(BaseModel):
# For iframe targets, the frame itself is likely cross-origin
if target.get('type') == 'iframe':
frame_info['isCrossOrigin'] = True
-
+
# Skip cross-origin frames if support is disabled
if not include_cross_origin and frame_info.get('isCrossOrigin'):
return # Skip this frame and its children
@@ -1518,7 +1536,7 @@ class BrowserSession(BaseModel):
# If cross-origin iframes are disabled, just use the main session
if not self.browser_profile.cross_origin_iframes:
return await self.get_or_create_cdp_session()
-
+
# Get complete frame hierarchy
all_frames, target_sessions = await self.get_all_frames()
diff --git a/browser_use/cli.py b/browser_use/cli.py
index 9463c3715..2720bddba 100644
--- a/browser_use/cli.py
+++ b/browser_use/cli.py
@@ -453,7 +453,7 @@ class BrowserUseApp(App):
browser_use_logger.propagate = False # Don't propagate to root logger
browser_use_logger.handlers = [log_handler] # Replace any existing handlers
browser_use_logger.setLevel(root.level)
-
+
# Also ensure agent loggers go to the main output
for logger_name in ['browser_use.Agent', 'browser_use.controller']:
agent_logger = logging.getLogger(logger_name)
@@ -655,7 +655,7 @@ class BrowserUseApp(App):
"""Setup listener for browser session event bus."""
if not self.browser_session or not self.browser_session.event_bus:
return
-
+
# Clean up any existing handler before registering a new one
if self._event_bus_handler_func is not None:
try:
@@ -671,7 +671,7 @@ class BrowserUseApp(App):
logging.debug(f'Error cleaning up event bus handler: {e}')
self._event_bus_handler_func = None
self._event_bus_handler_id = None
-
+
try:
# Get the events log widget
events_log = self.query_one('#events-log', RichLog)
@@ -706,7 +706,7 @@ class BrowserUseApp(App):
# Store the handler function before registering it
self._event_bus_handler_func = log_event
self._event_bus_handler_id = id(log_event)
-
+
# Register wildcard handler for all events
self.browser_session.event_bus.on('*', log_event)
logging.debug(f'Registered new event bus handler with id: {self._event_bus_handler_id}')
@@ -715,7 +715,7 @@ class BrowserUseApp(App):
"""Setup CDP message logger to capture already-transformed CDP logs."""
# No need to configure levels - setup_logging() already handles that
# We just need to capture the transformed logs and route them to the CDP pane
-
+
# Get the CDP log widget
cdp_log = self.query_one('#cdp-log', RichLog)
@@ -1084,15 +1084,15 @@ async def run_prompt_mode(prompt: str, ctx: click.Context, debug: bool = False):
finally:
# Ensure telemetry is flushed
telemetry.flush()
-
+
# Give a brief moment for cleanup to complete
await asyncio.sleep(0.1)
-
+
# Cancel any remaining tasks to ensure clean exit
tasks = [t for t in asyncio.all_tasks() if t != asyncio.current_task()]
for task in tasks:
task.cancel()
-
+
# Wait for all tasks to be cancelled
if tasks:
await asyncio.gather(*tasks, return_exceptions=True)
@@ -1175,7 +1175,7 @@ async def textual_interface(config: dict[str, Any]):
app.browser_session = browser_session
app.controller = controller
app.llm = llm
-
+
# Set up event bus listener now that browser session is available
# Note: This needs to be called before run_async() but after browser_session is set
# We'll defer this to on_mount() since it needs the widgets to be available
diff --git a/browser_use/dom/debug/highlights.py b/browser_use/dom/debug/highlights.py
index ef4f43055..b904e5fbb 100644
--- a/browser_use/dom/debug/highlights.py
+++ b/browser_use/dom/debug/highlights.py
@@ -103,9 +103,10 @@ async def inject_highlighting_script(dom_service: DomService, interactive_elemen
# ALWAYS remove any existing highlights first to prevent double-highlighting
await remove_highlighting_script(dom_service)
-
+
# Add a small delay to ensure removal completes
import asyncio
+
await asyncio.sleep(0.05)
# Create CSP-safe highlighting script using DOM methods instead of innerHTML
diff --git a/browser_use/logging_config.py b/browser_use/logging_config.py
index d8fdd74fb..5c4b59a57 100644
--- a/browser_use/logging_config.py
+++ b/browser_use/logging_config.py
@@ -124,7 +124,7 @@ def setup_logging(stream=None, log_level=None, force_setup=False):
cdp_loggers = [
'websockets.client', # Controls emission of transformed cdp_use.client logs
'cdp_use',
- 'cdp_use.client',
+ 'cdp_use.client',
'cdp_use.cdp',
'cdp_use.cdp.registry',
]
@@ -136,7 +136,7 @@ def setup_logging(stream=None, log_level=None, force_setup=False):
logger = logging.getLogger('browser_use')
# logger.info('BrowserUse logging setup complete with level %s', log_type)
-
+
# Silence third-party loggers (but not CDP ones which we configured above)
third_party_loggers = [
'WDM',
diff --git a/tests/ci/test_ClickElementEvent.py b/tests/ci/test_ClickElementEvent.py
deleted file mode 100644
index 643fea738..000000000
--- a/tests/ci/test_ClickElementEvent.py
+++ /dev/null
@@ -1,806 +0,0 @@
-import asyncio
-
-import pytest
-from pytest_httpserver import HTTPServer
-
-from browser_use.agent.views import ActionResult
-from browser_use.browser import BrowserSession
-from browser_use.browser.profile import BrowserProfile
-from browser_use.controller.service import Controller
-from browser_use.controller.views import (
- ClickElementAction,
- GoToUrlAction,
-)
-
-
[email protected](scope='session')
-def http_server():
- """Create and provide a test HTTP server that serves static content."""
- server = HTTPServer()
- server.start()
-
- # Add routes for common test pages
- server.expect_request('/').respond_with_data(
- '<html><head><title>Test Home Page</title></head><body><h1>Test Home Page</h1><p>Welcome to the test site</p></body></html>',
- content_type='text/html',
- )
-
- server.expect_request('/page1').respond_with_data(
- '<html><head><title>Test Page 1</title></head><body><h1>Test Page 1</h1><p>This is test page 1</p></body></html>',
- content_type='text/html',
- )
-
- server.expect_request('/page2').respond_with_data(
- '<html><head><title>Test Page 2</title></head><body><h1>Test Page 2</h1><p>This is test page 2</p></body></html>',
- content_type='text/html',
- )
-
- yield server
- server.stop()
-
-
[email protected](scope='session')
-def base_url(http_server):
- """Return the base URL for the test HTTP server."""
- return f'http://{http_server.host}:{http_server.port}'
-
-
[email protected](scope='module')
-async def browser_session():
- """Create and provide a Browser instance with security disabled."""
- browser_session = BrowserSession(
- browser_profile=BrowserProfile(
- headless=True,
- user_data_dir=None,
- keep_alive=True,
- )
- )
- await browser_session.start()
- yield browser_session
- await browser_session.kill()
-
-
[email protected](scope='function')
-def controller():
- """Create and provide a Controller instance."""
- return Controller()
-
-
-class TestClickElementEvent:
- """Test cases for ClickElementEvent and click_element_by_index action."""
-
- async def test_error_handling(self, controller, browser_session):
- """Test error handling when an action fails."""
- # Create an action with an invalid index
- invalid_action = {'click_element_by_index': ClickElementAction(index=999)} # doesn't exist on page
-
- from browser_use.agent.views import ActionModel
-
- class ClickActionModel(ActionModel):
- click_element_by_index: ClickElementAction | None = None
-
- # This should fail since the element doesn't exist
- result: ActionResult = await controller.act(ClickActionModel(**invalid_action), browser_session)
-
- assert result.error is not None
-
- async def test_click_element_by_index(self, controller, browser_session, base_url, http_server):
- """Test that click_element_by_index correctly clicks an element and handles different outcomes."""
- # Add route for clickable elements test page
- http_server.expect_request('/clickable').respond_with_data(
- """
- <!DOCTYPE html>
- <html>
- <head>
- <title>Click Test</title>
- <style>
- .clickable {
- margin: 10px;
- padding: 10px;
- border: 1px solid #ccc;
- cursor: pointer;
- }
- #result {
- margin-top: 20px;
- padding: 10px;
- border: 1px solid #ddd;
- min-height: 20px;
- }
- </style>
- </head>
- <body>
- <h1>Click Test</h1>
- <div class="clickable" id="button1" onclick="updateResult('Button 1 clicked')">Button 1</div>
- <div class="clickable" id="button2" onclick="updateResult('Button 2 clicked')">Button 2</div>
- <a href="#" class="clickable" id="link1" onclick="updateResult('Link 1 clicked'); return false;">Link 1</a>
- <div id="result"></div>
-
- <script>
- function updateResult(text) {
- document.getElementById('result').textContent = text;
- }
- </script>
- </body>
- </html>
- """,
- content_type='text/html',
- )
-
- # Navigate to the clickable elements test page
- goto_action = {'go_to_url': GoToUrlAction(url=f'{base_url}/clickable', new_tab=False)}
-
- from browser_use.agent.views import ActionModel
-
- class GoToUrlActionModel(ActionModel):
- go_to_url: GoToUrlAction | None = None
-
- await controller.act(GoToUrlActionModel(**goto_action), browser_session)
-
- # Wait for the page to load
- await asyncio.sleep(0.5) # Give page time to load
-
- # Initialize the DOM state to populate the selector map
- await browser_session.get_browser_state_summary(cache_clickable_elements_hashes=True)
-
- # Get the selector map
- selector_map = await browser_session.get_selector_map()
-
- # Find a clickable element in the selector map
- button_index = None
- button_text = None
-
- for idx, element in selector_map.items():
- # Look for the first div with class "clickable"
- if element.tag_name.lower() == 'div' and 'clickable' in str(element.attributes.get('class', '')):
- button_index = idx
- button_text = element.get_all_children_text(max_depth=2).strip()
- break
-
- # Verify we found a clickable element
- assert button_index is not None, (
- f'Could not find clickable element in selector map. Available elements: {[f"{idx}: {element.tag_name}" for idx, element in selector_map.items()]}'
- )
-
- # Define expected test data
- expected_button_text = 'Button 1'
- expected_result_text = 'Button 1 clicked'
-
- # Verify the button text matches what we expect
- assert button_text is not None and expected_button_text in button_text, (
- f"Expected button text '{expected_button_text}' not found in '{button_text}'"
- )
-
- # Create a model for the click_element_by_index action
- class ClickElementActionModel(ActionModel):
- click_element_by_index: ClickElementAction | None = None
-
- # Execute the action with the button index
- result = await controller.act(
- ClickElementActionModel(click_element_by_index=ClickElementAction(index=button_index)), browser_session
- )
-
- # Verify the result structure
- assert isinstance(result, ActionResult), 'Result should be an ActionResult instance'
- assert result.error is None, f'Expected no error but got: {result.error}'
-
- # Core logic validation: Verify click was successful
- assert result.extracted_content is not None
- assert f'Clicked element with index {button_index}' in result.extracted_content, (
- f'Expected click confirmation in result content, got: {result.extracted_content}'
- )
- # Note: The click action doesn't include button text in the result, only the index
-
- # Verify the click actually had an effect on the page using CDP
- cdp_session = await browser_session.get_or_create_cdp_session()
- result_js = await browser_session.cdp_client.send.Runtime.evaluate(
- params={'expression': "document.getElementById('result').textContent", 'returnByValue': True},
- session_id=cdp_session.session_id,
- )
- result_text = result_js.get('result', {}).get('value', '')
- assert result_text == expected_result_text, f"Expected result text '{expected_result_text}', got '{result_text}'"
-
- async def test_empty_css_selector_fallback(self, controller, browser_session, httpserver):
- """Test that clicking elements with empty CSS selectors falls back to XPath."""
- # Create a test page with an element that would produce an empty CSS selector
- # This could happen with elements that have no tag name or unusual XPath structures
- httpserver.expect_request('/empty_css_test').respond_with_data(
- """
- <html>
- <head><title>Empty CSS Selector Test</title></head>
- <body>
- <div id="container">
- <!-- Element with minimal attributes that might produce empty CSS selector -->
- <custom-element role="button" tabindex="0" style="cursor: pointer;">Click Me</custom-element>
- <div id="result">Not clicked</div>
- </div>
- <script>
- // Add click handler to the custom element
- document.querySelector('custom-element').addEventListener('click', function() {
- document.getElementById('result').textContent = 'Clicked!';
- });
- </script>
- </body>
- </html>
- """,
- content_type='text/html',
- )
-
- # Navigate to the test page using controller
- goto_action = {'go_to_url': GoToUrlAction(url=httpserver.url_for('/empty_css_test'), new_tab=False)}
-
- from browser_use.agent.views import ActionModel
-
- class GoToUrlActionModel(ActionModel):
- go_to_url: GoToUrlAction | None = None
-
- await controller.act(GoToUrlActionModel(**goto_action), browser_session)
- await asyncio.sleep(0.5) # Give page time to load
-
- # Get the page state which includes clickable elements
- state = await browser_session.get_browser_state_summary(cache_clickable_elements_hashes=False)
-
- # Find the custom element index
- custom_element_index = None
- for index, element in state.dom_state.selector_map.items():
- if element.tag_name == 'custom-element':
- custom_element_index = index
- break
-
- assert custom_element_index is not None, 'Could not find custom-element in selector map'
-
- # Mock a scenario where CSS selector generation returns empty string
- # by temporarily patching the method (we'll test the actual fallback behavior)
- original_method = browser_session._enhanced_css_selector_for_element
- empty_css_called = False
-
- def mock_css_selector(element, include_dynamic_attributes=True):
- nonlocal empty_css_called
- # Return empty string for our custom element to trigger fallback
- if element.tag_name == 'custom-element':
- empty_css_called = True
- return ''
- return original_method(element, include_dynamic_attributes)
-
- # Temporarily replace the method
- browser_session._enhanced_css_selector_for_element = mock_css_selector
-
- try:
- # Create click action for the custom element
- click_action = {'click_element_by_index': ClickElementAction(index=custom_element_index)}
-
- from browser_use.agent.views import ActionModel
-
- class ClickActionModel(ActionModel):
- click_element_by_index: ClickElementAction | None = None
-
- # Execute the click - should use XPath fallback
- result = await controller.act(ClickActionModel(**click_action), browser_session)
-
- # Verify the click succeeded
- assert result.error is None, f'Click failed with error: {result.error}'
- # Success field is not set for click actions, only error is set on failure
- assert empty_css_called, 'CSS selector method was not called'
-
- # Verify the element was actually clicked by checking the result using CDP
- cdp_session = await browser_session.get_or_create_cdp_session(browser_session.agent_focus)
- result_js = await browser_session.cdp_client.send.Runtime.evaluate(
- params={'expression': "document.getElementById('result').textContent", 'returnByValue': True},
- session_id=cdp_session.session_id,
- )
- result_text = result_js.get('result', {}).get('value', '')
- assert result_text == 'Clicked!', f'Element was not clicked, result text: {result_text}'
-
- finally:
- # Restore the original method
- browser_session._enhanced_css_selector_for_element = original_method
-
- async def test_click_element_new_tab(self, controller, browser_session, base_url, http_server):
- """Test that click_element_by_index with new_tab=True opens links in new tabs."""
- # Add route for new tab test page
- http_server.expect_request('/newTab').respond_with_data(
- """
- <!DOCTYPE html>
- <html>
- <head>
- <title>New Tab Test</title>
- </head>
- <body>
- <h1>New Tab Test</h1>
- <a href="/page1" id="testLink">Open Page 1</a>
- </body>
- </html>
- """,
- content_type='text/html',
- )
-
- # Navigate to the new tab test page
- goto_action = {'go_to_url': GoToUrlAction(url=f'{base_url}/newTab', new_tab=False)}
-
- from browser_use.agent.views import ActionModel
-
- class GoToUrlActionModel(ActionModel):
- go_to_url: GoToUrlAction | None = None
-
- await controller.act(GoToUrlActionModel(**goto_action), browser_session)
- await asyncio.sleep(1) # Wait for page to load
-
- # Count initial tabs
- initial_tab_count = len(browser_session.tabs)
-
- # Get the link element (assuming it will be at index 0)
- # First get the browser state to see what elements are available
- state = await browser_session.get_browser_state_with_recovery()
-
- # Find the link element in the selector map
- link_index = None
- for index, element in state.dom_state.selector_map.items():
- if hasattr(element, 'tag_name') and element.tag_name == 'a':
- link_index = index
- break
-
- assert link_index is not None, 'Could not find link element'
-
- # Click the link with new_tab=True
- click_action = {'click_element_by_index': ClickElementAction(index=link_index, new_tab=True)}
-
- class ClickActionModel(ActionModel):
- click_element_by_index: ClickElementAction | None = None
-
- result = await controller.act(ClickActionModel(**click_action), browser_session)
- await asyncio.sleep(1) # Wait for new tab to open
-
- # Verify the result
- assert isinstance(result, ActionResult)
- assert result.extracted_content is not None
-
- # Verify that a new tab was opened
- final_tab_count = len(browser_session.tabs)
- assert final_tab_count == initial_tab_count + 1, f'Expected {initial_tab_count + 1} tabs, got {final_tab_count}'
-
- # Verify we switched to the new tab and it has the correct URL
- current_url = await browser_session.get_current_page_url()
- assert f'{base_url}/page1' in current_url
-
- async def test_click_element_normal_vs_new_tab(self, controller, browser_session, base_url, http_server):
- """Test that click_element_by_index behaves differently with new_tab=False vs new_tab=True."""
- # Add route for comparison test page
- http_server.expect_request('/comparison').respond_with_data(
- """
- <!DOCTYPE html>
- <html>
- <head>
- <title>Comparison Test</title>
- </head>
- <body>
- <h1>Comparison Test</h1>
- <a href="/page2" id="normalLink">Normal Link</a>
- <a href="/page1" id="newTabLink">New Tab Link</a>
- </body>
- </html>
- """,
- content_type='text/html',
- )
-
- # Navigate to the comparison test page
- goto_action = {'go_to_url': GoToUrlAction(url=f'{base_url}/comparison', new_tab=False)}
-
- from browser_use.agent.views import ActionModel
-
- class GoToUrlActionModel(ActionModel):
- go_to_url: GoToUrlAction | None = None
-
- await controller.act(GoToUrlActionModel(**goto_action), browser_session)
- await asyncio.sleep(1)
-
- initial_tab_count = len(browser_session.tabs)
-
- # Get browser state and find link elements
- state = await browser_session.get_browser_state_with_recovery()
- link_indices = []
- for index, element in state.dom_state.selector_map.items():
- if hasattr(element, 'tag_name') and element.tag_name == 'a':
- link_indices.append(index)
-
- assert len(link_indices) >= 2, 'Need at least 2 links for comparison test'
-
- # Test normal click (new_tab=False) - should navigate in current tab
- click_action_normal = {'click_element_by_index': ClickElementAction(index=link_indices[0], new_tab=False)}
-
- class ClickActionModel(ActionModel):
- click_element_by_index: ClickElementAction | None = None
-
- result = await controller.act(ClickActionModel(**click_action_normal), browser_session)
- await asyncio.sleep(1)
-
- # Should still have same number of tabs
- assert len(browser_session.tabs) == initial_tab_count
-
- # Navigate back to comparison page for second test
- await controller.act(GoToUrlActionModel(**goto_action), browser_session)
- await asyncio.sleep(1)
-
- # Test new tab click (new_tab=True) - should open in new tab
- click_action_new_tab = {'click_element_by_index': ClickElementAction(index=link_indices[1], new_tab=True)}
- result = await controller.act(ClickActionModel(**click_action_new_tab), browser_session)
- await asyncio.sleep(1)
-
- # Should have one more tab
- assert len(browser_session.tabs) == initial_tab_count + 1
-
- async def test_inline_element_mostly_offscreen(self, controller, browser_session, base_url, http_server):
- """Test clicking an inline element that's mostly outside the viewport."""
- # Add route for test page with inline element extending beyond viewport
- http_server.expect_request('/inline_offscreen').respond_with_data(
- """
- <!DOCTYPE html>
- <html>
- <head>
- <title>Inline Offscreen Test</title>
- <style>
- body { margin: 0; padding: 20px; }
- .container { position: relative; width: 200%; }
- .inline-link {
- display: inline;
- position: absolute;
- left: -100px;
- width: 500px;
- padding: 10px;
- background: #007bff;
- color: white;
- cursor: pointer;
- }
- #result { margin-top: 100px; }
- </style>
- </head>
- <body>
- <div class="container">
- <span class="inline-link" onclick="document.getElementById('result').textContent = 'Inline clicked'">
- This is a very long inline element that extends way beyond the viewport edge
- </span>
- </div>
- <div id="result">Not clicked</div>
- </body>
- </html>
- """,
- content_type='text/html',
- )
-
- # Navigate to the page
- goto_action = {'go_to_url': GoToUrlAction(url=f'{base_url}/inline_offscreen', new_tab=False)}
-
- from browser_use.agent.views import ActionModel
-
- class GoToUrlActionModel(ActionModel):
- go_to_url: GoToUrlAction | None = None
-
- await controller.act(GoToUrlActionModel(**goto_action), browser_session)
- await asyncio.sleep(0.5)
-
- # Get the clickable elements
- await browser_session.get_browser_state_summary(cache_clickable_elements_hashes=True)
- selector_map = await browser_session.get_selector_map()
-
- # Find the inline element
- inline_index = None
- for idx, element in selector_map.items():
- if 'inline-link' in str(element.attributes.get('class', '')):
- inline_index = idx
- break
-
- assert inline_index is not None, 'Could not find inline element'
-
- # Click the element - should click the visible portion
- class ClickActionModel(ActionModel):
- click_element_by_index: ClickElementAction | None = None
-
- result = await controller.act(
- ClickActionModel(click_element_by_index=ClickElementAction(index=inline_index)),
- browser_session
- )
-
- assert result.error is None, f'Click failed: {result.error}'
-
- # Verify click worked using CDP
- cdp_session = await browser_session.get_or_create_cdp_session()
- result_js = await browser_session.cdp_client.send.Runtime.evaluate(
- params={'expression': "document.getElementById('result').textContent", 'returnByValue': True},
- session_id=cdp_session.session_id
- )
- assert result_js.get('result', {}).get('value') == 'Inline clicked'
-
- async def test_block_inside_inline_multiline(self, controller, browser_session, base_url, http_server):
- """Test clicking a block element inside an inline element that spans multiple lines."""
- # Add route for complex nested layout
- http_server.expect_request('/block_in_inline').respond_with_data(
- """
- <!DOCTYPE html>
- <html>
- <head>
- <title>Block in Inline Test</title>
- <style>
- body { margin: 20px; width: 300px; }
- .inline-wrapper {
- display: inline;
- background: #f0f0f0;
- line-height: 1.5;
- }
- .block-inside {
- display: block;
- margin: 10px 0;
- padding: 10px;
- background: #007bff;
- color: white;
- cursor: pointer;
- }
- #result { margin-top: 50px; }
- </style>
- </head>
- <body>
- <span class="inline-wrapper">
- This is some text that wraps around and contains
- <div class="block-inside" onclick="document.getElementById('result').textContent = 'Block clicked'">
- Click this block element
- </div>
- and continues after the block element with more text that will wrap to multiple lines
- </span>
- <div id="result">Not clicked</div>
- </body>
- </html>
- """,
- content_type='text/html',
- )
-
- # Navigate to the page
- goto_action = {'go_to_url': GoToUrlAction(url=f'{base_url}/block_in_inline', new_tab=False)}
-
- from browser_use.agent.views import ActionModel
-
- class GoToUrlActionModel(ActionModel):
- go_to_url: GoToUrlAction | None = None
-
- await controller.act(GoToUrlActionModel(**goto_action), browser_session)
- await asyncio.sleep(0.5)
-
- # Get the clickable elements
- await browser_session.get_browser_state_summary(cache_clickable_elements_hashes=True)
- selector_map = await browser_session.get_selector_map()
-
- # Find the block element inside inline
- block_index = None
- for idx, element in selector_map.items():
- if 'block-inside' in str(element.attributes.get('class', '')):
- block_index = idx
- break
-
- assert block_index is not None, 'Could not find block element'
-
- # Click the block element
- class ClickActionModel(ActionModel):
- click_element_by_index: ClickElementAction | None = None
-
- result = await controller.act(
- ClickActionModel(click_element_by_index=ClickElementAction(index=block_index)),
- browser_session
- )
-
- assert result.error is None, f'Click failed: {result.error}'
-
- # Verify click worked
- cdp_session = await browser_session.get_or_create_cdp_session()
- result_js = await browser_session.cdp_client.send.Runtime.evaluate(
- params={'expression': "document.getElementById('result').textContent", 'returnByValue': True},
- session_id=cdp_session.session_id
- )
- assert result_js.get('result', {}).get('value') == 'Block clicked'
-
- async def test_element_covered_by_overlay(self, controller, browser_session, base_url, http_server):
- """Test clicking an element that's mostly covered by another element."""
- # Add route for overlapping elements
- http_server.expect_request('/covered_element').respond_with_data(
- """
- <!DOCTYPE html>
- <html>
- <head>
- <title>Covered Element Test</title>
- <style>
- body { margin: 20px; position: relative; }
- .target {
- position: absolute;
- top: 50px;
- left: 50px;
- width: 200px;
- height: 100px;
- padding: 20px;
- background: #28a745;
- color: white;
- cursor: pointer;
- z-index: 1;
- }
- .overlay {
- position: absolute;
- top: 60px;
- left: 60px;
- width: 180px;
- height: 80px;
- background: rgba(255, 0, 0, 0.7);
- z-index: 2;
- pointer-events: none; /* Allow clicks through */
- }
- #result { margin-top: 200px; }
- </style>
- </head>
- <body>
- <div class="target" onclick="document.getElementById('result').textContent = 'Target clicked'">
- Click me (partially covered)
- </div>
- <div class="overlay">Overlaying element</div>
- <div id="result">Not clicked</div>
- </body>
- </html>
- """,
- content_type='text/html',
- )
-
- # Navigate to the page
- goto_action = {'go_to_url': GoToUrlAction(url=f'{base_url}/covered_element', new_tab=False)}
-
- from browser_use.agent.views import ActionModel
-
- class GoToUrlActionModel(ActionModel):
- go_to_url: GoToUrlAction | None = None
-
- await controller.act(GoToUrlActionModel(**goto_action), browser_session)
- await asyncio.sleep(0.5)
-
- # Get the clickable elements
- await browser_session.get_browser_state_summary(cache_clickable_elements_hashes=True)
- selector_map = await browser_session.get_selector_map()
-
- # Find the target element
- target_index = None
- for idx, element in selector_map.items():
- if 'target' in str(element.attributes.get('class', '')):
- target_index = idx
- break
-
- assert target_index is not None, 'Could not find target element'
-
- # Click should still work on the visible portion
- class ClickActionModel(ActionModel):
- click_element_by_index: ClickElementAction | None = None
-
- result = await controller.act(
- ClickActionModel(click_element_by_index=ClickElementAction(index=target_index)),
- browser_session
- )
-
- assert result.error is None, f'Click failed: {result.error}'
-
- # Verify click worked
- cdp_session = await browser_session.get_or_create_cdp_session()
- result_js = await browser_session.cdp_client.send.Runtime.evaluate(
- params={'expression': "document.getElementById('result').textContent", 'returnByValue': True},
- session_id=cdp_session.session_id
- )
- assert result_js.get('result', {}).get('value') == 'Target clicked'
-
- async def test_file_input_click_prevention(self, controller, browser_session, base_url, http_server):
- """Test that clicking a file input element raises an exception."""
- # Add route with file input
- http_server.expect_request('/file_input').respond_with_data(
- """
- <!DOCTYPE html>
- <html>
- <head>
- <title>File Input Test</title>
- </head>
- <body>
- <h1>File Upload Test</h1>
- <input type="file" id="fileInput" />
- <div id="result">No file selected</div>
- </body>
- </html>
- """,
- content_type='text/html',
- )
-
- # Navigate to the page
- goto_action = {'go_to_url': GoToUrlAction(url=f'{base_url}/file_input', new_tab=False)}
-
- from browser_use.agent.views import ActionModel
-
- class GoToUrlActionModel(ActionModel):
- go_to_url: GoToUrlAction | None = None
-
- await controller.act(GoToUrlActionModel(**goto_action), browser_session)
- await asyncio.sleep(0.5)
-
- # Get the clickable elements
- await browser_session.get_browser_state_summary(cache_clickable_elements_hashes=True)
- selector_map = await browser_session.get_selector_map()
-
- # Find the file input
- file_input_index = None
- for idx, element in selector_map.items():
- if element.tag_name and element.tag_name.lower() == 'input':
- if element.attributes and element.attributes.get('type') == 'file':
- file_input_index = idx
- break
-
- assert file_input_index is not None, 'Could not find file input element'
-
- # Attempt to click should raise an exception
- class ClickActionModel(ActionModel):
- click_element_by_index: ClickElementAction | None = None
-
- result = await controller.act(
- ClickActionModel(click_element_by_index=ClickElementAction(index=file_input_index)),
- browser_session
- )
-
- # Should have an error about file inputs
- assert result.error is not None, 'Expected error for file input click'
- assert 'file input' in result.error.lower() or 'file upload' in result.error.lower(), \
- f'Error message should mention file input, got: {result.error}'
-
- async def test_select_dropdown_click_prevention(self, controller, browser_session, base_url, http_server):
- """Test that clicking a select dropdown element raises an exception."""
- # Add route with select dropdown
- http_server.expect_request('/select_dropdown').respond_with_data(
- """
- <!DOCTYPE html>
- <html>
- <head>
- <title>Select Dropdown Test</title>
- </head>
- <body>
- <h1>Select Test</h1>
- <select id="testSelect">
- <option value="">Choose one</option>
- <option value="opt1">Option 1</option>
- <option value="opt2">Option 2</option>
- </select>
- <div id="result">Nothing selected</div>
- </body>
- </html>
- """,
- content_type='text/html',
- )
-
- # Navigate to the page
- goto_action = {'go_to_url': GoToUrlAction(url=f'{base_url}/select_dropdown', new_tab=False)}
-
- from browser_use.agent.views import ActionModel
-
- class GoToUrlActionModel(ActionModel):
- go_to_url: GoToUrlAction | None = None
-
- await controller.act(GoToUrlActionModel(**goto_action), browser_session)
- await asyncio.sleep(0.5)
-
- # Get the clickable elements
- await browser_session.get_browser_state_summary(cache_clickable_elements_hashes=True)
- selector_map = await browser_session.get_selector_map()
-
- # Find the select element
- select_index = None
- for idx, element in selector_map.items():
- if element.tag_name and element.tag_name.lower() == 'select':
- select_index = idx
- break
-
- assert select_index is not None, 'Could not find select element'
-
- # Attempt to click should raise an exception
- class ClickActionModel(ActionModel):
- click_element_by_index: ClickElementAction | None = None
-
- result = await controller.act(
- ClickActionModel(click_element_by_index=ClickElementAction(index=select_index)),
- browser_session
- )
-
- # Should have an error about select elements
- assert result.error is not None, 'Expected error for select element click'
- assert 'select' in result.error.lower() and 'dropdown' in result.error.lower(), \
- f'Error message should mention select/dropdown, got: {result.error}'
diff --git a/tests/ci/test_ScrollEvent.py b/tests/ci/test_ScrollEvent.py
deleted file mode 100644
index bd57751eb..000000000
--- a/tests/ci/test_ScrollEvent.py
+++ /dev/null
@@ -1,423 +0,0 @@
-import asyncio
-
-import pytest
-from pytest_httpserver import HTTPServer
-
-from browser_use.agent.views import ActionModel, ActionResult
-from browser_use.browser import BrowserSession
-from browser_use.browser.profile import BrowserProfile
-from browser_use.controller.service import Controller
-from browser_use.controller.views import (
- GoToUrlAction,
- ScrollAction,
-)
-
-
[email protected](scope='session')
-def http_server():
- """Create and provide a test HTTP server that serves static content."""
- server = HTTPServer()
- server.start()
-
- # Add routes for common test pages
- server.expect_request('/').respond_with_data(
- '<html><head><title>Test Home Page</title></head><body><h1>Test Home Page</h1><p>Welcome to the test site</p></body></html>',
- content_type='text/html',
- )
-
- server.expect_request('/scrollable').respond_with_data(
- """
- <!DOCTYPE html>
- <html>
- <head>
- <title>Scrollable Page</title>
- <style>
- body { margin: 0; padding: 20px; }
- .content { height: 3000px; background: linear-gradient(to bottom, #f0f0f0, #333); }
- .marker { padding: 20px; background: #007bff; color: white; margin: 500px 0; }
- </style>
- </head>
- <body>
- <h1>Scrollable Test Page</h1>
- <div class="content">
- <div class="marker" id="marker1">Marker 1</div>
- <div class="marker" id="marker2">Marker 2</div>
- <div class="marker" id="marker3">Marker 3</div>
- </div>
- </body>
- </html>
- """,
- content_type='text/html',
- )
-
- yield server
- server.stop()
-
-
[email protected](scope='session')
-def base_url(http_server):
- """Return the base URL for the test HTTP server."""
- return f'http://{http_server.host}:{http_server.port}'
-
-
[email protected](scope='module')
-async def browser_session():
- """Create and provide a Browser instance with security disabled."""
- profile = BrowserProfile(headless=True, disable_security=True, cross_origin_iframes=False)
- session = BrowserSession(browser_profile=profile)
- await session.start()
- yield session
- await session.kill()
-
-
-def controller():
- """Create and provide a Controller instance."""
- return Controller()
-
-
-class TestScrollActions:
- """Test scroll-related actions and events."""
-
- async def test_scroll_actions(self, controller, browser_session, base_url, http_server):
- """Test basic scroll action functionality."""
-
- # Navigate to scrollable page
- goto_action = {'go_to_url': GoToUrlAction(url=f'{base_url}/scrollable', new_tab=False)}
-
- class GoToUrlActionModel(ActionModel):
- go_to_url: GoToUrlAction | None = None
-
- await controller.act(GoToUrlActionModel(**goto_action), browser_session)
-
- # Test 1: Basic page scroll down
- scroll_action = {'scroll': ScrollAction(down=True, num_pages=1.0)}
-
- class ScrollActionModel(ActionModel):
- scroll: ScrollAction | None = None
-
- result = await controller.act(ScrollActionModel(**scroll_action), browser_session)
-
- # Verify scroll down succeeded
- assert isinstance(result, ActionResult)
- assert result.error is None, f'Scroll down failed: {result.error}'
- assert result.extracted_content is not None
- assert 'Scrolled down' in result.extracted_content
- assert 'the page' in result.extracted_content
- assert result.include_in_memory is True
-
- # Test 2: Basic page scroll up
- scroll_up_action = {'scroll': ScrollAction(down=False, num_pages=0.5)}
- result = await controller.act(ScrollActionModel(**scroll_up_action), browser_session)
-
- assert isinstance(result, ActionResult)
- assert result.error is None, f'Scroll up failed: {result.error}'
- assert result.extracted_content is not None
- assert 'Scrolled up' in result.extracted_content
- assert '0.5 pages' in result.extracted_content
-
- # Test 3: Test with invalid element index (should error)
- invalid_scroll_action = {'scroll': ScrollAction(down=True, num_pages=1.0, index=999)}
- result = await controller.act(ScrollActionModel(**invalid_scroll_action), browser_session)
-
- # This should fail with error about element not found
- assert isinstance(result, ActionResult)
- assert result.error is not None, 'Expected error for invalid element index'
- assert 'Element index 999 not found' in result.error or 'Failed to scroll' in result.error
-
- # Test 4: Model parameter validation
- scroll_with_index = ScrollAction(down=True, num_pages=1.0, index=5)
- assert scroll_with_index.down is True
- assert scroll_with_index.num_pages == 1.0
- assert scroll_with_index.index == 5
-
- scroll_without_index = ScrollAction(down=False, num_pages=0.25)
- assert scroll_without_index.down is False
- assert scroll_without_index.num_pages == 0.25
- assert scroll_without_index.index is None
-
- async def test_scroll_with_cross_origin_disabled(self, browser_session, base_url):
- """Test that scroll works when cross_origin_iframes is disabled."""
- from browser_use.browser.events import ScrollEvent
-
- # Navigate to a page
- await browser_session._cdp_navigate(f'{base_url}/scrollable')
- await asyncio.sleep(0.5)
-
- # Test simple scroll - should not hang
- event = browser_session.event_bus.dispatch(ScrollEvent(direction='down', amount=500))
- result = await asyncio.wait_for(event, timeout=3.0)
- assert result is not None
-
- # Test scroll up
- event = browser_session.event_bus.dispatch(ScrollEvent(direction='up', amount=200))
- result = await asyncio.wait_for(event, timeout=3.0)
- assert result is not None
-
- async def test_scroll_event_directly(self, browser_session):
- """Test ScrollEvent directly through the event bus."""
- from browser_use.browser.events import ScrollEvent
-
- # Test scroll on about:blank (should work)
- event = browser_session.event_bus.dispatch(ScrollEvent(direction='down', amount=100))
- result = await asyncio.wait_for(event, timeout=2.0)
- event_result = await result.event_result()
- assert event_result is not None
- assert event_result.get('success') is True
-
- async def test_scroll_non_scrollable_page(self, browser_session, base_url, http_server):
- """Test scrolling a page that's only 100px tall (not scrollable)."""
- from browser_use.browser.events import ScrollEvent
-
- # Add a non-scrollable page (content fits in viewport)
- http_server.expect_request('/non-scrollable').respond_with_data(
- """
- <!DOCTYPE html>
- <html>
- <head>
- <title>Non-Scrollable Page</title>
- <style>
- body { margin: 0; padding: 10px; height: 80px; overflow: hidden; }
- .content { height: 60px; background: #f0f0f0; }
- </style>
- </head>
- <body>
- <div class="content">This page is too small to scroll</div>
- </body>
- </html>
- """,
- content_type='text/html',
- )
-
- # Navigate to non-scrollable page
- await browser_session._cdp_navigate(f'{base_url}/non-scrollable')
- await asyncio.sleep(0.5)
-
- # Get initial scroll position
- cdp_session = await browser_session.get_or_create_cdp_session()
- initial_scroll = await browser_session.cdp_client.send.Runtime.evaluate(
- params={'expression': 'window.pageYOffset', 'returnByValue': True},
- session_id=cdp_session.session_id,
- )
- initial_y = initial_scroll.get('result', {}).get('value', 0)
-
- # Try to scroll down - should succeed but not actually move
- event = browser_session.event_bus.dispatch(ScrollEvent(direction='down', amount=500))
- result = await asyncio.wait_for(event, timeout=3.0)
- event_result = await result.event_result()
- assert event_result is not None
- assert event_result.get('success') is True
-
- # Check scroll position didn't change (page isn't scrollable)
- final_scroll = await browser_session.cdp_client.send.Runtime.evaluate(
- params={'expression': 'window.pageYOffset', 'returnByValue': True},
- session_id=cdp_session.session_id,
- )
- final_y = final_scroll.get('result', {}).get('value', 0)
- assert final_y == initial_y, f'Scroll position changed on non-scrollable page: {initial_y} -> {final_y}'
-
- async def test_scroll_very_long_page(self, browser_session, base_url, http_server):
- """Test scrolling a very long page (over 10,000px) by 8,000px."""
- from browser_use.browser.events import ScrollEvent
-
- # Add a very long page
- http_server.expect_request('/very-long').respond_with_data(
- """
- <!DOCTYPE html>
- <html>
- <head>
- <title>Very Long Page</title>
- <style>
- body { margin: 0; padding: 20px; }
- .content { height: 12000px; background: linear-gradient(to bottom, #f0f0f0, #333); }
- .marker { padding: 20px; background: #007bff; color: white; margin: 2000px 0; }
- </style>
- </head>
- <body>
- <h1 id="top">Very Long Page - Top</h1>
- <div class="content">
- <div class="marker" id="marker1">Marker 1 at 2000px</div>
- <div class="marker" id="marker2">Marker 2 at 4000px</div>
- <div class="marker" id="marker3">Marker 3 at 6000px</div>
- <div class="marker" id="marker4">Marker 4 at 8000px</div>
- <div class="marker" id="marker5">Marker 5 at 10000px</div>
- </div>
- <h1 id="bottom">Very Long Page - Bottom</h1>
- </body>
- </html>
- """,
- content_type='text/html',
- )
-
- # Navigate to very long page
- await browser_session._cdp_navigate(f'{base_url}/very-long')
- await asyncio.sleep(0.5)
-
- # Get initial scroll position
- cdp_session = await browser_session.get_or_create_cdp_session()
- initial_scroll = await browser_session.cdp_client.send.Runtime.evaluate(
- params={'expression': 'window.pageYOffset', 'returnByValue': True},
- session_id=cdp_session.session_id,
- )
- initial_y = initial_scroll.get('result', {}).get('value', 0)
- assert initial_y == 0, f'Page should start at top, but pageYOffset is {initial_y}'
-
- # Scroll down by 8000px
- event = browser_session.event_bus.dispatch(ScrollEvent(direction='down', amount=8000))
- result = await asyncio.wait_for(event, timeout=3.0)
- event_result = await result.event_result()
- assert event_result is not None
- assert event_result.get('success') is True
-
- # Wait a bit for scroll to take effect
- await asyncio.sleep(0.5)
-
- # Check scroll position moved significantly
- final_scroll = await browser_session.cdp_client.send.Runtime.evaluate(
- params={'expression': 'window.pageYOffset', 'returnByValue': True},
- session_id=cdp_session.session_id,
- )
- final_y = final_scroll.get('result', {}).get('value', 0)
-
- # Get page height to understand constraints
- page_height = await browser_session.cdp_client.send.Runtime.evaluate(
- params={'expression': 'document.body.scrollHeight', 'returnByValue': True},
- session_id=cdp_session.session_id,
- )
- scroll_height = page_height.get('result', {}).get('value', 0)
-
- # Should have scrolled down significantly (might not be exactly 8000 due to viewport constraints)
- assert final_y > 5000, f'Expected to scroll significantly (page height: {scroll_height}px), but only at {final_y}px'
-
- # Verify we can see marker 4 which is at 8000px
- marker4_visible = await browser_session.cdp_client.send.Runtime.evaluate(
- params={
- 'expression': """
- (() => {
- const marker = document.getElementById('marker4');
- const rect = marker.getBoundingClientRect();
- return rect.top >= 0 && rect.top <= window.innerHeight;
- })()
- """,
- 'returnByValue': True,
- },
- session_id=cdp_session.session_id,
- )
- assert marker4_visible.get('result', {}).get('value', False), 'Marker 4 should be visible after scrolling 8000px'
-
- async def test_scroll_iframe_content(self, browser_session, base_url, http_server):
- """Test scrolling inside a same-origin iframe."""
- from browser_use.browser.events import ScrollEvent
-
- # Add iframe content page
- http_server.expect_request('/iframe-content').respond_with_data(
- """
- <!DOCTYPE html>
- <html>
- <head>
- <style>
- body { margin: 0; padding: 10px; }
- .content { height: 2000px; background: linear-gradient(to bottom, #e0e0e0, #666); }
- </style>
- </head>
- <body>
- <h2 id="iframe-top">Iframe Content - Top</h2>
- <div class="content">
- <div style="margin-top: 900px;">Middle of iframe content</div>
- <div style="margin-top: 900px;">Bottom of iframe content</div>
- </div>
- </body>
- </html>
- """,
- content_type='text/html',
- )
-
- # Add main page with iframe
- http_server.expect_request('/page-with-iframe').respond_with_data(
- f"""
- <!DOCTYPE html>
- <html>
- <head>
- <title>Page with Iframe</title>
- <style>
- body {{ margin: 0; padding: 20px; }}
- #main-content {{ height: 200px; background: #f0f0f0; }}
- #scrollable-iframe {{
- width: 100%;
- height: 400px;
- border: 2px solid #333;
- }}
- </style>
- </head>
- <body>
- <div id="main-content">
- <h1>Main Page Content</h1>
- <p>This is the main page with an embedded iframe below.</p>
- </div>
- <iframe id="scrollable-iframe" src="{base_url}/iframe-content"></iframe>
- <div style="height: 200px; background: #e0e0e0;">
- <p>Content after iframe</p>
- </div>
- </body>
- </html>
- """,
- content_type='text/html',
- )
-
- # Navigate to page with iframe
- await browser_session._cdp_navigate(f'{base_url}/page-with-iframe')
- await asyncio.sleep(1.0) # Give iframe time to load
-
- # Get initial scroll position of main page and iframe
- cdp_session = await browser_session.get_or_create_cdp_session()
-
- # Check main page scroll
- main_scroll = await browser_session.cdp_client.send.Runtime.evaluate(
- params={'expression': 'window.pageYOffset', 'returnByValue': True},
- session_id=cdp_session.session_id,
- )
- main_y = main_scroll.get('result', {}).get('value', 0)
-
- # Check iframe scroll (should start at 0)
- iframe_initial = await browser_session.cdp_client.send.Runtime.evaluate(
- params={
- 'expression': """
- (() => {
- const iframe = document.getElementById('scrollable-iframe');
- if (iframe && iframe.contentWindow) {
- return iframe.contentWindow.pageYOffset || 0;
- }
- return -1;
- })()
- """,
- 'returnByValue': True,
- },
- session_id=cdp_session.session_id,
- )
- iframe_y = iframe_initial.get('result', {}).get('value', -1)
- assert iframe_y == 0, f'Iframe should start at top, but pageYOffset is {iframe_y}'
-
- # Scroll the main page first to bring iframe into view
- event = browser_session.event_bus.dispatch(ScrollEvent(direction='down', amount=100))
- await asyncio.wait_for(event, timeout=3.0)
-
- # Now try to scroll inside the iframe
- # Note: This would require finding the iframe element and scrolling it specifically
- # For now, we just verify the iframe exists and is scrollable
- iframe_scrollable = await browser_session.cdp_client.send.Runtime.evaluate(
- params={
- 'expression': """
- (() => {
- const iframe = document.getElementById('scrollable-iframe');
- if (iframe && iframe.contentDocument) {
- const iframeBody = iframe.contentDocument.body;
- return iframeBody.scrollHeight > iframe.clientHeight;
- }
- return false;
- })()
- """,
- 'returnByValue': True,
- },
- session_id=cdp_session.session_id,
- )
- assert iframe_scrollable.get('result', {}).get('value', False), 'Iframe should be scrollable'
diff --git a/tests/ci/test_TypeTextEvent.py b/tests/ci/test_TypeTextEvent.py
deleted file mode 100644
index 5aa786276..000000000
--- a/tests/ci/test_TypeTextEvent.py
+++ /dev/null
@@ -1,682 +0,0 @@
-import asyncio
-
-import pytest
-from pytest_httpserver import HTTPServer
-
-from browser_use.agent.views import ActionModel, ActionResult
-from browser_use.browser import BrowserSession
-from browser_use.browser.profile import BrowserProfile
-from browser_use.controller.service import Controller
-from browser_use.controller.views import (
- GoToUrlAction,
- InputTextAction,
-)
-
-
[email protected](scope='session')
-def http_server():
- """Create and provide a test HTTP server that serves static content."""
- server = HTTPServer()
- server.start()
-
- # Add routes for common test pages
- server.expect_request('/').respond_with_data(
- '<html><head><title>Test Home Page</title></head><body><h1>Test Home Page</h1><p>Welcome to the test site</p></body></html>',
- content_type='text/html',
- )
-
- server.expect_request('/form').respond_with_data(
- """
- <!DOCTYPE html>
- <html>
- <head>
- <title>Form Test Page</title>
- </head>
- <body>
- <h1>Test Form</h1>
- <form>
- <input type="text" id="name" name="name" placeholder="Enter name">
- <input type="email" id="email" name="email" placeholder="Enter email">
- <textarea id="message" name="message" placeholder="Enter message"></textarea>
- <input type="password" id="password" name="password" placeholder="Enter password">
- <button type="submit">Submit</button>
- </form>
- </body>
- </html>
- """,
- content_type='text/html',
- )
-
- yield server
- server.stop()
-
-
[email protected](scope='session')
-def base_url(http_server):
- """Return the base URL for the test HTTP server."""
- return f'http://{http_server.host}:{http_server.port}'
-
-
[email protected](scope='module')
-async def browser_session():
- """Create and provide a Browser instance with security disabled."""
- profile = BrowserProfile(headless=True, disable_security=True, cross_origin_iframes=False)
- session = BrowserSession(browser_profile=profile)
- await session.start()
- yield session
- await session.kill()
-
-
-def controller():
- """Create and provide a Controller instance."""
- return Controller()
-
-
-class TestTypeTextEvent:
- """Test TypeTextEvent and input_text action functionality."""
-
- async def test_input_text_action(self, controller, browser_session, base_url, http_server):
- """Test that InputTextAction correctly inputs text into form fields."""
- # Set up search form endpoint for this test
- http_server.expect_request('/searchform').respond_with_data(
- """
- <html>
- <head><title>Search Form</title></head>
- <body>
- <h1>Search Form</h1>
- <form action="/search" method="get">
- <input type="text" id="searchbox" name="q" placeholder="Search...">
- <button type="submit">Search</button>
- </form>
- </body>
- </html>
- """,
- content_type='text/html',
- )
-
- # Navigate to a page with a form
- goto_action = {'go_to_url': GoToUrlAction(url=f'{base_url}/searchform', new_tab=False)}
-
- class GoToUrlActionModel(ActionModel):
- go_to_url: GoToUrlAction | None = None
-
- await controller.act(GoToUrlActionModel(**goto_action), browser_session)
-
- # Wait for page to load
- await asyncio.sleep(0.5)
-
- # Get the search input field index
- selector_map = await browser_session.get_selector_map()
-
- # Find the search input field - this requires examining the DOM
- # We'll mock this part since we can't rely on specific element indices
- # In a real test, you would get the actual index from the selector map
-
- # For demonstration, we'll just use a hard-coded mock value
- # and check that the controller processes the action correctly
- mock_input_index = 1 # This would normally be determined dynamically
-
- # Create input text action
- input_action = {'input_text': InputTextAction(index=mock_input_index, text='Python programming')}
-
- class InputTextActionModel(ActionModel):
- input_text: InputTextAction | None = None
-
- # The actual input might fail if the page structure changes or in headless mode
- # So we'll just verify the controller correctly processes the action
- result = await controller.act(InputTextActionModel(**input_action), browser_session)
-
- # Verify the result is an ActionResult
- assert isinstance(result, ActionResult)
-
- # Check if the action succeeded or failed
- if result.error is None:
- # Action succeeded, verify the extracted_content
- assert result.extracted_content is not None
- assert 'Input' in result.extracted_content
- else:
- # Action failed, verify the error message contains expected text
- assert 'Element index' in result.error or 'does not exist' in result.error or 'Failed to input text' in result.error
-
- async def test_type_text_event_directly(self, browser_session, base_url):
- """Test TypeTextEvent directly through the event bus."""
- from browser_use.browser.events import TypeTextEvent
-
- # Navigate to a page with input fields
- await browser_session._cdp_navigate(f'{base_url}/form')
- await asyncio.sleep(0.5)
-
- # Get the DOM state to find input elements
- state = await browser_session.get_browser_state_summary()
-
- # Find an input field
- input_node = None
- for node in state.dom_state.selector_map.values():
- if node.tag_name == 'input' and node.attributes.get('type') == 'text':
- input_node = node
- break
-
- if input_node:
- # Test typing text into the input field
- event = browser_session.event_bus.dispatch(TypeTextEvent(node=input_node, text='Hello World', clear_existing=True))
- result = await asyncio.wait_for(event, timeout=3.0)
- event_result = await result.event_result()
- assert event_result is not None
- assert event_result.get('success') is True
-
- # Verify the text was actually typed
- cdp_session = await browser_session.get_or_create_cdp_session()
- value_check = await browser_session.cdp_client.send.Runtime.evaluate(
- params={
- 'expression': f'document.getElementById("{input_node.attributes.get("id")}").value',
- 'returnByValue': True,
- },
- session_id=cdp_session.session_id,
- )
- assert value_check.get('result', {}).get('value') == 'Hello World'
-
- async def test_type_text_clear_existing(self, browser_session, base_url):
- """Test TypeTextEvent with clear_existing flag."""
- from browser_use.browser.events import TypeTextEvent
-
- # Navigate to form page
- await browser_session._cdp_navigate(f'{base_url}/form')
- await asyncio.sleep(0.5)
-
- # Get DOM state
- state = await browser_session.get_browser_state_summary()
-
- # Find email input field
- email_node = None
- for node in state.dom_state.selector_map.values():
- if node.tag_name == 'input' and node.attributes.get('type') == 'email':
- email_node = node
- break
-
- if email_node:
- # First, type some text without clearing
- event = browser_session.event_bus.dispatch(
- TypeTextEvent(node=email_node, text='[email protected]', clear_existing=False)
- )
- await asyncio.wait_for(event, timeout=3.0)
-
- # Now type new text with clearing
- event = browser_session.event_bus.dispatch(
- TypeTextEvent(node=email_node, text='[email protected]', clear_existing=True)
- )
- result = await asyncio.wait_for(event, timeout=3.0)
- event_result = await result.event_result()
- assert event_result is not None
- assert event_result.get('success') is True
-
- # Verify only the second text is in the field
- cdp_session = await browser_session.get_or_create_cdp_session()
- value_check = await browser_session.cdp_client.send.Runtime.evaluate(
- params={
- 'expression': f'document.getElementById("{email_node.attributes.get("id")}").value',
- 'returnByValue': True,
- },
- session_id=cdp_session.session_id,
- )
- assert value_check.get('result', {}).get('value') == '[email protected]'
-
- async def test_type_text_textarea(self, browser_session, base_url):
- """Test typing text into a textarea element."""
- from browser_use.browser.events import TypeTextEvent
-
- # Navigate to form page
- await browser_session._cdp_navigate(f'{base_url}/form')
- await asyncio.sleep(0.5)
-
- # Get DOM state
- state = await browser_session.get_browser_state_summary()
-
- # Find textarea element
- textarea_node = None
- for node in state.dom_state.selector_map.values():
- if node.tag_name == 'textarea':
- textarea_node = node
- break
-
- if textarea_node:
- # Type multiline text
- multiline_text = 'Line 1\nLine 2\nLine 3'
- event = browser_session.event_bus.dispatch(
- TypeTextEvent(node=textarea_node, text=multiline_text, clear_existing=True)
- )
- result = await asyncio.wait_for(event, timeout=3.0)
- event_result = await result.event_result()
- assert event_result is not None
- assert event_result.get('success') is True
-
- # Verify the multiline text was typed
- cdp_session = await browser_session.get_or_create_cdp_session()
- value_check = await browser_session.cdp_client.send.Runtime.evaluate(
- params={
- 'expression': f'document.getElementById("{textarea_node.attributes.get("id")}").value',
- 'returnByValue': True,
- },
- session_id=cdp_session.session_id,
- )
- assert value_check.get('result', {}).get('value') == multiline_text
-
- async def test_type_text_password_field(self, browser_session, base_url):
- """Test typing into a password field."""
- from browser_use.browser.events import TypeTextEvent
-
- # Navigate to form page
- await browser_session._cdp_navigate(f'{base_url}/form')
- await asyncio.sleep(0.5)
-
- # Get DOM state
- state = await browser_session.get_browser_state_summary()
-
- # Find password input field
- password_node = None
- for node in state.dom_state.selector_map.values():
- if node.tag_name == 'input' and node.attributes.get('type') == 'password':
- password_node = node
- break
-
- if password_node:
- # Type password text
- password_text = 'SecureP@ssw0rd!'
- event = browser_session.event_bus.dispatch(TypeTextEvent(node=password_node, text=password_text, clear_existing=True))
- result = await asyncio.wait_for(event, timeout=3.0)
- event_result = await result.event_result()
- assert event_result is not None
- assert event_result.get('success') is True
-
- # Verify the password was typed (value is present but masked visually)
- cdp_session = await browser_session.get_or_create_cdp_session()
- value_check = await browser_session.cdp_client.send.Runtime.evaluate(
- params={
- 'expression': f'document.getElementById("{password_node.attributes.get("id")}").value',
- 'returnByValue': True,
- },
- session_id=cdp_session.session_id,
- )
- assert value_check.get('result', {}).get('value') == password_text
-
- async def test_type_text_readonly_field(self, browser_session, base_url, http_server):
- """Test typing into a readonly field should handle gracefully."""
- from browser_use.browser.events import TypeTextEvent
-
- # Add page with readonly input
- http_server.expect_request('/readonly').respond_with_data(
- """
- <!DOCTYPE html>
- <html>
- <head><title>Readonly Test</title></head>
- <body>
- <h1>Readonly Field Test</h1>
- <input type="text" id="readonly-input" value="Cannot change this" readonly>
- <input type="text" id="normal-input" placeholder="Can change this">
- </body>
- </html>
- """,
- content_type='text/html',
- )
-
- # Navigate to page
- await browser_session._cdp_navigate(f'{base_url}/readonly')
- await asyncio.sleep(0.5)
-
- # Get DOM state
- state = await browser_session.get_browser_state_summary()
-
- # Find readonly input field
- readonly_node = None
- for node in state.dom_state.selector_map.values():
- if node.tag_name == 'input' and node.attributes.get('id') == 'readonly-input':
- readonly_node = node
- break
-
- if readonly_node:
- # Try to type into readonly field
- event = browser_session.event_bus.dispatch(TypeTextEvent(node=readonly_node, text='New text', clear_existing=True))
- result = await asyncio.wait_for(event, timeout=3.0)
- event_result = await result.event_result()
-
- # The operation should complete (CDP allows typing into readonly fields)
- assert event_result is not None
- assert event_result.get('success') is True
-
- # But the value should remain unchanged due to readonly attribute
- cdp_session = await browser_session.get_or_create_cdp_session()
- value_check = await browser_session.cdp_client.send.Runtime.evaluate(
- params={'expression': 'document.getElementById("readonly-input").value', 'returnByValue': True},
- session_id=cdp_session.session_id,
- )
- # Readonly fields keep their original value
- assert value_check.get('result', {}).get('value') == 'Cannot change this'
-
- async def test_type_text_special_characters(self, browser_session, base_url):
- """Test typing text with special characters."""
- from browser_use.browser.events import TypeTextEvent
-
- # Navigate to form page
- await browser_session._cdp_navigate(f'{base_url}/form')
- await asyncio.sleep(0.5)
-
- # Get DOM state
- state = await browser_session.get_browser_state_summary()
-
- # Find text input field
- text_node = None
- for node in state.dom_state.selector_map.values():
- if node.tag_name == 'input' and node.attributes.get('type') == 'text':
- text_node = node
- break
-
- if text_node:
- # Type text with special characters
- special_text = 'Test @#$%^&*()_+-={}[]|\\:";<>?,./~`'
- event = browser_session.event_bus.dispatch(TypeTextEvent(node=text_node, text=special_text, clear_existing=True))
- result = await asyncio.wait_for(event, timeout=3.0)
- event_result = await result.event_result()
- assert event_result is not None
- assert event_result.get('success') is True
-
- # Verify the special characters were typed correctly
- cdp_session = await browser_session.get_or_create_cdp_session()
- value_check = await browser_session.cdp_client.send.Runtime.evaluate(
- params={
- 'expression': f'document.getElementById("{text_node.attributes.get("id")}").value',
- 'returnByValue': True,
- },
- session_id=cdp_session.session_id,
- )
- assert value_check.get('result', {}).get('value') == special_text
-
- async def test_type_text_empty_string(self, browser_session, base_url):
- """Test clearing a field by typing empty string with clear_existing=True."""
- from browser_use.browser.events import TypeTextEvent
-
- # Navigate to form page
- await browser_session._cdp_navigate(f'{base_url}/form')
- await asyncio.sleep(0.5)
-
- # Get DOM state
- state = await browser_session.get_browser_state_summary()
-
- # Find text input field
- text_node = None
- for node in state.dom_state.selector_map.values():
- if node.tag_name == 'input' and node.attributes.get('type') == 'text':
- text_node = node
- break
-
- if text_node:
- # First type some text
- event = browser_session.event_bus.dispatch(TypeTextEvent(node=text_node, text='Initial text', clear_existing=True))
- await asyncio.wait_for(event, timeout=3.0)
-
- # Now clear it by typing empty string with clear_existing=True
- event = browser_session.event_bus.dispatch(TypeTextEvent(node=text_node, text='', clear_existing=True))
- result = await asyncio.wait_for(event, timeout=3.0)
- event_result = await result.event_result()
- assert event_result is not None
- assert event_result.get('success') is True
-
- # Verify the field is now empty
- cdp_session = await browser_session.get_or_create_cdp_session()
- value_check = await browser_session.cdp_client.send.Runtime.evaluate(
- params={
- 'expression': f'document.getElementById("{text_node.attributes.get("id")}").value',
- 'returnByValue': True,
- },
- session_id=cdp_session.session_id,
- )
- assert value_check.get('result', {}).get('value') == ''
-
- async def test_type_text_index_zero_whole_page(self, browser_session, base_url, http_server):
- """Test typing with index 0 types into the page (goes to whatever has focus)."""
- from browser_use.browser.events import TypeTextEvent
-
- # Create a page with an input that auto-focuses
- http_server.expect_request('/autofocus-form').respond_with_data(
- """
- <!DOCTYPE html>
- <html>
- <head><title>Autofocus Form</title></head>
- <body>
- <h1>Form with Autofocus</h1>
- <input type="text" id="first-input" placeholder="First input">
- <input type="text" id="autofocus-input" placeholder="Has autofocus" autofocus>
- <input type="text" id="third-input" placeholder="Third input">
- </body>
- </html>
- """,
- content_type='text/html',
- )
-
- # Navigate to page with autofocus
- await browser_session._cdp_navigate(f'{base_url}/autofocus-form')
- await asyncio.sleep(0.5)
-
- # Get DOM state
- state = await browser_session.get_browser_state_summary()
-
- # Create a node with index 0 - this should type to the page (whatever has focus)
- from browser_use.dom.views import EnhancedDOMTreeNode
-
- mock_node = EnhancedDOMTreeNode(
- element_index=0,
- node_id=0,
- backend_node_id=0,
- session_id='',
- frame_id='',
- target_id='',
- node_type=1,
- node_name='body',
- node_value='',
- attributes={},
- is_scrollable=False,
- is_visible=True,
- absolute_position=None,
- content_document=None,
- shadow_root_type=None,
- shadow_roots=None,
- parent_node=None,
- children_nodes=[],
- ax_node=None,
- snapshot_node=None,
- )
-
- # Type text with index 0 - should type to whatever has focus (the autofocus input)
- event = browser_session.event_bus.dispatch(TypeTextEvent(node=mock_node, text='Hello Page', clear_existing=False))
- result = await asyncio.wait_for(event, timeout=3.0)
- event_result = await result.event_result()
- assert event_result is not None
- assert event_result.get('success') is True
-
- # Verify the text went into the autofocus input
- cdp_session = await browser_session.get_or_create_cdp_session()
-
- # Check that the autofocus input has the text
- autofocus_value = await browser_session.cdp_client.send.Runtime.evaluate(
- params={'expression': 'document.getElementById("autofocus-input").value', 'returnByValue': True},
- session_id=cdp_session.session_id,
- )
- assert autofocus_value.get('result', {}).get('value') == 'Hello Page', 'Text should have gone to the autofocus input'
-
- # Check other inputs are empty
- first_value = await browser_session.cdp_client.send.Runtime.evaluate(
- params={'expression': 'document.getElementById("first-input").value', 'returnByValue': True},
- session_id=cdp_session.session_id,
- )
- assert first_value.get('result', {}).get('value') == '', 'First input should be empty'
-
- third_value = await browser_session.cdp_client.send.Runtime.evaluate(
- params={'expression': 'document.getElementById("third-input").value', 'returnByValue': True},
- session_id=cdp_session.session_id,
- )
- assert third_value.get('result', {}).get('value') == '', 'Third input should be empty'
-
- async def test_type_text_nonexistent_element(self, browser_session, base_url):
- """Test that typing into a non-existent element falls back to typing to the page."""
- from browser_use.browser.events import TypeTextEvent
-
- # Navigate to form page
- await browser_session._cdp_navigate(f'{base_url}/form')
- await asyncio.sleep(0.5)
-
- # Focus the first input manually to have a known focused element
- cdp_session = await browser_session.get_or_create_cdp_session()
- await browser_session.cdp_client.send.Runtime.evaluate(
- params={'expression': 'document.getElementById("name").focus()', 'returnByValue': True},
- session_id=cdp_session.session_id,
- )
-
- # Create a node with a non-existent index
- from browser_use.dom.views import EnhancedDOMTreeNode
-
- nonexistent_node = EnhancedDOMTreeNode(
- element_index=99999,
- node_id=99999,
- backend_node_id=99999, # Non-existent backend node
- session_id='',
- frame_id='',
- target_id='',
- node_type=1,
- node_name='input',
- node_value='',
- attributes={'id': 'nonexistent'},
- is_scrollable=False,
- is_visible=True,
- absolute_position=None,
- content_document=None,
- shadow_root_type=None,
- shadow_roots=None,
- parent_node=None,
- children_nodes=[],
- ax_node=None,
- snapshot_node=None,
- )
-
- # Try to type into non-existent element - should fall back to typing to the page
- event = browser_session.event_bus.dispatch(
- TypeTextEvent(node=nonexistent_node, text='Fallback text', clear_existing=False)
- )
-
- # This should complete (might succeed by falling back to page typing)
- result = await asyncio.wait_for(event, timeout=3.0)
- event_result = await result.event_result()
- assert event_result is not None
-
- # Check if the text was typed to the focused element as a fallback
- name_value = await browser_session.cdp_client.send.Runtime.evaluate(
- params={'expression': 'document.getElementById("name").value', 'returnByValue': True},
- session_id=cdp_session.session_id,
- )
-
- # Either it failed (and returned an error) or it fell back to typing to the page
- success = event_result.get('success', False)
- if success:
- # If it succeeded, it should have typed to the focused element
- assert name_value.get('result', {}).get('value') == 'Fallback text', (
- 'Text should have been typed to the focused element as fallback'
- )
- else:
- # Or it might have failed entirely
- assert event_result.get('error') is not None, "Should have an error if it didn't fall back"
-
- async def test_type_text_iframe_input(self, browser_session, base_url, http_server):
- """Test typing text into an input field inside an iframe."""
- from browser_use.browser.events import TypeTextEvent
-
- # Add iframe content page
- http_server.expect_request('/iframe-form').respond_with_data(
- """
- <!DOCTYPE html>
- <html>
- <head>
- <title>Iframe Form</title>
- </head>
- <body>
- <h2>Iframe Form</h2>
- <input type="text" id="iframe-input" name="iframe-field" placeholder="Type here in iframe">
- <div id="iframe-result"></div>
- </body>
- </html>
- """,
- content_type='text/html',
- )
-
- # Add main page with iframe
- http_server.expect_request('/page-with-form-iframe').respond_with_data(
- f"""
- <!DOCTYPE html>
- <html>
- <head>
- <title>Page with Form Iframe</title>
- </head>
- <body>
- <h1>Main Page</h1>
- <p>This page contains an iframe with a form:</p>
- <iframe id="form-iframe" src="{base_url}/iframe-form" style="width: 100%; height: 300px; border: 2px solid #333;"></iframe>
- <div>
- <input type="text" id="main-input" placeholder="Main page input">
- </div>
- </body>
- </html>
- """,
- content_type='text/html',
- )
-
- # Navigate to page with iframe
- await browser_session._cdp_navigate(f'{base_url}/page-with-form-iframe')
- await asyncio.sleep(1.0) # Give iframe time to load
-
- # Get DOM state to find the iframe input
- state = await browser_session.get_browser_state_summary()
-
- # Find the input inside the iframe (it should be in the DOM if same-origin)
- iframe_input_node = None
- for node in state.dom_state.selector_map.values():
- # Look for the iframe input by its id
- if node.tag_name == 'input' and node.attributes.get('id') == 'iframe-input':
- iframe_input_node = node
- break
-
- if iframe_input_node:
- # Type text into the iframe input
- event = browser_session.event_bus.dispatch(
- TypeTextEvent(node=iframe_input_node, text='Text in iframe', clear_existing=True)
- )
- result = await asyncio.wait_for(event, timeout=3.0)
- event_result = await result.event_result()
- assert event_result is not None
- assert event_result.get('success') is True
-
- # Verify the text was typed into the iframe input
- cdp_session = await browser_session.get_or_create_cdp_session()
- iframe_value = await browser_session.cdp_client.send.Runtime.evaluate(
- params={
- 'expression': """
- (() => {
- const iframe = document.getElementById('form-iframe');
- if (iframe && iframe.contentDocument) {
- const input = iframe.contentDocument.getElementById('iframe-input');
- return input ? input.value : null;
- }
- return null;
- })()
- """,
- 'returnByValue': True,
- },
- session_id=cdp_session.session_id,
- )
- assert iframe_value.get('result', {}).get('value') == 'Text in iframe'
-
- # Verify the main page input is still empty
- main_value = await browser_session.cdp_client.send.Runtime.evaluate(
- params={'expression': 'document.getElementById("main-input").value', 'returnByValue': True},
- session_id=cdp_session.session_id,
- )
- assert main_value.get('result', {}).get('value') == '', 'Main page input should still be empty'
- else:
- # If cross-origin iframes are disabled, we won't see the iframe content
- pytest.skip('Iframe input not found - likely due to cross_origin_iframes=False')
diff --git a/tests/ci/test_browser_session_tab_management.py b/tests/ci/test_browser_session_tab_management.py
index 6e20dc953..5619bccd4 100644
--- a/tests/ci/test_browser_session_tab_management.py
+++ b/tests/ci/test_browser_session_tab_management.py
@@ -416,28 +416,6 @@ class TestEventDrivenTabOperations:
assert len(closed_events) >= 1
assert closed_events[-1].tab_index == 1
- async def test_navigate_to_url_event_with_new_tab(self, browser_session, base_url):
- """Test NavigateToUrlEvent with new_tab=True."""
- from browser_use.browser.events import NavigateToUrlEvent, TabCreatedEvent
-
- initial_tab_count = len(browser_session.tabs)
-
- # Navigate to URL in new tab via direct event
- nav_event = browser_session.event_bus.dispatch(NavigateToUrlEvent(url=f'{base_url}/page2', new_tab=True))
- await nav_event
-
- # Verify new tab was created
- assert len(browser_session.tabs) == initial_tab_count + 1
-
- # Check that current page is the new tab
- current_url = await browser_session.get_current_page_url()
- assert f'{base_url}/page2' in current_url
-
- # Check event history for TabCreatedEvent
- event_history = list(browser_session.event_bus.event_history.values())
- created_events = [e for e in event_history if isinstance(e, TabCreatedEvent)]
- assert len(created_events) >= 1
-
async def test_concurrent_tab_operations_via_events(self, browser_session, base_url):
"""Test concurrent tab operations via event system."""
from browser_use.browser.events import NavigateToUrlEvent, SwitchTabEvent
diff --git a/tests/ci/test_controller.py b/tests/ci/test_controller.py
index 3b622439a..7847a81bb 100644
--- a/tests/ci/test_controller.py
+++ b/tests/ci/test_controller.py
@@ -95,22 +95,6 @@ def controller():
class TestControllerIntegration:
"""Integration tests for Controller using actual browser instances."""
- async def test_go_to_url_action(self, controller, browser_session: BrowserSession, base_url):
- """Test that GoToUrlAction navigates to the specified URL and test both state summary methods."""
- # Test successful navigation to a valid page
- action_data = {'go_to_url': GoToUrlAction(url=f'{base_url}/page1', new_tab=False)}
-
- class GoToUrlActionModel(ActionModel):
- go_to_url: GoToUrlAction | None = None
-
- action_model = GoToUrlActionModel(**action_data)
- result = await controller.act(action_model, browser_session)
-
- # Verify the successful navigation result
- assert isinstance(result, ActionResult)
- assert result.extracted_content is not None
- assert f'Navigated to {base_url}' in result.extracted_content
-
async def test_registry_actions(self, controller, browser_session):
"""Test that the registry contains the expected default actions."""
# Check that common actions are registered
@@ -850,32 +834,3 @@ class TestControllerIntegration:
# Verify the actual dropdown selection was made by checking the DOM
selected_value = await page.evaluate("document.getElementById('test-dropdown').value")
assert selected_value == 'option2' # Second Option has value "option2"
-
- async def test_go_to_url_network_error(self, controller, browser_session: BrowserSession):
- """Test that go_to_url handles network errors gracefully instead of throwing hard errors."""
- # Create action model for go_to_url with an invalid domain
- action_data = {'go_to_url': GoToUrlAction(url='https://www.nonexistentdndbeyond.com/', new_tab=False)}
-
- # Create the ActionModel instance
- class GoToUrlActionModel(ActionModel):
- go_to_url: GoToUrlAction | None = None
-
- action_model = GoToUrlActionModel(**action_data)
-
- # Execute the action - should return soft error instead of throwing
- result = await controller.act(action_model, browser_session)
-
- # Verify the result
- assert isinstance(result, ActionResult)
- # The navigation should fail with an error for non-existent domain
-
- # Test that get_state_summary works
- try:
- await browser_session.get_browser_state_summary(cache_clickable_elements_hashes=True)
- assert False, 'Expected throw error when navigating to non-existent page'
- except Exception as e:
- pass
-
- # Test that browser state recovery works after error
- summary = await browser_session.get_browser_state_summary(include_screenshot=False)
- assert summary is not None