"""Test CrashWatchdog functionality.""" import asyncio from typing import cast import pytest from browser_use.browser.events import ( BrowserConnectedEvent, BrowserErrorEvent, BrowserStartEvent, BrowserStopEvent, BrowserStoppedEvent, NavigateToUrlEvent, ) from browser_use.browser.profile import BrowserProfile from browser_use.browser.session import BrowserSession from browser_use.utils import logger @pytest.mark.asyncio @pytest.mark.skip('CrashWatchdog not implemented in current CDP architecture') async def test_crash_watchdog_network_timeout(): """Test that CrashWatchdog detects network timeouts by monitoring actual network requests.""" # Create browser session profile = BrowserProfile(headless=True) session = BrowserSession(browser_profile=profile) try: # Start browser using event system session.event_bus.dispatch(BrowserStartEvent()) await session.event_bus.expect(BrowserConnectedEvent, timeout=10.0) logger.info('[TEST] Browser started, configuring watchdog timeout') # Configure crash watchdog with very short timeout for testing if hasattr(session, '_crash_watchdog') and session._crash_watchdog: session._crash_watchdog.network_timeout_seconds = 1.0 # Very short timeout session._crash_watchdog.check_interval_seconds = 0.2 # Check frequently # Try to navigate to a non-existent slow server that will hang # This will create a real network request that will timeout slow_url = 'http://192.0.2.1:4242/timeout-test' # RFC5737 TEST-NET-1 - non-routable logger.info(f'[TEST] Navigating to non-routable URL via events: {slow_url}') session.event_bus.dispatch(NavigateToUrlEvent(url=slow_url)) # Wait for the network timeout error via event bus logger.info('[TEST] Waiting for NetworkTimeout event via event bus...') try: timeout_error = cast( BrowserErrorEvent, await session.event_bus.expect( BrowserErrorEvent, predicate=lambda e: cast(BrowserErrorEvent, e).error_type == 'NetworkTimeout', timeout=8.0 ), ) # Verify the timeout event details assert 'timeout-test' in timeout_error.details['url'] assert timeout_error.details['elapsed_seconds'] >= 0.8 # Should be at least close to our timeout assert timeout_error.message.startswith('Network request timed out after') logger.info(f'[TEST] Successfully detected network timeout: {timeout_error.message}') except TimeoutError: # Network timeout detection can be flaky in test environment logger.warning('[TEST] NetworkTimeout event not received - this is expected in some test environments') # Verify the crash watchdog is running and configured correctly assert session._crash_watchdog is not None, 'CrashWatchdog should exist' assert session._crash_watchdog.network_timeout_seconds == 1.0, 'Network timeout should be configured' assert session._crash_watchdog._monitoring_task is not None, 'Monitoring task should be running' assert not session._crash_watchdog._monitoring_task.done(), 'Monitoring task should still be active' logger.info('[TEST] Crash watchdog is properly configured and running - test passes') finally: # Clean shutdown try: session.event_bus.dispatch(BrowserStopEvent()) await session.event_bus.expect(BrowserStoppedEvent, timeout=3.0) except Exception: # If graceful shutdown fails, force cleanup await session.kill() @pytest.mark.asyncio @pytest.mark.skip('CrashWatchdog not implemented in current CDP architecture') async def test_crash_watchdog_browser_disconnect(): """Test that CrashWatchdog detects browser disconnection through monitoring.""" profile = BrowserProfile(headless=True) session = BrowserSession(browser_profile=profile) try: # Start browser start_event = session.event_bus.dispatch(BrowserStartEvent()) await start_event # Ensure any exceptions from the event handler are propagated await start_event.event_result(raise_if_any=True, raise_if_none=False) # Wait for browser to be fully started await session.event_bus.expect(BrowserConnectedEvent, timeout=5.0) # Browser disconnection detection is now handled by the crash watchdog # No configuration needed # Mock browser disconnection by overriding is_connected # This simulates what would happen if the browser process crashed if session._cdp_client_root: # Simulate disconnection by setting _cdp_client_root to None original_cdp_client = session._cdp_client_root session._cdp_client_root = None try: # Wait for watchdog to detect disconnection disconnect_error: BrowserErrorEvent = cast( BrowserErrorEvent, await session.event_bus.expect( BrowserErrorEvent, predicate=lambda e: cast(BrowserErrorEvent, e).error_type == 'BrowserDisconnected', timeout=2.0, ), ) assert 'disconnected unexpectedly' in disconnect_error.message finally: # Restore original CDP client session._cdp_client_root = original_cdp_client finally: # Force stop even if browser is marked as disconnected try: session.event_bus.dispatch(BrowserStopEvent(force=True)) await asyncio.sleep(0.5) # Give it time to stop except Exception: pass # Browser might already be stopped @pytest.mark.asyncio @pytest.mark.skip('CrashWatchdog not implemented in current CDP architecture') async def test_crash_watchdog_lifecycle(): """Test that CrashWatchdog starts and stops with browser session.""" profile = BrowserProfile(headless=True) session = BrowserSession(browser_profile=profile) # Start browser via event and wait for BrowserConnectedEvent start_event = session.event_bus.dispatch(BrowserStartEvent()) await start_event # Wait for the event and all handlers to complete started_event: BrowserConnectedEvent = cast( BrowserConnectedEvent, await session.event_bus.expect(BrowserConnectedEvent, timeout=5.0) ) assert started_event.cdp_url is not None # Verify crash watchdog is running assert hasattr(session, '_crash_watchdog'), 'CrashWatchdog should be created' assert session._crash_watchdog is not None, 'CrashWatchdog should not be None' # Check monitoring task is active assert session._crash_watchdog._monitoring_task is not None assert not session._crash_watchdog._monitoring_task.done() # Stop browser via event session.event_bus.dispatch(BrowserStopEvent()) # Wait for browser stopped event try: stopped_event: BrowserStoppedEvent = cast( BrowserStoppedEvent, await session.event_bus.expect(BrowserStoppedEvent, timeout=3.0) ) assert stopped_event.reason is not None except TimeoutError: # Browser stop can be flaky in test environment logger.warning('[TEST] BrowserStoppedEvent timeout - this is expected in some test environments') # Just verify the crash watchdog exists assert session._crash_watchdog is not None # Verify monitoring task was stopped await asyncio.sleep(0.1) # Give it a moment to clean up if session._crash_watchdog._monitoring_task: assert session._crash_watchdog._monitoring_task.done() @pytest.mark.asyncio @pytest.mark.skip(reason='Browser initialization timeout in test environment - timing issue') 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( '