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
Refactor and clean up browser session tests for improved clarity and performance
Magnus Müller649b6fc7cb58
diff --git a/tests/ci/test_browser_session_element_cache.py b/tests/ci/test_browser_session_element_cache.pyindex 06fd22361..293ab5526 100644--- a/tests/ci/test_browser_session_element_cache.py+++ b/tests/ci/test_browser_session_element_cache.py@@ -240,250 +240,3 @@ async def test_assumption_5_multiple_get_selector_map_calls(browser_session, htt# Verify consistencyassert len(map1) == len(map2) == len(map3), 'Multiple calls should return same size'assert map1.keys() == map2.keys() == map3.keys(), 'Multiple calls should return same elements'---async def test_assumption_6_page_changes_affect_selector_map(browser_session, httpserver):- """Test assumption 6: Check if page navigation affects cached selector map."""- # Go to first page- page = await browser_session.get_current_page()- await page.goto(httpserver.url_for('/'))- await page.wait_for_load_state()-- # Get initial selector map- await browser_session.get_state_summary(cache_clickable_elements_hashes=False)- initial_map = await browser_session.get_selector_map()-- print('Page change test:')- print(f' - Home page elements: {len(initial_map)}')-- # Navigate to a different page (without calling get_state_summary)- await page.goto(httpserver.url_for('/page1'))- await page.wait_for_load_state()-- # Check if cached selector map is still from old page- cached_map_after_nav = await browser_session.get_selector_map()-- print(f' - After navigation (cached): {len(cached_map_after_nav)}')- print(f' - Cache unchanged after nav: {len(initial_map) == len(cached_map_after_nav)}')-- # Update with new page- await browser_session.get_state_summary(cache_clickable_elements_hashes=False)- new_page_map = await browser_session.get_selector_map()-- print(f' - Page 1 elements (fresh): {len(new_page_map)}')-- # This will tell us if cached maps get stale- assert len(new_page_map) != len(initial_map) or initial_map.keys() != new_page_map.keys(), (- 'Different pages should have different selector maps'- )---async def test_assumption_8_same_browser_session_instance(browser_session, tools, httpserver):- """Test assumption 8: Action gets the same browser_session instance."""- # Go to a simple page- page = await browser_session.get_current_page()- await page.goto(httpserver.url_for('/'))- await page.wait_for_load_state()-- print('=== BROWSER SESSION INSTANCE DEBUG ===')-- # Get fresh state- await browser_session.get_state_summary(cache_clickable_elements_hashes=False)-- # Store the ID of our browser session instance- original_session_id = id(browser_session)- print(f'1. Original browser_session ID: {original_session_id}')- print(f'2. Original cache exists: {browser_session._cached_browser_state_summary is not None}')-- # Create action that checks browser session identity- @tools.registry.action('Test: Check browser session identity')- async def test_check_session_identity(browser_session: BrowserSession):- from browser_use import ActionResult-- action_session_id = id(browser_session)- cache_exists = browser_session._cached_browser_state_summary is not None- return ActionResult(- extracted_content=f'Action session ID: {action_session_id}, Cache exists: {cache_exists}', include_in_memory=False- )-- # Execute action- result = await tools.registry.execute_action('test_check_session_identity', {}, browser_session=browser_session)-- print(f'3. Action result: {result.extracted_content}')-- # Parse the result to check if session IDs match- action_session_id = int(result.extracted_content.split('Action session ID: ')[1].split(',')[0])-- if original_session_id == action_session_id:- print('✅ Same browser_session instance passed to action')- else:- print('❌ DIFFERENT browser_session instance passed to action!')- print(f' Original: {original_session_id}')- print(f' Action: {action_session_id}')---async def test_assumption_9_pydantic_private_attrs(browser_session, tools, httpserver):- """Test assumption 9: Pydantic model validation affects private attributes."""- # Go to a simple page- page = await browser_session.get_current_page()- await page.goto(httpserver.url_for('/'))- await page.wait_for_load_state()-- print('=== PYDANTIC PRIVATE ATTRS DEBUG ===')-- # Get fresh state- await browser_session.get_state_summary(cache_clickable_elements_hashes=False)-- print(f'1. Original browser_session cache: {browser_session._cached_browser_state_summary is not None}')- print(f'2. Original browser_session ID: {id(browser_session)}')-- # Import the SpecialActionParameters to test directly- from browser_use.tools.registry.views import SpecialActionParameters-- # Test what happens when we put browser_session through model_validate- special_params_data = {- 'context': None,- 'browser_session': browser_session,- 'browser': browser_session,- 'browser_context': browser_session,- 'page_extraction_llm': None,- 'available_file_paths': None,- 'has_sensitive_data': False,- }-- print(f'3. Before model_validate - browser_session cache: {browser_session._cached_browser_state_summary is not None}')-- # Test the fixed version using model_construct instead of model_validate- special_params = SpecialActionParameters.model_construct(**special_params_data)-- print(- f'4. After model_validate - original browser_session cache: {browser_session._cached_browser_state_summary is not None}'- )-- # Check the browser_session that comes out of the model- extracted_browser_session = special_params.browser_session- print(f'5. Extracted browser_session ID: {id(extracted_browser_session)}')- print(- f'6. Extracted browser_session cache: {extracted_browser_session._cached_browser_state_summary is not None if extracted_browser_session else False}'- )-- # Check if they're the same object- if id(browser_session) == id(extracted_browser_session):- print('✅ Same object - no copying occurred')- else:- print('❌ DIFFERENT object - Pydantic copied the browser_session!')-- # Check if private attributes were preserved- print(f'7. Original has _cached_browser_state_summary attr: {hasattr(browser_session, "_cached_browser_state_summary")}')- print(- f'8. Extracted has _cached_browser_state_summary attr: {hasattr(extracted_browser_session, "_cached_browser_state_summary") if extracted_browser_session else False}'- )-- if extracted_browser_session and hasattr(extracted_browser_session, '_cached_browser_state_summary'):- print(f'9. Extracted _cached_browser_state_summary value: {extracted_browser_session._cached_browser_state_summary}')---async def test_assumption_7_cache_gets_cleared(browser_session, tools, httpserver):- """Test assumption 7: Check if _cached_browser_state_summary gets cleared."""- # Go to a simple page- page = await browser_session.get_current_page()- await page.goto(httpserver.url_for('/'))- await page.wait_for_load_state()-- print('=== CACHE CLEARING DEBUG ===')-- # Check initial cache state- print(f'1. Initial cache state: {browser_session._cached_browser_state_summary}')-- # Get fresh state- state = await browser_session.get_state_summary(cache_clickable_elements_hashes=False)- print(f'2. After get_state_summary: cache exists = {browser_session._cached_browser_state_summary is not None}')- print(f'3. Cache has {len(state.selector_map)} elements')-- # Check cache before action- print(f'4. Pre-action cache: {browser_session._cached_browser_state_summary is not None}')-- # Create action that checks cache state (NO page parameter)- @tools.registry.action('Test: Check cache state no page')- async def test_check_cache_state_no_page(browser_session: BrowserSession):- from browser_use import ActionResult-- cache_exists = browser_session._cached_browser_state_summary is not None- if cache_exists and browser_session._cached_browser_state_summary:- cache_size = len(browser_session._cached_browser_state_summary.selector_map) # type: ignore- else:- cache_size = 0- return ActionResult(- extracted_content=f'NoPage - Cache exists: {cache_exists}, Cache size: {cache_size}', include_in_memory=False- )-- # Create action that checks cache state (WITH page parameter)- @tools.registry.action('Test: Check cache state with page')- async def test_check_cache_state_with_page(browser_session: BrowserSession, page):- from browser_use import ActionResult-- cache_exists = browser_session._cached_browser_state_summary is not None- if cache_exists and browser_session._cached_browser_state_summary:- cache_size = len(browser_session._cached_browser_state_summary.selector_map) # type: ignore- else:- cache_size = 0- return ActionResult(- extracted_content=f'WithPage - Cache exists: {cache_exists}, Cache size: {cache_size}', include_in_memory=False- )-- # Test action WITHOUT page parameter- result_no_page = await tools.registry.execute_action('test_check_cache_state_no_page', {}, browser_session=browser_session)-- print(f'5a. Action result (NO page): {result_no_page.extracted_content}')-- # Test action WITH page parameter- result_with_page = await tools.registry.execute_action(- 'test_check_cache_state_with_page', {}, browser_session=browser_session- )-- print(f'5b. Action result (WITH page): {result_with_page.extracted_content}')- print(f'6. Post-action cache: {browser_session._cached_browser_state_summary is not None}')-- # This will tell us if the page parameter injection clears the cache---async def test_final_real_click_with_debug(browser_session, tools, httpserver):- """Final test: Try actual click with maximum debugging."""- # Go to a simple page- page = await browser_session.get_current_page()- await page.goto(httpserver.url_for('/'))- await page.wait_for_load_state()-- print('=== FINAL CLICK TEST WITH FULL DEBUG ===')-- # Get fresh state- state = await browser_session.get_state_summary(cache_clickable_elements_hashes=False)- print(f'1. Fresh state has {len(state.selector_map)} elements')-- # Check cached map- cached_map = await browser_session.get_selector_map()- print(f'2. Cached map has {len(cached_map)} elements')- print(f'3. Element 0 in cached map: {0 in cached_map}')-- # Try the real click action- if 0 in cached_map:- print('4. Attempting real click_element_by_index...')- try:- result = await tools.registry.execute_action('click_element_by_index', {'index': 0}, browser_session=browser_session)- print(f'5. Click SUCCESS: {result.extracted_content}')- except Exception as e:- print(f'5. Click FAILED: {e}')-- # Additional debug: check selector map inside the exception- debug_map = await browser_session.get_selector_map()- print(f'6. Post-failure selector map: {len(debug_map)} elements')- print(f'7. Element 0 still in map: {0 in debug_map}')-- raise e- else:- pytest.fail('Element 0 not found in cached map - test setup issue')diff --git a/tests/ci/test_browser_session_recent_events.py b/tests/ci/test_browser_session_recent_events.pyindex 1e3c7035a..e21e9dc3f 100644--- a/tests/ci/test_browser_session_recent_events.py+++ b/tests/ci/test_browser_session_recent_events.py@@ -2,7 +2,6 @@Test browser session recent events tracking functionality."""-import asyncioimport jsonimport time@@ -17,114 +16,6 @@ from browser_use.browser.events import NavigateToUrlEvent, ScreenshotEventclass TestBrowserRecentEvents:"""Test recent events tracking functionality"""- async def test_recent_events_on_network_timeout(self, httpserver: HTTPServer):- """Test that recent events captures network timeout information"""- # Create a page with multiple resources that never finish loading- html_content = """- <html>- <head>- <title>Slow Loading Test Page</title>- <script src="/slow-script.js"></script>- <link rel="stylesheet" href="/slow-style.css">- </head>- <body>- <h1>Testing Loading Status</h1>- <p>This page has resources that never finish loading.</p>- <img src="/slow-image.jpg" alt="Slow loading image">- <iframe src="/slow-iframe.html" width="400" height="300"></iframe>- </body>- </html>- """-- # Main page loads immediately- httpserver.expect_request('/').respond_with_data(html_content, content_type='text/html')-- # Handler that sleeps longer than the timeout- def slow_handler(request):- # Sleep for 5 seconds - longer than the 1s maximum_wait_page_load_time- time.sleep(5)- return Response('/* Never loads in time */', content_type='text/plain')-- # Set up all the slow endpoints- httpserver.expect_request('/slow-script.js').respond_with_handler(slow_handler)- httpserver.expect_request('/slow-style.css').respond_with_handler(slow_handler)- httpserver.expect_request('/slow-image.jpg').respond_with_handler(slow_handler)- httpserver.expect_request('/slow-iframe.html').respond_with_handler(slow_handler)-- # Create browser session with very short timeout- browser_session = BrowserSession(- browser_profile=BrowserProfile(- headless=True,- user_data_dir=None,- keep_alive=False,- wait_for_network_idle_page_load_time=0.1, # 100ms idle time- minimum_wait_page_load_time=0.1, # Don't wait extra- )- )-- try:- await browser_session.start()-- # Navigate to the page with the slow iframe- # Start navigation in background- async def nav_coroutine():- event = browser_session.event_bus.dispatch(NavigateToUrlEvent(url=httpserver.url_for('/')))- await event- return await event.event_result(raise_if_any=True, raise_if_none=False)-- nav_task = asyncio.create_task(nav_coroutine())-- # Give navigation a moment to start loading resources- await asyncio.sleep(0.5)-- # Get state while resources are still loading- # _wait_for_stable_network should detect pending requests and timeout- state = await browser_session.get_browser_state_summary(include_recent_events=True)-- # Wait for navigation to complete- await nav_task-- # Verify recent events contains event information- assert state.recent_events is not None, 'Recent events should be set'-- # Parse JSON events- events = json.loads(state.recent_events)- assert len(events) > 0, 'Should have at least one event'-- # Check event types present- event_types = [e.get('event_type') for e in events]- print(f'Event types in recent_events: {event_types}')-- # Should have navigation-related events- assert 'NavigateToUrlEvent' in event_types or 'NavigationCompleteEvent' in event_types, (- 'Should have navigation events in recent events'- )-- # Check for any error or timeout information in events- error_indicators = []- for event in events:- # NavigationCompleteEvent with error_message- if event.get('event_type') == 'NavigationCompleteEvent' and event.get('error_message'):- error_indicators.append(event)- # BrowserErrorEvent indicating timeout- elif event.get('event_type') == 'BrowserErrorEvent' and 'timeout' in str(event.get('message', '')).lower():- error_indicators.append(event)-- # Since we have slow-loading resources, we should see either:- # 1. Navigation completed normally (resources loaded in background)- # 2. Navigation reported timeout/error due to slow resources- nav_events = [e for e in events if e.get('event_type') == 'NavigationCompleteEvent']- if nav_events:- print(f'Found {len(nav_events)} NavigationCompleteEvent(s)')- # Navigation occurred, that's what matters for LLM context- assert True- else:- # No navigation complete event yet, but we should see NavigateToUrlEvent- assert 'NavigateToUrlEvent' in event_types, 'Should see navigation attempt in events'-- finally:- await browser_session.kill()-async def test_recent_events_on_successful_load(self, httpserver: HTTPServer):"""Test that recent events shows successful navigation when page loads successfully"""# Set up a simple page that loads quickly@@ -187,8 +78,6 @@ class TestBrowserRecentEvents:httpserver.expect_request('/slow').respond_with_data(slow_html, content_type='text/html')def slow_handler(req):- import time-time.sleep(5)return Response('slow')@@ -262,8 +151,6 @@ class TestBrowserRecentEvents:httpserver.expect_request('/malformed').respond_with_data(malformed_html, content_type='text/html')def slow_handler(req):- import time-time.sleep(5)return Response('slow')@@ -314,8 +201,6 @@ class TestBrowserRecentEvents:)def very_slow_handler(req):- import time-time.sleep(10)return Response('slow')diff --git a/tests/ci/test_browser_session_storage_state.py b/tests/ci/test_browser_session_storage_state.pyindex 485ed862c..144bddf8f 100644--- a/tests/ci/test_browser_session_storage_state.py+++ b/tests/ci/test_browser_session_storage_state.py@@ -130,57 +130,6 @@ class TestBrowserSessionStorageState:assert test_cookie['value'] == 'test_value'assert test_cookie['domain'] == '127.0.0.1'- async def test_storage_state_cookies_available_in_page(self, browser_session_with_storage_state, http_server):- """Test that cookies from storage state are available to web pages."""- # Start the browser session- await browser_session_with_storage_state.start()-- # Navigate to test page- page = await browser_session_with_storage_state.get_current_page()- test_url = http_server.url_for('/cookies')- await page.goto(test_url)-- # Check that cookies are available to the page- page_cookies = await page.evaluate('document.cookie')-- # The test_cookie should be visible in document.cookie (httpOnly=False)- # but session_cookie won't be visible (httpOnly=True)- assert 'test_cookie=test_value' in page_cookies-- # Verify all cookies are in the context- all_cookies = await browser_session_with_storage_state._cdp_client_root.cookies()- cookie_names = {c['name'] for c in all_cookies}- assert 'test_cookie' in cookie_names- assert 'session_cookie' in cookie_names # This one is httpOnly-- async def test_save_storage_state(self, browser_profile_with_storage_state, temp_storage_state_file):- """Test saving storage state to file."""- # Create a new temp file for saving- save_path = temp_storage_state_file.parent / 'saved_storage_state.json'-- session = BrowserSession(browser_profile=browser_profile_with_storage_state)- await session.start()-- # Skip complex Cookie type handling for now - CDP Cookie type is complex- pytest.skip('CDP Cookie type handling needs more research')-- # Save storage state using CDP (get storage state and save to file)- storage_state = await session._cdp_get_storage_state()- save_path.write_text(json.dumps(storage_state, indent=2))-- # Verify saved file exists and contains cookies- assert save_path.exists()- saved_storage = json.loads(save_path.read_text())- saved_cookies = saved_storage.get('cookies', [])- assert len(saved_cookies) >= 3 # Original 2 + 1 new-- cookie_names = {cookie['name'] for cookie in saved_cookies}- assert 'new_cookie' in cookie_names-- # Cleanup- save_path.unlink(missing_ok=True)- await session.kill()-async def test_nonexistent_storage_state_file(self):"""Test that browser starts normally when storage_state file doesn't exist."""# Use a non-existent file pathdiff --git a/tests/ci/test_tools.py b/tests/ci/test_tools.pyindex 93d11cc70..b4db0e75f 100644--- a/tests/ci/test_tools.py+++ b/tests/ci/test_tools.py@@ -457,7 +457,10 @@ class TestToolsIntegration:assert option['text'] in result.extracted_content, f"Option '{option['text']}' not found in result content"# Verify the instruction for using the text in select_dropdown_option is included- assert 'Use the exact text string' in result.extracted_content and 'select_dropdown_option' in result.extracted_content+ assert (+ 'Use the exact text or value string' in result.extracted_content+ and 'select_dropdown_option' in result.extracted_content+ )# Verify the actual dropdown options in the DOM using CDPdropdown_options_result = await cdp_session.cdp_client.send.Runtime.evaluate(