Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
tests fixes
Nick Sweeting6fab25c043e4
diff --git a/tests/ci/test_action_parameter_injection.py b/tests/ci/test_action_parameter_injection.pyindex 890e5aace..2d5df651e 100644--- a/tests/ci/test_action_parameter_injection.py+++ b/tests/ci/test_action_parameter_injection.py@@ -6,7 +6,6 @@ import pytestfrom pytest_httpserver import HTTPServerfrom browser_use.browser import BrowserProfile, BrowserSession-from browser_use.dom.views import EnhancedDOMTreeNode, NodeType# Fix for httpserver hanging on shutdown - prevent blocking on socket closesocketserver.ThreadingMixIn.block_on_close = False@@ -81,9 +80,10 @@ class TestBrowserContext:the allowed domains configuration."""# Scenario 1: allowed_domains is None, any URL should be allowed.- from browser_use.browser.security_watchdog import SecurityWatchdogfrom bubus import EventBus-++ from browser_use.browser.security_watchdog import SecurityWatchdog+config1 = BrowserProfile(allowed_domains=None, headless=True, user_data_dir=None)context1 = BrowserSession(browser_profile=config1)event_bus1 = EventBus()@@ -127,12 +127,12 @@ class TestBrowserContext:"""pass # Method was removed from BrowserSession-@pytest.mark.asyncioasync def test_navigate_and_get_current_page(self, browser_session, base_url):"""Test that navigate method changes the URL and get_current_page returns the proper page."""# Navigate to the test pagefrom browser_use.browser.events import NavigateToUrlEvent+event = browser_session.event_bus.dispatch(NavigateToUrlEvent(url=f'{base_url}/'))await event@@ -151,6 +151,7 @@ class TestBrowserContext:"""Test that refresh_page correctly reloads the current page."""# Navigate to the test pagefrom browser_use.browser.events import NavigateToUrlEvent+event = browser_session.event_bus.dispatch(NavigateToUrlEvent(url=f'{base_url}/'))await event@@ -175,6 +176,7 @@ class TestBrowserContext:"""Test that execute_javascript correctly executes JavaScript in the current page."""# Navigate to a test pagefrom browser_use.browser.events import NavigateToUrlEvent+event = browser_session.event_bus.dispatch(NavigateToUrlEvent(url=f'{base_url}/'))await event@@ -196,6 +198,7 @@ class TestBrowserContext:"""Test that get_scroll_info returns the correct scroll position information."""# Navigate to the scroll test pagefrom browser_use.browser.events import NavigateToUrlEvent+event = browser_session.event_bus.dispatch(NavigateToUrlEvent(url=f'{base_url}/scroll_test'))await eventpage = await browser_session.get_current_page()@@ -224,6 +227,7 @@ class TestBrowserContext:"""Test that take_screenshot returns a valid base64 encoded image."""# Navigate to the test pagefrom browser_use.browser.events import NavigateToUrlEvent+event = browser_session.event_bus.dispatch(NavigateToUrlEvent(url=f'{base_url}/'))await event@@ -247,6 +251,7 @@ class TestBrowserContext:"""Test tab creation, switching, and closing operations."""# Navigate to home page in first tabfrom browser_use.browser.events import NavigateToUrlEvent+event = browser_session.event_bus.dispatch(NavigateToUrlEvent(url=f'{base_url}/'))await event@@ -339,6 +344,7 @@ class TestBrowserContext:# Navigate to a test pagefrom browser_use.browser.events import NavigateToUrlEvent+event = browser_session.event_bus.dispatch(NavigateToUrlEvent(url=f'{base_url}/'))await eventdiff --git a/tests/ci/test_agent_multiprocessing.py b/tests/ci/test_agent_multiprocessing.pyindex 7beb51041..685af51cc 100644--- a/tests/ci/test_agent_multiprocessing.py+++ b/tests/ci/test_agent_multiprocessing.py@@ -439,8 +439,12 @@ class TestParallelism:try:await session.start()- # Get browser PID from local_browser_watchdog- initial_browser_pid = session.local_browser_watchdog._subprocess.pid if session.local_browser_watchdog and session.local_browser_watchdog._subprocess else None+ # Get browser PID from private local_browser_watchdog+ initial_browser_pid = (+ session._local_browser_watchdog._subprocess.pid+ if session._local_browser_watchdog and session._local_browser_watchdog._subprocess+ else None+ )# First agentagent1 = Agent(@@ -452,7 +456,11 @@ class TestParallelism:result1 = await agent1.run()# Session should still be alive- current_browser_pid = session.local_browser_watchdog._subprocess.pid if session.local_browser_watchdog and session.local_browser_watchdog._subprocess else None+ current_browser_pid = (+ session._local_browser_watchdog._subprocess.pid+ if session._local_browser_watchdog and session._local_browser_watchdog._subprocess+ else None+ )assert current_browser_pid is not Noneassert current_browser_pid == initial_browser_pid@@ -472,7 +480,11 @@ class TestParallelism:if last_history.model_output and last_history.model_output.action:assert any('done' in action.model_dump(include={'done'}) for action in last_history.model_output.action)# Verify same browser process is still running- final_browser_pid = session.local_browser_watchdog._subprocess.pid if session.local_browser_watchdog and session.local_browser_watchdog._subprocess else None+ final_browser_pid = (+ session._local_browser_watchdog._subprocess.pid+ if session._local_browser_watchdog and session._local_browser_watchdog._subprocess+ else None+ )assert final_browser_pid == initial_browser_pidfinally:diff --git a/tests/ci/test_aria_menu_dropdown.py b/tests/ci/test_aria_menu_dropdown.pyindex f70944909..fd62533a2 100644--- a/tests/ci/test_aria_menu_dropdown.py+++ b/tests/ci/test_aria_menu_dropdown.py@@ -160,6 +160,7 @@ class TestARIAMenuDropdown:# Wait for the page to loadfrom browser_use.browser.events import NavigationCompleteEvent+await browser_session.event_bus.expect(NavigationCompleteEvent, timeout=10.0)# Initialize the DOM state to populate the selector map@@ -225,6 +226,7 @@ class TestARIAMenuDropdown:# Wait for the page to loadfrom browser_use.browser.events import NavigationCompleteEvent+await browser_session.event_bus.expect(NavigationCompleteEvent, timeout=10.0)# Initialize the DOM state to populate the selector map@@ -273,10 +275,10 @@ class TestARIAMenuDropdown:assert 'Filter' in result.extracted_content# Verify the click actually had an effect on the page using CDP- cdp_session = await session.attach_cdp_session()+ cdp_session = await browser_session.get_or_create_cdp_session()result = await cdp_session.cdp_client.send.Runtime.evaluate(params={'expression': "document.getElementById('result').textContent", 'returnByValue': True},- session_id=cdp_session.session_id+ session_id=cdp_session.session_id,)result_text = result.get('result', {}).get('value', '')assert 'Filter' in result_text, f"Expected 'Filter' in result text, got '{result_text}'"@@ -293,6 +295,7 @@ class TestARIAMenuDropdown:# Wait for the page to loadfrom browser_use.browser.events import NavigationCompleteEvent+await browser_session.event_bus.expect(NavigationCompleteEvent, timeout=10.0)# Initialize the DOM state to populate the selector mapdiff --git a/tests/ci/test_browser_event_ScrollEvent.py b/tests/ci/test_browser_event_ScrollEvent.pyindex bd57751eb..5ac7a2e9a 100644--- a/tests/ci/test_browser_event_ScrollEvent.py+++ b/tests/ci/test_browser_event_ScrollEvent.py@@ -117,7 +117,7 @@ class TestScrollActions: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)}+ invalid_scroll_action = {'scroll': ScrollAction(down=True, num_pages=1.0, frame_element_index=999)}result = await controller.act(ScrollActionModel(**invalid_scroll_action), browser_session)# This should fail with error about element not found@@ -126,15 +126,15 @@ class TestScrollActions: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)+ scroll_with_index = ScrollAction(down=True, num_pages=1.0, frame_element_index=5)assert scroll_with_index.down is Trueassert scroll_with_index.num_pages == 1.0- assert scroll_with_index.index == 5+ assert scroll_with_index.frame_element_index == 5scroll_without_index = ScrollAction(down=False, num_pages=0.25)assert scroll_without_index.down is Falseassert scroll_without_index.num_pages == 0.25- assert scroll_without_index.index is None+ assert scroll_without_index.frame_element_index is Noneasync def test_scroll_with_cross_origin_disabled(self, browser_session, base_url):"""Test that scroll works when cross_origin_iframes is disabled."""diff --git a/tests/ci/test_browser_event_TypeTextEvent.py b/tests/ci/test_browser_event_TypeTextEvent.pyindex 5aa786276..645408991 100644--- a/tests/ci/test_browser_event_TypeTextEvent.py+++ b/tests/ci/test_browser_event_TypeTextEvent.py@@ -460,7 +460,7 @@ class TestTypeTextEvent: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+ from browser_use.dom.views import EnhancedDOMTreeNode, NodeTypemock_node = EnhancedDOMTreeNode(element_index=0,@@ -469,7 +469,7 @@ class TestTypeTextEvent:session_id='',frame_id='',target_id='',- node_type=1,+ node_type=NodeType.ELEMENT_NODE,node_name='body',node_value='',attributes={},@@ -531,7 +531,7 @@ class TestTypeTextEvent:)# Create a node with a non-existent index- from browser_use.dom.views import EnhancedDOMTreeNode+ from browser_use.dom.views import EnhancedDOMTreeNode, NodeTypenonexistent_node = EnhancedDOMTreeNode(element_index=99999,@@ -540,7 +540,7 @@ class TestTypeTextEvent:session_id='',frame_id='',target_id='',- node_type=1,+ node_type=NodeType.ELEMENT_NODE,node_name='input',node_value='',attributes={'id': 'nonexistent'},diff --git a/tests/ci/test_browser_event_TypeTextEvent2.py b/tests/ci/test_browser_event_TypeTextEvent2.pyindex 5aa786276..645408991 100644--- a/tests/ci/test_browser_event_TypeTextEvent2.py+++ b/tests/ci/test_browser_event_TypeTextEvent2.py@@ -460,7 +460,7 @@ class TestTypeTextEvent: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+ from browser_use.dom.views import EnhancedDOMTreeNode, NodeTypemock_node = EnhancedDOMTreeNode(element_index=0,@@ -469,7 +469,7 @@ class TestTypeTextEvent:session_id='',frame_id='',target_id='',- node_type=1,+ node_type=NodeType.ELEMENT_NODE,node_name='body',node_value='',attributes={},@@ -531,7 +531,7 @@ class TestTypeTextEvent:)# Create a node with a non-existent index- from browser_use.dom.views import EnhancedDOMTreeNode+ from browser_use.dom.views import EnhancedDOMTreeNode, NodeTypenonexistent_node = EnhancedDOMTreeNode(element_index=99999,@@ -540,7 +540,7 @@ class TestTypeTextEvent:session_id='',frame_id='',target_id='',- node_type=1,+ node_type=NodeType.ELEMENT_NODE,node_name='input',node_value='',attributes={'id': 'nonexistent'},diff --git a/tests/ci/test_browser_session_allowed_domains.py b/tests/ci/test_browser_session_allowed_domains.pyindex bf8e7875c..c1ecde940 100644--- a/tests/ci/test_browser_session_allowed_domains.py+++ b/tests/ci/test_browser_session_allowed_domains.py@@ -6,9 +6,10 @@ class TestUrlAllowlistSecurity:def test_authentication_bypass_prevention(self):"""Test that the URL allowlist cannot be bypassed using authentication credentials."""- from browser_use.browser.security_watchdog import SecurityWatchdogfrom bubus import EventBus-++ from browser_use.browser.security_watchdog import SecurityWatchdog+# Create a context config with a sample allowed domainbrowser_profile = BrowserProfile(allowed_domains=['example.com'], headless=True, user_data_dir=None)browser_session = BrowserSession(browser_profile=browser_profile)@@ -27,9 +28,10 @@ class TestUrlAllowlistSecurity:def test_glob_pattern_matching(self):"""Test that glob patterns in allowed_domains work correctly."""- from browser_use.browser.security_watchdog import SecurityWatchdogfrom bubus import EventBus-++ from browser_use.browser.security_watchdog import SecurityWatchdog+# Test *.example.com pattern (should match subdomains and main domain)browser_profile = BrowserProfile(allowed_domains=['*.example.com'], headless=True, user_data_dir=None)browser_session = BrowserSession(browser_profile=browser_profile)@@ -90,9 +92,10 @@ class TestUrlAllowlistSecurity:def test_glob_pattern_edge_cases(self):"""Test edge cases for glob pattern matching to ensure proper behavior."""- from browser_use.browser.security_watchdog import SecurityWatchdogfrom bubus import EventBus-++ from browser_use.browser.security_watchdog import SecurityWatchdog+# Test with domains containing glob pattern in the middlebrowser_profile = BrowserProfile(allowed_domains=['*.google.com', 'https://wiki.org'], headless=True, user_data_dir=None)browser_session = BrowserSession(browser_profile=browser_profile)diff --git a/tests/ci/test_browser_session_crashed_page_recovery.py b/tests/ci/test_browser_session_crashed_page_recovery.pydeleted file mode 100644index 576b49217..000000000--- a/tests/ci/test_browser_session_crashed_page_recovery.py+++ /dev/null@@ -1,639 +0,0 @@-"""-Comprehensive tests for browser session recovery when pages become unresponsive due to blocking JavaScript.--This test module covers:-1. Permanent blocking JavaScript that never unblocks-2. Transient blocking JavaScript that unblocks after a delay-3. Page recovery mechanisms via CDP force-close-4. Minimal demonstrations of Playwright hanging on blocking JS-5. Multiple concurrent sessions handling blocking pages-"""--import asyncio-import os-import signal-import time-import warnings--import pytest-from playwright.async_api import async_playwright-from pytest_httpserver import HTTPServer-from werkzeug.wrappers import Response--from browser_use.browser.profile import BrowserProfile-from browser_use.browser.session import BrowserSession--# Suppress TargetClosedError warnings during tests-warnings.filterwarnings('ignore', category=RuntimeWarning, message='.*TargetClosedError.*')--[email protected](scope='function')-async def playwright():- """Create a fresh playwright instance for each test to avoid interference"""- async with async_playwright() as p:- yield p---class TestPlaywrightBlockingJavaScript:- """Minimal demonstrations showing that Playwright hangs on blocking JavaScript"""-- async def test_playwright_hangs_on_infinite_loop(self, playwright):- """Demonstrate that Playwright operations hang indefinitely on pages with blocking JavaScript"""-- browser = await playwright.chromium.launch(headless=True)- page = await browser.new_page()-- # Navigate to a page with an infinite loop- print('1. Navigating to page with infinite JavaScript loop...')- try:- await page.goto('data:text/html,<script>while(true){}</script>', wait_until='domcontentloaded', timeout=1000)- except Exception as e:- print(f' Navigation timed out as expected: {type(e).__name__}')- print('2. Navigation complete (page is now blocked)')-- # Try to evaluate simple JavaScript with asyncio timeout- print("3. Attempting page.evaluate('1+1') with 2 second timeout...")- print(' Expected: Should timeout after 2 seconds')- print(' Reality: In some versions, this may hang forever!\n')-- try:- # In older Playwright versions, this could hang forever!- # In newer versions, it may properly timeout- result = await asyncio.wait_for(page.evaluate('1 + 1'), timeout=2.0)- print(f'✅ Result: {result} (Playwright handled it properly)')- assert False, (- 'Playwright is expected to time out here, otherwise congrats, you can remove all the complexity around handling crashed pages!'- )- except TimeoutError:- print('✅ Timed out after 2 seconds as expected because page is crashed (asyncio timeout worked)')- except Exception as e:- print(f'⚠️ Other error: {type(e).__name__}: {e}')- assert False, 'Playwright is expected to time out during all operations on crashed pages, not raise an exception'-- async def test_all_playwright_operations_hang(self, playwright):- """Show that ALL Playwright operations hang on a blocked page"""-- browser = await playwright.chromium.launch(headless=True)- page = await browser.new_page()-- # Navigate to blocking page- try:- await page.goto('data:text/html,<script>while(true){}</script>', wait_until='domcontentloaded', timeout=1500)- except Exception:- pass # Expected timeout- print('✅ Page is now blocked by infinite loop')-- # List of operations that will ALL hang forever- operations = [- ('page.title()', lambda: page.title()),- ('page.content()', lambda: page.content()),- ('page.screenshot()', lambda: page.screenshot()),- ("page.evaluate('1')", lambda: page.evaluate('1')),- ("page.query_selector('body')", lambda: page.query_selector('body')),- ]-- for op_name, op_func in operations:- print(f'\n🧪 Testing {op_name} with 1s timeout...')- try:- await asyncio.wait_for(op_func(), timeout=1.0)- print(f'✅ {op_name} succeeded')- except TimeoutError:- print(f'⏱️ {op_name} timed out after 1s (expected for blocked page)')- except Exception as e:- print(f'❌ {op_name} failed: {type(e).__name__}')-- async def test_transient_vs_permanent_blocking(self, playwright):- """Demonstrate the difference between transient and permanent blocking"""-- browser = await playwright.chromium.launch(headless=True)-- # Test 1: Transient blocking (blocks for 2 seconds)- print('=== Test 1: Transient Blocking ===')- page1 = await browser.new_page()-- transient_html = """- <html><body>- <h1>Transient Block</h1>- <script>- const start = Date.now();- while (Date.now() - start < 2000) {} // Block for 2 seconds- document.body.innerHTML += '<p>Now responsive!</p>';- </script>- </body></html>- """-- await page1.goto(f'data:text/html,{transient_html}')- print('Page loaded with transient blocking script')-- # Wait for block to end- await asyncio.sleep(2.5)-- # This should work now- try:- result = await asyncio.wait_for(page1.evaluate('2 + 2'), timeout=1.0)- print(f'✅ Transient block ended, evaluate worked: {result}')- except TimeoutError:- print('❌ Still blocked after transient period')-- # Test 2: Permanent blocking- print('\n=== Test 2: Permanent Blocking ===')- page2 = await browser.new_page()-- try:- await page2.goto('data:text/html,<script>while(true){}</script>', wait_until='domcontentloaded', timeout=1000)- except Exception:- pass # Expected timeout- print('Page loaded with permanent blocking script')-- # This will always timeout- try:- await asyncio.wait_for(page2.evaluate('3 + 3'), timeout=1.0)- print('❌ Should not succeed on permanently blocked page')- except TimeoutError:- print('✅ Permanently blocked page timed out as expected')-- del page1, page2---def slow_response_handler(request):- time.sleep(5) # 5 second delay to guaranteed exceed the 3 second max timeout- return Response("""<html><body>Finally loaded!</body></html>""", content_type='text/html')---class TestBrowserSessionRecovery:- """Test browser-use's recovery mechanisms for unresponsive pages"""-- @pytest.fixture(scope='function')- async def browser_session(self):- """Create a browser session for testing - no playwright parameter to ensure fresh instance"""- session = BrowserSession(- browser_profile=BrowserProfile(- headless=True,- keep_alive=False, # Don't keep alive between tests- default_navigation_timeout=30_000,- minimum_wait_page_load_time=0.1, # Short wait for testing- user_data_dir=None, # No user data dir to avoid race conditions with other tests- ),- # Don't pass playwright - let each session create its own- )- await session.start()- yield session- # Simple cleanup - just kill the browser- await session.kill()-- @pytest.mark.timeout(60) # 60 second timeout- @pytest.mark.skip(reason='Crash recovery not yet implemented in event-based architecture')- async def test_permanent_blocking_forces_cdp_recovery(self, httpserver: HTTPServer, browser_session: BrowserSession):- """Test critical behaviors:- 1. Crashed pages should never crash the entire agent- 2. Pages should be retried once on the original URL- 3. Then reset to about:blank to prevent browser from becoming unusable- """-- # Create a page with permanent blocking JavaScript- httpserver.expect_request('/permanent-block').respond_with_data(- """- <html>- <head><title>Permanent Block</title></head>- <body>- <h1>This page blocks forever</h1>- <script>- console.log('Starting infinite loop...');- while (true) {- // Infinite loop - page will never recover- }- </script>- </body>- </html>- """,- content_type='text/html',- )-- # Track recovery attempts- cdp_force_close_count = 0- reopen_attempts = 0-- # Note: These tests need to access internal state for testing recovery mechanisms- # This is acceptable for testing crash recovery behavior- assert hasattr(browser_session, 'cdp_client'), 'CDP client must be available for crash recovery testing'- original_new_cdp_session = None # Will be set if browser_context is available- if hasattr(browser_session, 'browser_context') and browser_session.browser_context:- original_new_cdp_session = browser_session.browser_context.new_cdp_session- original_goto = None-- async def track_cdp_force_close(page):- nonlocal cdp_force_close_count- cdp_force_close_count += 1- print(f'🔨 CDP force-close recovery attempted (count: {cdp_force_close_count})')- return await original_new_cdp_session(page)-- async def track_goto(page_instance, url, **kwargs):- nonlocal reopen_attempts- if 'permanent-block' in url and cdp_force_close_count > 0:- reopen_attempts += 1- print(f'🔄 Reopening blocked URL attempt #{reopen_attempts}: {url[:50]}...')- # Get the original goto method for this specific page- original = page_instance.__class__.goto- return await original(page_instance, url, **kwargs)-- if hasattr(browser_session, 'browser_context') and browser_session.browser_context:- browser_session.browser_context.new_cdp_session = track_cdp_force_close # type: ignore[assignment]-- # Navigate to the permanently blocking page- blocking_url = httpserver.url_for('/permanent-block')- print(f'1️⃣ Navigating to blocking page: {blocking_url}')- try:- from browser_use.browser.events import NavigateToUrlEvent- event = browser_session.event_bus.dispatch(NavigateToUrlEvent(url=blocking_url))- await event- except Exception as e:- # Expected - the page is unresponsive- print(f' Navigation raised expected error: {type(e).__name__}')- await asyncio.sleep(0.5) # Let blocking script start-- # We need to patch goto on any new pages created during recovery- pages_patched = set()-- def patch_page_goto(page):- if page not in pages_patched:- pages_patched.add(page)- # Create a bound method that properly tracks goto calls- import types-- page.goto = types.MethodType(track_goto, page)-- # Note: Cannot patch page directly without internal access- # This test's page patching functionality is skipped- pass # patch_page_goto(page) - skipped-- # Also patch browser context's new_page to catch pages created during recovery- original_new_page = browser_session.browser_context.new_page-- async def patched_new_page():- new_page = await original_new_page()- patch_page_goto(new_page)- return new_page-- browser_session.browser_context.new_page = patched_new_page # type: ignore[assignment]-- # Try to take a screenshot - should trigger recovery- print('\n2️⃣ Attempting screenshot on blocked page (should trigger recovery)...')- try:- # Use a longer timeout since recovery takes time- from browser_use.browser.events import ScreenshotEvent- event = browser_session.event_bus.dispatch(ScreenshotEvent())- result = await asyncio.wait_for(event.event_result(), timeout=40.0)- screenshot = result.get('screenshot') if result else None- assert screenshot is not None, 'Screenshot should succeed after recovery'- assert len(screenshot) > 10, 'Screenshot should have content'- except TimeoutError:- pytest.fail('Screenshot timed out after 40 seconds - recovery may have failed')-- # Verify current page is on about:blank after recovery- current_url = await browser_session.get_current_page_url()- print(f'\n3️⃣ Current URL after recovery: {current_url}')- assert current_url == 'about:blank', 'Page should be on about:blank after recovery'-- # Verify recovery behavior- print('\n📊 Recovery statistics:')- print(f' - CDP force-close attempts: {cdp_force_close_count}')- print(f' - Reopen attempts: {reopen_attempts}')-- assert cdp_force_close_count >= 1, 'CDP force-close should have been attempted at least once'- assert reopen_attempts == 1, 'Should attempt to reopen the URL exactly once'-- # Verify browser still works by navigating to a normal page- print('\n4️⃣ Testing browser still works with normal pages...')- httpserver.expect_request('/normal').respond_with_data(- '<html><body><h1>Normal Page</h1></body></html>',- content_type='text/html',- )-- from browser_use.browser.events import NavigateToUrlEvent- event = browser_session.event_bus.dispatch(NavigateToUrlEvent(url=httpserver.url_for('/normal')))- await event-- # Take a screenshot to verify browser works- state2 = await browser_session.get_browser_state_summary()- assert state2.screenshot is not None- assert len(state2.screenshot) > 100, 'Browser should still work after recovery'-- print('\n✅ All critical behaviors verified:')- print(' - Crashed page did not crash the entire agent')- print(' - Page was retried once on the original URL')- print(' - Page was reset to about:blank to remain usable')-- async def test_transient_blocking_recovers_naturally(self, httpserver: HTTPServer, browser_session: BrowserSession):- """Test that transiently blocked pages recover without intervention"""- # Create page that blocks for 3 seconds- httpserver.expect_request('/transient-block').respond_with_data(- """<html><body>- <h1>Blocking temporarily...</h1>- <script>- const start = Date.now();- while (Date.now() - start < 3000) {}- document.body.innerHTML += '<p>Page recovered!</p>';- </script>- </body></html>""",- content_type='text/html',- )-- print('1️⃣ Starting test_transient_blocking_recovers_naturally')- # Note: browser_context and browser are internal attributes- print(' Browser session started')-- try:- print('2️⃣ Navigating to transient-block page...')- from browser_use.browser.events import NavigateToUrlEvent- event = browser_session.event_bus.dispatch(NavigateToUrlEvent(url=httpserver.url_for('/transient-block')))- await event- print(' Navigation completed')- except Exception as e:- print(f' Navigation failed: {type(e).__name__}: {e}')- raise-- print('3️⃣ Waiting 3.5 seconds for block to end...')- await asyncio.sleep(3.5) # Wait for block to end- print(' Wait completed')-- # Check browser state before screenshot- print('4️⃣ Checking browser state before screenshot...')- print(f' Browser connected: {browser_session.browser.is_connected() if browser_session.browser else "No browser"}')- print(f' Browser context: {browser_session.browser_context}')-- # Should work without recovery- print('5️⃣ Taking screenshot...')- try:- from browser_use.browser.events import ScreenshotEvent- event = browser_session.event_bus.dispatch(ScreenshotEvent())- result = await event.event_result()- screenshot = result.get('screenshot') if result else None- print(f' Screenshot taken: {len(screenshot) if screenshot else 0} bytes')- except Exception as e:- print(f' Screenshot failed: {type(e).__name__}: {e}')- raise-- assert screenshot is not None and len(screenshot) > 100-- print('6️⃣ Checking page recovery...')- # Note: Cannot get page content directly without internal access- # Verify recovery through observable behavior (URL)- current_url = await browser_session.get_current_page_url()- print(f' Current URL after recovery: {current_url}')- # Recovery should have navigated away from blocking page- assert '/transient-block' not in current_url- print('✅ Test completed successfully')-- async def test_multiple_blocking_recovery_cycles(self, httpserver: HTTPServer, browser_session: BrowserSession):- """Test multiple cycles of blocking and recovery"""-- # Create pages- httpserver.expect_request('/block1').respond_with_data(- '<html><body><script>while(true){}</script></body></html>',- content_type='text/html',- )-- httpserver.expect_request('/normal1').respond_with_data(- '<html><body><h1>Normal Page 1</h1></body></html>',- content_type='text/html',- )-- httpserver.expect_request('/block2').respond_with_data(- '<html><body><script>while(true){}</script></body></html>',- content_type='text/html',- )-- httpserver.expect_request('/normal2').respond_with_data(- '<html><body><h1>Normal Page 2</h1></body></html>',- content_type='text/html',- )-- # First blocking cycle- print('=== Cycle 1: Navigate to blocking page ===')- try:- from browser_use.browser.events import NavigateToUrlEvent- event = browser_session.event_bus.dispatch(NavigateToUrlEvent(url=httpserver.url_for('/block1')))- await event- except RuntimeError as e:- # Expected - navigation to blocking page will fail- print(f' Navigation to blocking page failed as expected: {type(e).__name__}')-- # Try screenshot (may trigger recovery)- try:- from browser_use.browser.events import ScreenshotEvent- event = browser_session.event_bus.dispatch(ScreenshotEvent())- await event- except Exception:- pass-- # Navigate to normal page- print('=== Cycle 1: Navigate to normal page ===')- from browser_use.browser.events import NavigateToUrlEvent- event = browser_session.event_bus.dispatch(NavigateToUrlEvent(url=httpserver.url_for('/normal1')))- await event- # Verify navigation succeeded by checking URL- current_url = await browser_session.get_current_page_url()- assert '/normal1' in current_url-- # Second blocking cycle- print('=== Cycle 2: Navigate to another blocking page ===')- try:- from browser_use.browser.events import NavigateToUrlEvent- event = browser_session.event_bus.dispatch(NavigateToUrlEvent(url=httpserver.url_for('/block2')))- await event- except RuntimeError as e:- # Expected - navigation to blocking page will fail- print(f' Navigation to blocking page failed as expected: {type(e).__name__}')-- # Try screenshot again- try:- from browser_use.browser.events import ScreenshotEvent- event = browser_session.event_bus.dispatch(ScreenshotEvent())- await event- except Exception:- pass-- # Navigate to second normal page- print('=== Cycle 2: Navigate to another normal page ===')- from browser_use.browser.events import NavigateToUrlEvent- event = browser_session.event_bus.dispatch(NavigateToUrlEvent(url=httpserver.url_for('/normal2')))- await event- # Verify navigation succeeded by checking URL- current_url = await browser_session.get_current_page_url()- assert '/normal2' in current_url-- print('✅ Multiple recovery cycles completed successfully')-- async def test_navigation_timeout_warning_appears(self, httpserver: HTTPServer, browser_session: BrowserSession):- """Test that navigation handles slow page loads without hanging"""- # Set up a page that takes 4 seconds to load- httpserver.expect_request('/delayed').respond_with_handler(slow_response_handler)-- # Navigate with a timeout - navigation should complete even though page is slow- start_time = time.time()- from browser_use.browser.events import NavigateToUrlEvent- event = browser_session.event_bus.dispatch(NavigateToUrlEvent(url=httpserver.url_for('/delayed')))- await event- elapsed = time.time() - start_time-- # Navigation should complete within a reasonable time (not hang for full 4 seconds)- assert elapsed < 3.5, f'Navigation took too long: {elapsed:.2f}s'-- # Browser should still be functional - verify by getting URL- current_url = await browser_session.get_current_page_url()- assert current_url is not None-- # Verify we can navigate to another page- httpserver.expect_request('/normal').respond_with_data('<html><body>Normal</body></html>')- from browser_use.browser.events import NavigateToUrlEvent- event = browser_session.event_bus.dispatch(NavigateToUrlEvent(url=httpserver.url_for('/normal')))- await event- url2 = await browser_session.get_current_page_url()- assert 'normal' in url2-- async def test_browser_crash_throws_hard_error_no_restart(self, browser_session: BrowserSession):- """Test that browser crashes throw hard errors instead of restarting the browser"""- # Get the browser process PID from local_browser_watchdog- browser_pid = None- if hasattr(browser_session, 'local_browser_watchdog') and browser_session.local_browser_watchdog:- if hasattr(browser_session.local_browser_watchdog, '_subprocess') and browser_session.local_browser_watchdog._subprocess:- browser_pid = browser_session.local_browser_watchdog._subprocess.pid- assert browser_pid is not None, 'Browser PID must be available'-- print(f'1️⃣ Browser PID: {browser_pid}')-- # Force kill the browser process to simulate a hard crash- print('2️⃣ Killing browser process to simulate crash...')- try:- os.kill(browser_pid, signal.SIGKILL)- except ProcessLookupError:- # Process might have already exited- pass-- # Wait a bit for the process to die- await asyncio.sleep(1)-- # Try to use the browser session - should raise error (TargetClosedError or RuntimeError)- print('3️⃣ Attempting to use crashed browser session...')- with pytest.raises(Exception) as exc_info:- from browser_use.browser.events import NavigateToUrlEvent- event = browser_session.event_bus.dispatch(NavigateToUrlEvent(url='about:blank'))- await event-- # Verify the error indicates browser disconnection/crash- error_msg = str(exc_info.value).lower()- error_type = type(exc_info.value).__name__- print(f'4️⃣ Got expected error ({error_type}): {error_msg}')- assert (- 'closed' in error_msg or 'crash' in error_msg or 'cannot be recovered' in error_msg or 'connection lost' in error_msg- ), f'Error message should indicate browser crash/closure, got: {error_msg}'-- # Verify browser was NOT restarted (PID should still be the same dead one)- assert browser_session.browser_pid == browser_pid or browser_session.browser_pid is None, (- 'Browser PID should not change (no restart should occur)'- )-- print('✅ Browser crash correctly threw hard error without restarting')-- async def test_singleton_lock_error_throws_hard_error(self, browser_session: BrowserSession):- """Test that SingletonLock errors throw hard error instead of restarting"""- # Create a conflicting user data directory scenario- import tempfile- from pathlib import Path-- # Create a temp directory for user data- temp_dir = tempfile.mkdtemp(prefix='browseruse-test-')-- # Create a fake SingletonLock file to simulate conflict- singleton_lock = Path(temp_dir) / 'SingletonLock'- singleton_lock.write_text('fake-lock')-- # Create a session with this directory- session = BrowserSession(browser_profile=BrowserProfile(user_data_dir=temp_dir, headless=True))-- # Modify the Chrome launch args to trigger SingletonLock error- original_args = session.browser_profile.args.copy()- # Add an arg that will cause Chrome to exit with SingletonLock error- session.browser_profile.args.append('--no-sandbox')- session.browser_profile.args.append('--disable-setuid-sandbox')-- print(f'1️⃣ Attempting to launch browser with potentially conflicting user_data_dir: {temp_dir}')-- # Note: This test is checking that IF a SingletonLock error occurs,- # it throws a hard error instead of restarting. The actual error might- # not always occur depending on the system state.- try:- await session.start()- # If it succeeds, that's OK - we can't reliably trigger SingletonLock- print('2️⃣ Browser launched successfully (SingletonLock error did not occur)')- await session.kill()- except RuntimeError as e:- # If we get a RuntimeError with SingletonLock, that's what we want- error_msg = str(e)- print(f'2️⃣ Got expected error: {error_msg}')- if 'SingletonLock' in error_msg:- print('✅ SingletonLock error correctly threw hard error without restart')- else:- # Some other RuntimeError - re-raise it- raise- except Exception as e:- # Unexpected error type- print(f'2️⃣ Got unexpected error type {type(e).__name__}: {e}')- raise- finally:- # Cleanup- import shutil-- try:- shutil.rmtree(temp_dir, ignore_errors=True)- except Exception:- pass---async def test_multiple_sessions_with_blocking_pages(httpserver: HTTPServer):- """Test multiple browser sessions with blocking pages simultaneously"""- httpserver.expect_request('/infinite-loop').respond_with_data(- """<html><body><script>while(true){}</script></body></html>""",- content_type='text/html',- )-- sessions = []- for i in range(3):- session = BrowserSession(- browser_profile=BrowserProfile(- headless=True,- keep_alive=False,- default_navigation_timeout=6000, # 5 second timeout- user_data_dir=None,- )- )- sessions.append(session)-- try:- await asyncio.gather(*[s.start() for s in sessions])-- # Navigate to blocking pages - should handle without crashing- nav_tasks = []- for s in sessions:- nav_tasks.append(s.navigate(httpserver.url_for('/infinite-loop'), timeout_ms=5000))-- # All navigations should complete within 5s (they may fail but shouldn't hang forever)- results = await asyncio.gather(*nav_tasks, return_exceptions=True)-- # Check that navigations completed (with or without errors)- for i, result in enumerate(results):- if isinstance(result, Exception):- print(f'Session {i} navigation failed as expected: {type(result).__name__}')-- # Sessions should still be functional, test by creating a new page and executing JS- for i, session in enumerate(sessions):- assert await (await session.browser_context.new_page()).evaluate('1+1') == 2, f'Session {i} page failed to execute JS'-- finally:- # Kill sessions with exception handling to avoid cascade failures- results = await asyncio.gather(*[s.kill() for s in sessions], return_exceptions=True)- # Log any exceptions but don't fail the test- for i, result in enumerate(results):- if isinstance(result, Exception):- print(f'Warning: Session {i} kill raised exception: {type(result).__name__}: {result}')diff --git a/tests/ci/test_browser_session_downloads_simple.py b/tests/ci/test_browser_session_downloads_simple.pyindex 384baf26f..0cdc13fb0 100644--- a/tests/ci/test_browser_session_downloads_simple.py+++ b/tests/ci/test_browser_session_downloads_simple.py@@ -115,28 +115,28 @@ async def test_browser_use_download_with_http_server(http_server):</body></html>"""-+# Set content using CDP- cdp_session = await session.attach_cdp_session()+ cdp_session = await browser_session.get_or_create_cdp_session()await cdp_session.cdp_client.send.Page.setDocumentContent(- params={'frameId': cdp_session.cdp_client.page_frame_id, 'html': html_content},- session_id=cdp_session.session_id+ params={'frameId': cdp_session.cdp_client.page_frame_id, 'html': html_content}, session_id=cdp_session.session_id)# Wait a moment for DOM to be readyawait asyncio.sleep(0.5)# Click the download link using events- state = await session.get_browser_state_summary()+ state = await browser_session.get_browser_state_summary()download_link = Nonefor idx, element in state.dom_state.selector_map.items():if element.attributes.get('id') == 'download-link':download_link = elementbreak-- assert download_link is not None, "Download link not found"++ assert download_link is not None, 'Download link not found'from browser_use.browser.events import ClickElementEvent- click_event = session.event_bus.dispatch(ClickElementEvent(node=download_link))++ click_event = browser_session.event_bus.dispatch(ClickElementEvent(node=download_link))await click_event# Wait for the DownloadsWatchdog to process the download by expecting the FileDownloadedEventdiff --git a/tests/ci/test_browser_session_element_cache.py b/tests/ci/test_browser_session_element_cache.pyindex 2e26f7bdb..801e7adc7 100644--- a/tests/ci/test_browser_session_element_cache.py+++ b/tests/ci/test_browser_session_element_cache.py@@ -66,8 +66,9 @@ async def browser_session():headless=True,))- async with session:- yield session+ await session.start()+ yield session+ await session.stop()@pytest.fixturediff --git a/tests/ci/test_browser_session_file_uploads.py b/tests/ci/test_browser_session_file_uploads.pyindex d028ace87..ba9075caa 100644--- a/tests/ci/test_browser_session_file_uploads.py+++ b/tests/ci/test_browser_session_file_uploads.py@@ -99,6 +99,7 @@ class TestBrowserSessionFileUploads:await browser_session.start()# Navigate to upload page using eventfrom browser_use.browser.events import NavigateToUrlEvent, NavigationCompleteEvent+event = browser_session.event_bus.dispatch(NavigateToUrlEvent(url=test_server.url_for('/upload')))await event# Wait for page to load@@ -207,6 +208,7 @@ class TestBrowserSessionFileUploads:await browser_session.start()# Navigate to traversal page using eventfrom browser_use.browser.events import NavigateToUrlEvent, NavigationCompleteEvent+event = browser_session.event_bus.dispatch(NavigateToUrlEvent(url=test_server.url_for('/traversal')))await event# Wait for page to load@@ -273,7 +275,8 @@ class TestBrowserSessionFileUploads:test_server.expect_request('/limits').respond_with_data(html, content_type='text/html')await browser_session.start()# Navigate to limits page using event- from browser_use.browser.events import NavigateToUrlEvent, NavigationCompleteEvent+ from browser_use.browser.events import NavigateToUrlEvent, NavigationCompleteEvent+event = browser_session.event_bus.dispatch(NavigateToUrlEvent(url=test_server.url_for('/limits')))await event# Wait for page to loaddiff --git a/tests/ci/test_browser_session_viewport_and_proxy.py b/tests/ci/test_browser_session_viewport_and_proxy.pyindex 678929e81..b5f01d317 100644--- a/tests/ci/test_browser_session_viewport_and_proxy.py+++ b/tests/ci/test_browser_session_viewport_and_proxy.py@@ -65,9 +65,9 @@ async def test_window_size_with_real_browser():height: window.innerHeight};})()""",- 'returnByValue': True+ 'returnByValue': True,},- session_id=cdp_session.session_id+ session_id=cdp_session.session_id,)video_size = result.get('result', {}).get('value', {})@@ -80,9 +80,9 @@ async def test_window_size_with_real_browser():height: window.innerHeight}})()""",- 'returnByValue': True+ 'returnByValue': True,},- session_id=cdp_session.session_id+ session_id=cdp_session.session_id,)actual_size = result2.get('result', {}).get('value', {})diff --git a/tests/ci/test_browser_watchdog_crash.py b/tests/ci/test_browser_watchdog_crash.pyindex ea5d12501..a0cd541d7 100644--- a/tests/ci/test_browser_watchdog_crash.py+++ b/tests/ci/test_browser_watchdog_crash.py@@ -170,3 +170,150 @@ async def test_crash_watchdog_lifecycle():await asyncio.sleep(0.1) # Give it a moment to clean upif session._crash_watchdog._monitoring_task:assert session._crash_watchdog._monitoring_task.done()+++async def test_infinite_loop_page_blocking():+ """Test that pages with infinite JavaScript loops are detected as unresponsive."""+ from pytest_httpserver import HTTPServer++ # Create HTTP server with blocking page+ httpserver = HTTPServer()+ httpserver.start()++ # Add route that serves permanently blocking JavaScript+ httpserver.expect_request('/infinite-loop').respond_with_data(+ '<html><body><h1>Loading...</h1><script>while(true){}</script></body></html>', content_type='text/html'+ )++ profile = BrowserProfile(headless=True)+ session = BrowserSession(browser_profile=profile)++ try:+ # Start browser+ session.event_bus.dispatch(BrowserStartEvent())+ await session.event_bus.expect(BrowserConnectedEvent, timeout=5.0)++ # Navigate to blocking page+ blocking_url = httpserver.url_for('/infinite-loop')+ session.event_bus.dispatch(NavigateToUrlEvent(url=blocking_url))++ # The navigation should timeout or trigger an error+ # We don't expect NavigationCompleteEvent since the page blocks+ await asyncio.sleep(2) # Give it time to detect the issue++ # Try to interact with the page via CDP - should still work at protocol level+ cdp_session = await session.get_or_create_cdp_session()++ # CDP commands should still work even if page is blocked+ version_result = await session.cdp_client.send.Browser.getVersion()+ assert version_result is not None++ # Close the blocking tab to recover+ await session.cdp_client.send.Target.closeTarget(params={'targetId': cdp_session.target_id})++ finally:+ httpserver.stop()+ session.event_bus.dispatch(BrowserStopEvent())+ await asyncio.sleep(0.5)+++async def test_transient_blocking_recovery():+ """Test recovery from temporarily blocking JavaScript."""+ from pytest_httpserver import HTTPServer++ httpserver = HTTPServer()+ httpserver.start()++ # Page that blocks for 1 second then recovers+ httpserver.expect_request('/transient-block').respond_with_data(+ """<html><body>+ <h1 id="status">Blocking...</h1>+ <script>+ const start = Date.now();+ while (Date.now() - start < 1000) {} // Block for 1 second+ document.getElementById('status').textContent = 'Recovered!';+ </script>+ </body></html>""",+ content_type='text/html',+ )++ profile = BrowserProfile(headless=True)+ session = BrowserSession(browser_profile=profile)++ try:+ # Start browser+ session.event_bus.dispatch(BrowserStartEvent())+ await session.event_bus.expect(BrowserConnectedEvent, timeout=5.0)++ # Navigate to transiently blocking page+ url = httpserver.url_for('/transient-block')+ session.event_bus.dispatch(NavigateToUrlEvent(url=url))++ # Wait for the blocking to end+ await asyncio.sleep(2)++ # Verify page recovered and we can interact with it+ cdp_session = await session.get_or_create_cdp_session()+ result = await session.cdp_client.send.Runtime.evaluate(+ params={'expression': 'document.getElementById("status").textContent', 'returnByValue': True},+ session_id=cdp_session.session_id,+ )++ status_text = result.get('result', {}).get('value', '')+ assert status_text == 'Recovered!', f"Expected 'Recovered!' but got '{status_text}'"++ finally:+ httpserver.stop()+ session.event_bus.dispatch(BrowserStopEvent())+ await asyncio.sleep(0.5)+++async def test_browser_process_kill_detection():+ """Test that killing the browser process is detected."""+ import os+ import signal++ profile = BrowserProfile(headless=True)+ session = BrowserSession(browser_profile=profile)++ try:+ # Start browser+ session.event_bus.dispatch(BrowserStartEvent())+ await session.event_bus.expect(BrowserConnectedEvent, timeout=5.0)++ # Get browser process PID+ browser_pid = None+ if session._local_browser_watchdog and session._local_browser_watchdog._subprocess:+ browser_pid = session._local_browser_watchdog._subprocess.pid++ if browser_pid:+ # Kill the browser process+ try:+ os.kill(browser_pid, signal.SIGKILL)+ except ProcessLookupError:+ pass # Process might already be gone++ # Wait for crash detection+ try:+ error_event = cast(+ BrowserErrorEvent,+ await session.event_bus.expect(+ BrowserErrorEvent,+ predicate=lambda e: 'disconnect' in cast(BrowserErrorEvent, e).message.lower(),+ timeout=5.0,+ ),+ )+ assert error_event is not None+ except TimeoutError:+ # Crash detection might not trigger in all environments+ pass++ finally:+ # Force cleanup+ try:+ await session.kill()+ except Exception:+ passdiff --git a/tests/ci/test_browser_watchdog_downloads.py b/tests/ci/test_browser_watchdog_downloads.pyindex 8ec529540..0f3813492 100644--- a/tests/ci/test_browser_watchdog_downloads.py+++ b/tests/ci/test_browser_watchdog_downloads.py@@ -117,10 +117,12 @@ async def test_downloads_watchdog_file_detection(download_test_server):# Navigate to test pagetest_url = download_test_server.url_for('/')- await session.navigate(test_url)+ await session.event_bus.dispatch(NavigateToUrlEvent(url=test_url))# Click download link to trigger download- await session.click('a[href="/download/test.pdf"]')+ await session.event_bus.dispatch(+ ClickElementEvent(element_node=session.get_element_node('a[href="/download/test.pdf"]'))+ )# Wait for download to completeawait asyncio.sleep(2.0)diff --git a/tests/ci/test_multi_websocket.py b/tests/ci/test_multi_websocket.pydeleted file mode 100644index 8730697f9..000000000--- a/tests/ci/test_multi_websocket.py+++ /dev/null@@ -1,196 +0,0 @@-"""Test multi-WebSocket CDP connections."""--import logging--import pytest-from pytest_httpserver import HTTPServer--from browser_use.browser.session import BrowserSession--# Set up logging-logging.basicConfig(level=logging.DEBUG)-logger = logging.getLogger(__name__)---def test_html(httpserver: HTTPServer):- """Set up test HTML pages."""- # Main page HTML- main_html = """- <!DOCTYPE html>- <html>- <head><title>Main Page</title></head>- <body>- <h1 id="main-title">Main Page</h1>- <button id="test-button">Click Me</button>- <a href="/page2.html" target="_blank">Open Page 2</a>- </body>- </html>- """-- # Second page HTML- page2_html = """- <!DOCTYPE html>- <html>- <head><title>Page 2</title></head>- <body>- <h1 id="page2-title">Page 2</h1>- <input id="test-input" type="text" placeholder="Enter text">- </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('/')---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')-- # 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')-- finally:- await browser_session.kill()---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')-- # 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)-- # 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- )- 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- )- assert result2['result']['value'] == 'Page 2'-- logger.info('✅ Multi-WebSocket: Each target has its own WebSocket connection')-- finally:- await browser_session.kill()---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_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)- 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')-- finally:- await browser_session.kill()---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_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)-- # 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')-- finally:- await browser_session.kill()---if __name__ == '__main__':- # Run with: python -m pytest tests/ci/test_multi_websocket.py -v- pytest.main([__file__, '-v', '-s'])diff --git a/tests/ci/test_navigation_events.py b/tests/ci/test_navigation_events.pyindex 51b8c627b..19c15f1f3 100644--- a/tests/ci/test_navigation_events.py+++ b/tests/ci/test_navigation_events.py@@ -219,7 +219,7 @@ async def test_navigation_events_history_pushstate(httpserver):# Test 1: history.pushState (should NOT trigger NavigationCompleteEvent)push_button_found = False- for idx, element in state.selector_map.items():+ for idx, element in state.dom_state.selector_map.items():if hasattr(element, 'attributes') and element.attributes.get('id') == 'push-btn':push_button_found = Trueclick_element = await session.get_dom_element_by_index(idx)@@ -238,7 +238,7 @@ async def test_navigation_events_history_pushstate(httpserver):state = await session.get_browser_state_summary()same_tab_link_found = False- for idx, element in state.selector_map.items():+ for idx, element in state.dom_state.selector_map.items():if hasattr(element, 'attributes') and element.attributes.get('id') == 'same-tab-link':same_tab_link_found = Trueclick_element = await session.get_dom_element_by_index(idx)@@ -267,7 +267,7 @@ async def test_navigation_events_history_pushstate(httpserver):state = await session.get_browser_state_summary()new_tab_link_found = False- for idx, element in state.selector_map.items():+ for idx, element in state.dom_state.selector_map.items():if hasattr(element, 'attributes') and element.attributes.get('id') == 'new-tab-link':new_tab_link_found = True# Use new_tab=True parameter to properly trigger new tab behavior@@ -364,7 +364,7 @@ async def test_navigation_events_link_clicks(httpserver):state = await session.get_browser_state_summary()same_tab_link_found = False- for idx, element in state.selector_map.items():+ for idx, element in state.dom_state.selector_map.items():if hasattr(element, 'attributes') and element.attributes.get('id') == 'same-tab-link':same_tab_link_found = Trueclick_element = await session.get_dom_element_by_index(idx)@@ -393,7 +393,7 @@ async def test_navigation_events_link_clicks(httpserver):state = await session.get_browser_state_summary()new_tab_link_found = False- for idx, element in state.selector_map.items():+ for idx, element in state.dom_state.selector_map.items():if hasattr(element, 'attributes') and element.attributes.get('id') == 'new-tab-link':new_tab_link_found = Trueclick_element = await session.get_dom_element_by_index(idx)@@ -421,7 +421,7 @@ async def test_navigation_events_link_clicks(httpserver):state = await session.get_browser_state_summary()js_link_found = False- for idx, element in state.selector_map.items():+ for idx, element in state.dom_state.selector_map.items():if hasattr(element, 'attributes') and element.attributes.get('id') == 'js-navigation':js_link_found = Trueclick_element = await session.get_dom_element_by_index(idx)diff --git a/tests/ci/test_registry.py b/tests/ci/test_registry.pyindex 1e0dd7640..44ba7164c 100644--- a/tests/ci/test_registry.py+++ b/tests/ci/test_registry.py@@ -21,7 +21,6 @@ from pytest_httpserver.httpserver import HandlerTypefrom browser_use.agent.views import ActionResultfrom browser_use.browser import BrowserSessionfrom browser_use.browser.profile import BrowserProfile-from browser_use.browser.types import Pagefrom browser_use.controller.registry.service import Registryfrom browser_use.controller.registry.views import ActionModel as BaseActionModelfrom browser_use.controller.views import (@@ -107,6 +106,7 @@ async def browser_session(base_url):)await browser_session.start()from browser_use.browser.events import NavigateToUrlEvent+browser_session.event_bus.dispatch(NavigateToUrlEvent(url=f'{base_url}/test'))await asyncio.sleep(0.5) # Wait for navigationyield browser_session@@ -140,6 +140,7 @@ class TestActionRegistryParameterPatterns:# Navigate to test page firstfrom browser_use.browser.events import NavigateToUrlEvent+event = browser_session.event_bus.dispatch(NavigateToUrlEvent(url=f'{base_url}/test', new_tab=True))await event@@ -151,48 +152,6 @@ class TestActionRegistryParameterPatterns:assert 'Text: hello, URL:' in result.extracted_contentassert base_url in result.extracted_content- async def test_page_parameter_injection(self, registry, browser_session, base_url):- """Test action with direct Page parameter injection"""-- @registry.action('Action with page parameter')- async def action_with_page(text: str, page: Page):- title = await page.title()- return ActionResult(extracted_content=f'Text: {text}, Page Title: {title}')-- # Navigate to test page first- from browser_use.browser.events import NavigateToUrlEvent- event = browser_session.event_bus.dispatch(NavigateToUrlEvent(url=f'{base_url}/test', new_tab=True))- await event-- # Test execution- result = await registry.execute_action('action_with_page', {'text': 'hello'}, browser_session=browser_session)-- assert isinstance(result, ActionResult)- assert result.extracted_content is not None- assert 'Text: hello, Page Title: Test Page' in result.extracted_content-- async def test_pydantic_model_with_page_parameter(self, registry, browser_session, base_url):- """Test pydantic model action with page parameter injection"""-- @registry.action('Pydantic action with page', param_model=ComplexParams)- async def pydantic_action_with_page(params: ComplexParams, page: Page):- title = await page.title()- return ActionResult(extracted_content=f'Text: {params.text}, Number: {params.number}, Page Title: {title}')-- # Navigate to test page first- from browser_use.browser.events import NavigateToUrlEvent- event = browser_session.event_bus.dispatch(NavigateToUrlEvent(url=f'{base_url}/test', new_tab=True))- await event-- # Test execution- result = await registry.execute_action(- 'pydantic_action_with_page', {'text': 'test', 'number': 100}, browser_session=browser_session- )-- assert isinstance(result, ActionResult)- assert result.extracted_content is not None- assert 'Text: test, Number: 100, Page Title: Test Page' in result.extracted_content-async def test_pydantic_model_parameters(self, registry, browser_session, base_url):"""Test action that takes a pydantic model as first parameter"""@@ -205,6 +164,7 @@ class TestActionRegistryParameterPatterns:# Navigate to test page firstfrom browser_use.browser.events import NavigateToUrlEvent+event = browser_session.event_bus.dispatch(NavigateToUrlEvent(url=f'{base_url}/test', new_tab=True))await event@@ -226,13 +186,13 @@ class TestActionRegistryParameterPatterns:@registry.action('Action with multiple special params')async def multi_special_action(text: str,- page: Page,+ browser_session: BrowserSession,page_extraction_llm: BaseChatModel,available_file_paths: list,):llm_response = await page_extraction_llm.ainvoke([UserMessage(content='test')])files = available_file_paths or []- url = page.url+ url = await browser_session.get_current_page_url()return ActionResult(extracted_content=f'Text: {text}, URL: {url}, LLM: {llm_response.completion}, Files: {len(files)}'@@ -240,6 +200,7 @@ class TestActionRegistryParameterPatterns:# Navigate to test page firstfrom browser_use.browser.events import NavigateToUrlEvent+event = browser_session.event_bus.dispatch(NavigateToUrlEvent(url=f'{base_url}/test', new_tab=True))await event@@ -264,7 +225,8 @@ class TestActionRegistryParameterPatterns:"""Test action with NoParamsAction model"""@registry.action('No params action', param_model=NoParamsAction)- async def no_params_action(params: NoParamsAction, page: Page):+ async def no_params_action(params: NoParamsAction, browser_session: BrowserSession):+ url = await browser_session.get_current_page_url()return ActionResult(extracted_content=f'No params action executed on {url}')# Test execution with any parameters (should be ignored)@@ -302,51 +264,6 @@ class TestActionRegistryParameterPatterns:assert 'Legacy context: test2, URL:' in result2.extracted_contentassert '/test' in result2.extracted_content- async def test_page_parameter_optimization(self, browser_session: BrowserSession, httpserver: HTTPServer):- """Test that actions can use page: Page parameter directly instead of browser_session"""- registry = Registry()-- httpserver.expect_request('/test').respond_with_data('<html><body>Test Page</body></html>')- url = await browser_session.get_current_page_url()- await page.goto(httpserver.url_for('/test'))-- # Action that takes page directly (optimized pattern)- @registry.action('Action with direct page parameter')- async def direct_page_action(text: str, page: Page):- # This is the optimized pattern - no need to call get_current_page()- return ActionResult(extracted_content=f'Direct page: {text}, URL: {url}')-- # Action that takes browser_session and calls get_current_page (old pattern)- @registry.action('Action with browser_session parameter')- async def browser_session_action(text: str, browser_session: BrowserSession):- url = await browser_session.get_current_page_url()- return ActionResult(extracted_content=f'Browser session: {text}, URL: {url}')-- # Test direct page parameter- result1 = await registry.execute_action('direct_page_action', {'text': 'optimized'}, browser_session=browser_session)- assert result1.extracted_content is not None- assert 'Direct page: optimized, URL:' in result1.extracted_content- assert '/test' in result1.extracted_content-- # Test browser_session parameter (should still work)- result2 = await registry.execute_action('browser_session_action', {'text': 'legacy'}, browser_session=browser_session)- assert result2.extracted_content is not None- assert 'Browser session: legacy, URL:' in result2.extracted_content- assert '/test' in result2.extracted_content-- # Verify both patterns work with pydantic models too- class PageActionParams(BaseActionModel):- message: str = Field(..., description='Test message')-- @registry.action('Pydantic action with page', param_model=PageActionParams)- async def pydantic_page_action(params: PageActionParams, page: Page):- return ActionResult(extracted_content=f'Pydantic page: {params.message}, URL: {url}')-- result3 = await registry.execute_action('pydantic_page_action', {'message': 'pydantic'}, browser_session=browser_session)- assert result3.extracted_content is not None- assert 'Pydantic page: pydantic, URL:' in result3.extracted_content- assert '/test' in result3.extracted_content-class TestActionToActionCalling:"""Test scenarios where actions call other actions"""@@ -694,9 +611,7 @@ class TestType1Pattern:from browser_use.llm.base import BaseChatModel@registry.action('Extract content', param_model=ExtractAction)- async def extract_content(- params: ExtractAction, browser_session: BrowserSession, page: Page, page_extraction_llm: BaseChatModel- ):+ async def extract_content(params: ExtractAction, browser_session: BrowserSession, page_extraction_llm: BaseChatModel):return ActionResult(extracted_content=params.goal)assert 'extract_content' in registry.registry.actions@@ -710,7 +625,7 @@ class TestType2Pattern:registry = Registry()@registry.action('Fill field')- async def fill_field(index: int, text: str, page: Page):+ async def fill_field(index: int, text: str, browser_session: BrowserSession):return ActionResult(extracted_content=f'Filled {index} with {text}')# Verify registration@@ -741,7 +656,7 @@ class TestType2Pattern:registry = Registry()@registry.action('Save PDF')- async def save_pdf(browser_session: BrowserSession, page: Page):+ async def save_pdf(browser_session: BrowserSession):return ActionResult(extracted_content='Saved PDF')action = registry.registry.actions['save_pdf']@@ -780,20 +695,13 @@ class TestValidationRules:with pytest.raises(ValueError, match='kwargs.*not allowed'):@registry.action('Bad action')- async def bad_action(index: int, page: Page, **kwargs):+ async def bad_action(index: int, browser_session: BrowserSession, **kwargs):passdef test_error_on_special_param_name_with_wrong_type(self):"""Should error if special param name used with wrong type"""registry = Registry()- # Using 'page' with str type should error- with pytest.raises(ValueError, match=rf'conflicts with special argument.*page: {repr(Page)}'):-- @registry.action('Navigate')- async def navigate_to_page(page: str, browser_session: BrowserSession):- pass-# Using 'browser_session' with wrong type should errorwith pytest.raises(ValueError, match='conflicts with special argument.*browser_session: BrowserSession'):@@ -808,7 +716,6 @@ class TestValidationRules:@registry.action('Good action')async def good_action(index: int,- page: Page, # Correct typebrowser_session: BrowserSession, # Correct type):return ActionResult()@@ -868,7 +775,6 @@ class TestDecoratedFunctionBehavior:# Should work even with extra kwargsspecial_context = {- 'page': MockPage(),'browser_session': None,'page_extraction_llm': create_mock_llm(),'context': {'extra': 'data'},@@ -894,7 +800,6 @@ class TestParamsModelGeneration:query: str,max_results: int,include_images: bool = True,- page: Page = None, # type: ignorebrowser_session: BrowserSession = None, # type: ignore):return ActionResult()@@ -988,7 +893,6 @@ class TestParameterOrdering:first: str,browser_session: BrowserSession,second: int,- page: Page,third: bool = True,page_extraction_llm: BaseChatModel = None, # type: ignore):@@ -1020,7 +924,6 @@ class TestParameterOrdering:@registry.action('Extract content from page')async def extract_content(goal: str,- page: Page,page_extraction_llm,include_links: bool = False,):diff --git a/tests/ci/test_search_google_tab_focus.py b/tests/ci/test_search_google_tab_focus.pyindex c5765ac66..a9ca40d87 100644--- a/tests/ci/test_search_google_tab_focus.py+++ b/tests/ci/test_search_google_tab_focus.py@@ -15,7 +15,7 @@ async def browser_session():session = BrowserSession()await session.start()yield session- await session.close()+ await session.stop()async def test_search_google_creates_and_focuses_new_tab(browser_session: BrowserSession):@@ -125,6 +125,6 @@ if __name__ == '__main__':print('\n✅ All tests passed!')finally:- await session.close()+ await session.stop()asyncio.run(run_all_tests())diff --git a/tests/ci/test_sequential_agents_simple.py b/tests/ci/test_sequential_agents_simple.pyindex 675093b26..3e89c62a2 100644--- a/tests/ci/test_sequential_agents_simple.py+++ b/tests/ci/test_sequential_agents_simple.py@@ -31,7 +31,11 @@ class TestSequentialAgentsSimple:# Verify browser is running# Get initial browser PID from local_browser_watchdog- initial_pid = browser_session.local_browser_watchdog._subprocess.pid if browser_session.local_browser_watchdog and browser_session.local_browser_watchdog._subprocess else None+ initial_pid = (+ browser_session._local_browser_watchdog._subprocess.pid+ if browser_session._local_browser_watchdog and browser_session._local_browser_watchdog._subprocess+ else None+ )# Browser PID detection may fail in CI environments# The important thing is that the browser is connected# Verify session is connected@@ -69,7 +73,11 @@ class TestSequentialAgentsSimple:assert url is not Noneif initial_pid is not None:# Check browser PID is still the same- current_pid = browser_session.local_browser_watchdog._subprocess.pid if browser_session.local_browser_watchdog and browser_session.local_browser_watchdog._subprocess else None+ current_pid = (+ browser_session._local_browser_watchdog._subprocess.pid+ if browser_session._local_browser_watchdog and browser_session._local_browser_watchdog._subprocess+ else None+ )assert current_pid == initial_pid# Delete agent1 and force garbage collection@@ -83,7 +91,11 @@ class TestSequentialAgentsSimple:assert url is not Noneif initial_pid is not None:# Check browser PID is still the same- current_pid = browser_session.local_browser_watchdog._subprocess.pid if browser_session.local_browser_watchdog and browser_session.local_browser_watchdog._subprocess else None+ current_pid = (+ browser_session._local_browser_watchdog._subprocess.pid+ if browser_session._local_browser_watchdog and browser_session._local_browser_watchdog._subprocess+ else None+ )assert current_pid == initial_pid# Verify session is still connectedurl = await browser_session.get_current_page_url()@@ -117,7 +129,11 @@ class TestSequentialAgentsSimple:assert url is not Noneif initial_pid is not None:# Check browser PID is still the same- current_pid = browser_session.local_browser_watchdog._subprocess.pid if browser_session.local_browser_watchdog and browser_session.local_browser_watchdog._subprocess else None+ current_pid = (+ browser_session._local_browser_watchdog._subprocess.pid+ if browser_session._local_browser_watchdog and browser_session._local_browser_watchdog._subprocess+ else None+ )assert current_pid == initial_pid# Verify session is still connectedurl = await browser_session.get_current_page_url()diff --git a/tests/scripts/debug_iframe_scrolling.py b/tests/scripts/debug_iframe_scrolling.pyindex 99063c3d9..404bddc4e 100644--- a/tests/scripts/debug_iframe_scrolling.py+++ b/tests/scripts/debug_iframe_scrolling.py@@ -13,11 +13,9 @@ from pathlib import Pathsys.path.insert(0, str(Path(__file__).parent.parent.parent))from browser_use.agent.service import Agent-from browser_use.agent.views import ActionModel, AgentOutput+from browser_use.agent.views import ActionModelfrom browser_use.browser import BrowserProfile, BrowserSessionfrom browser_use.browser.events import BrowserStateRequestEvent-from browser_use.controller.service import Controller-from browser_use.controller.views import GoToUrlAction, ScrollAction# Import the mock LLM helper from conftestfrom tests.ci.conftest import create_mock_llm@@ -25,9 +23,9 @@ from tests.ci.conftest import create_mock_llmasync def debug_iframe_scrolling():"""Debug iframe scrolling and DOM visibility issue."""-- print("Starting iframe scrolling debug test...")-++ print('Starting iframe scrolling debug test...')+# Create the sequence of actions for the mock LLM# We need to format these as the LLM would return themactions = [@@ -99,12 +97,12 @@ async def debug_iframe_scrolling():}]}- """+ """,]-+# Create mock LLM with our action sequencemock_llm = create_mock_llm(actions=actions)-+# Create browser session with headless=False so we can see what's happeningbrowser_session = BrowserSession(browser_profile=BrowserProfile(@@ -115,191 +113,189 @@ async def debug_iframe_scrolling():cross_origin_iframes=True, # Enable cross-origin iframe support))-+try:# Start the browser sessionawait browser_session.start()- print("Browser session started")-+ print('Browser session started')+# Create an agent with the mock LLMagent = Agent(- task="Navigate to the iframe test page and scroll inside the iframe",+ task='Navigate to the iframe test page and scroll inside the iframe',llm=mock_llm,browser_session=browser_session,)-+# Helper function to capture and analyze DOM stateasync def capture_dom_state(label: str) -> dict:"""Capture DOM state and return analysis"""- print(f"\n📸 Capturing DOM state: {label}")+ print(f'\n📸 Capturing DOM state: {label}')state_event = browser_session.event_bus.dispatch(BrowserStateRequestEvent(- include_dom=True,- include_screenshot=False,- cache_clickable_elements_hashes=True,- include_recent_events=False+ include_dom=True, include_screenshot=False, cache_clickable_elements_hashes=True, include_recent_events=False))browser_state = await state_event.event_result()-+if browser_state and browser_state.dom_state and browser_state.dom_state.selector_map:selector_map = browser_state.dom_state.selector_mapelement_count = len(selector_map)-+# Check for specific elementsfound_elements = {}expected_checks = [- ("First Name", ["firstName", "first name"]),- ("Last Name", ["lastName", "last name"]),- ("Email", ["email"]),- ("City", ["city"]),- ("State", ["state"]),- ("Zip", ["zip", "zipCode"]),+ ('First Name', ['firstName', 'first name']),+ ('Last Name', ['lastName', 'last name']),+ ('Email', ['email']),+ ('City', ['city']),+ ('State', ['state']),+ ('Zip', ['zip', 'zipCode']),]-+for name, keywords in expected_checks:for index, element in selector_map.items():element_str = str(element).lower()if any(kw.lower() in element_str for kw in keywords):found_elements[name] = Truebreak-+return {- "label": label,- "total_elements": element_count,- "found_elements": found_elements,- "selector_map": selector_map+ 'label': label,+ 'total_elements': element_count,+ 'found_elements': found_elements,+ 'selector_map': selector_map,}- return {"label": label, "error": "No DOM state available"}-+ return {'label': label, 'error': 'No DOM state available'}+# Capture initial state before any actions- print("\n" + "="*80)- print("PHASE 1: INITIAL PAGE LOAD")- print("="*80)-+ print('\n' + '=' * 80)+ print('PHASE 1: INITIAL PAGE LOAD')+ print('=' * 80)+# Navigate to the page first- from browser_use.controller.views import GoToUrlActionfrom browser_use.controller.service import Controller+controller = Controller()-+# Create the action model for navigationgoto_action = ActionModel.model_validate_json(actions[0])await controller.act(goto_action, browser_session)await asyncio.sleep(2) # Wait for page to fully load-- initial_state = await capture_dom_state("INITIAL (after page load)")-++ initial_state = await capture_dom_state('INITIAL (after page load)')+# Now run the rest of the actions via the agent- print("\n" + "="*80)- print("PHASE 2: EXECUTING ACTIONS")- print("="*80)-+ print('\n' + '=' * 80)+ print('PHASE 2: EXECUTING ACTIONS')+ print('=' * 80)+# Create new agent with remaining actionsremaining_actions = actions[1:] # Skip the navigation we already didmock_llm_remaining = create_mock_llm(actions=remaining_actions)agent = Agent(- task="Input text and scroll inside the iframe",+ task='Input text and scroll inside the iframe',llm=mock_llm_remaining,browser_session=browser_session,)-+# Hook into agent actions to capture state after each onestates = []original_act = controller.act+async def wrapped_act(action, session):result = await original_act(action, session)# Capture state after each action- action_type = "unknown"+ action_type = 'unknown'if hasattr(action, 'input_text') and action.input_text:- action_type = "input_text"+ action_type = 'input_text'await asyncio.sleep(1) # Give time for DOM to update- state = await capture_dom_state("AFTER INPUT_TEXT")+ state = await capture_dom_state('AFTER INPUT_TEXT')states.append(state)elif hasattr(action, 'scroll') and action.scroll:- action_type = "scroll"+ action_type = 'scroll'await asyncio.sleep(2) # Give more time after scroll- state = await capture_dom_state("AFTER SCROLL")+ state = await capture_dom_state('AFTER SCROLL')states.append(state)return result-+controller.act = wrapped_act-+# Run the agent with remaining actionsresult = await agent.run()- print(f"\nAgent completed with result: {result}")-+ print(f'\nAgent completed with result: {result}')+# Analyze all captured states- print("\n" + "="*80)- print("PHASE 3: ANALYSIS OF DOM STATES")- print("="*80)-+ print('\n' + '=' * 80)+ print('PHASE 3: ANALYSIS OF DOM STATES')+ print('=' * 80)+all_states = [initial_state] + states-+for state in all_states:- if "error" in state:- print(f"\n❌ {state['label']}: {state['error']}")+ if 'error' in state:+ print(f'\n❌ {state["label"]}: {state["error"]}')else:- print(f"\n📊 {state['label']}:")- print(f" Total elements: {state['total_elements']}")- print(f" Found elements:")+ print(f'\n📊 {state["label"]}:')+ print(f' Total elements: {state["total_elements"]}')+ print(' Found elements:')for elem_name, found in state['found_elements'].items():- status = "✓" if found else "✗"- print(f" {status} {elem_name}")-+ status = '✓' if found else '✗'+ print(f' {status} {elem_name}')+# Compare states- print("\n" + "="*80)- print("COMPARISON SUMMARY")- print("="*80)-+ print('\n' + '=' * 80)+ print('COMPARISON SUMMARY')+ print('=' * 80)+if len(all_states) >= 3:initial = all_states[0]after_input = all_states[1] if len(all_states) > 1 else Noneafter_scroll = all_states[2] if len(all_states) > 2 else None-- print(f"\nElement count changes:")- print(f" Initial: {initial.get('total_elements', 0)} elements")++ print('\nElement count changes:')+ print(f' Initial: {initial.get("total_elements", 0)} elements')if after_input:- print(f" After input_text: {after_input.get('total_elements', 0)} elements")+ print(f' After input_text: {after_input.get("total_elements", 0)} elements')if after_scroll:- print(f" After scroll: {after_scroll.get('total_elements', 0)} elements")-+ print(f' After scroll: {after_scroll.get("total_elements", 0)} elements')+# Check if lower form fields appear after scroll- if after_scroll and "found_elements" in after_scroll:- lower_fields = ["City", "State", "Zip"]- missing_fields = [f for f in lower_fields if not after_scroll["found_elements"].get(f, False)]-+ if after_scroll and 'found_elements' in after_scroll:+ lower_fields = ['City', 'State', 'Zip']+ missing_fields = [f for f in lower_fields if not after_scroll['found_elements'].get(f, False)]+if missing_fields:- print(f"\n⚠️ BUG CONFIRMED: Lower form fields missing after scroll:")+ print('\n⚠️ BUG CONFIRMED: Lower form fields missing after scroll:')for field in missing_fields:- print(f" ✗ {field}")- print("\nThis confirms that scrolling inside iframes does not update the DOM tree properly.")+ print(f' ✗ {field}')+ print('\nThis confirms that scrolling inside iframes does not update the DOM tree properly.')else:- print("\n✅ SUCCESS: All lower form fields are visible after scrolling!")-+ print('\n✅ SUCCESS: All lower form fields are visible after scrolling!')+# Show first few elements from final state for debugging- if states and "selector_map" in states[-1]:- print("\n" + "="*80)- print("DEBUG: First 5 elements in final selector_map")- print("="*80)- final_map = states[-1]["selector_map"]+ if states and 'selector_map' in states[-1]:+ print('\n' + '=' * 80)+ print('DEBUG: First 5 elements in final selector_map')+ print('=' * 80)+ final_map = states[-1]['selector_map']for i, (index, element) in enumerate(list(final_map.items())[:5]):elem_preview = str(element)[:150]- print(f"\n [{index}]: {elem_preview}...")-+ print(f'\n [{index}]: {elem_preview}...')+# Keep browser open for manual inspection if needed- print("\n" + "="*80)- print("Test complete. Browser will remain open for 10 seconds for inspection...")- print("="*80)+ print('\n' + '=' * 80)+ print('Test complete. Browser will remain open for 10 seconds for inspection...')+ print('=' * 80)await asyncio.sleep(10)-+finally:# Clean up- print("\nCleaning up...")+ print('\nCleaning up...')await browser_session.kill()await browser_session.event_bus.stop(clear=True, timeout=5)- print("Browser session closed")+ print('Browser session closed')-if __name__ == "__main__":+if __name__ == '__main__':# Run the debug test- asyncio.run(debug_iframe_scrolling())\ No newline at end of file+ asyncio.run(debug_iframe_scrolling())diff --git a/tests/scripts/test_frame_hierarchy.py b/tests/scripts/test_frame_hierarchy.pyindex 02eaf21d4..7f9225ef8 100755--- a/tests/scripts/test_frame_hierarchy.py+++ b/tests/scripts/test_frame_hierarchy.py@@ -224,7 +224,7 @@ async def analyze_frame_hierarchy(url):if session._cdp_client_root:try:await session._cdp_client_root.stop()- except:+ except Exception:pass # Ignore errors if already disconnected# Then stop the browser process