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
Remove non critical tests
Magnus Müllerf188a4e9f6e9
diff --git a/tests/ci/test_agent_concurrency_multiprocessing.py b/tests/ci/test_agent_concurrency_multiprocessing.pydeleted file mode 100644index c35f1b0a0..000000000--- a/tests/ci/test_agent_concurrency_multiprocessing.py+++ /dev/null@@ -1,482 +0,0 @@-"""-Tests for parallelism and event loop handling in browser-use.--Tests cover:-1. One event loop with asyncio.run and one task-2. One event loop with two different parallel agents-3. One event loop with two different sequential agents-4. Two event loops, with one agent per loop sequential-5. Two event loops, one per thread, with one agent in each loop-6. Two subprocesses, with one agent per subprocess-7. Failing test to catch asyncio.run() RuntimeError issue-"""--import asyncio-import logging-import multiprocessing-from concurrent.futures import ThreadPoolExecutor--import pytest--from browser_use import Agent, setup_logging-from browser_use.browser import BrowserProfile, BrowserSession-from tests.ci.conftest import create_mock_llm--# Set up test logging-setup_logging()-logger = logging.getLogger(__name__)---def run_agent_in_subprocess_module(task_description):- """Module-level function to run an agent in a subprocess"""- import asyncio-- from browser_use import Agent-- # Create new event loop for this process- loop = asyncio.new_event_loop()- asyncio.set_event_loop(loop)-- async def run_agent():- # Create mock LLM inline to avoid pickling issues- mock_llm = create_mock_llm()-- agent = Agent(- task=task_description,- llm=mock_llm,- browser_profile=BrowserProfile(headless=True, user_data_dir=None),- )- return await agent.run()-- try:- result = loop.run_until_complete(run_agent())- has_done = False- if len(result.history) > 0:- last_history = result.history[-1]- if last_history.model_output and last_history.model_output.action:- has_done = any('done' in action.model_dump(include={'done'}) for action in last_history.model_output.action)- return {'success': has_done, 'error': None}- except Exception as e:- return {'success': False, 'error': str(e)}- finally:- # Give asyncio tasks a moment to complete- try:- loop.run_until_complete(asyncio.sleep(0.1))- except Exception:- pass- # Cancel all pending tasks- try:- pending = asyncio.all_tasks(loop)- for task in pending:- task.cancel()- if pending:- loop.run_until_complete(asyncio.gather(*pending, return_exceptions=True))- except Exception:- pass- loop.stop()- loop.close()---class TestParallelism:- """Test parallelism and event loop handling"""-- async def test_one_event_loop_with_asyncio_run_and_one_task(self):- """Test one event loop with asyncio.run and one task"""- logger.info('Testing one event loop with asyncio.run and one task')-- # Create mock LLM- mock_llm = create_mock_llm()-- # Just run directly in the current event loop- agent = Agent(- task='Test task',- llm=mock_llm,- browser_profile=BrowserProfile(headless=True, user_data_dir=None),- )- result = await agent.run()-- # Verify the agent completed successfully- assert result is not None- assert len(result.history) > 0- # Check that the last action was 'done'- last_history = result.history[-1]- 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)-- @pytest.mark.skip('TODO: fix this')- async def test_one_event_loop_two_parallel_agents(self):- """Test one event loop with two different parallel agents"""- logger.info('Testing one event loop with two parallel agents')-- # Create mock LLM- mock_llm = create_mock_llm()-- # Create a shared browser session- browser_session = BrowserSession(- browser_profile=BrowserProfile(- headless=True,- user_data_dir=None, # Use temp directory- keep_alive=True,- )- )-- try:- await browser_session.start()-- # Create two agents that will run in parallel- agent1 = Agent(- task='First parallel task',- llm=mock_llm,- browser_session=browser_session,- )-- agent2 = Agent(- task='Second parallel task',- llm=mock_llm,- browser_session=browser_session,- )-- # Run both agents in parallel on the same event loop- results = await asyncio.gather(agent1.run(), agent2.run())-- # Verify both agents completed successfully- assert len(results) == 2- for result in results:- assert len(result.history) > 0- last_history = result.history[-1]- 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 they share the same browser session (new behavior)- assert agent1.browser_session is agent2.browser_session- assert agent1.browser_session is browser_session- finally:- await browser_session.kill()-- @pytest.mark.skip('TODO: fix this')- async def test_one_event_loop_two_sequential_agents(self):- """Test one event loop with two different sequential agents"""- logger.info('Testing one event loop with two sequential agents')-- # Create mock LLM- mock_llm = create_mock_llm()-- # Create a shared browser session- browser_session = BrowserSession(- browser_profile=BrowserProfile(- headless=True,- user_data_dir=None, # Use temp directory- keep_alive=True,- )- )-- try:- await browser_session.start()-- # First agent- agent1 = Agent(- task='First sequential task',- llm=mock_llm,- browser_session=browser_session,- )- result1 = await agent1.run()-- # Second agent (runs after first completes)- agent2 = Agent(- task='Second sequential task',- llm=mock_llm,- browser_session=browser_session,- )- result2 = await agent2.run()-- # Verify both agents completed successfully- for result in [result1, result2]:- assert len(result.history) > 0- last_history = result.history[-1]- 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 they share the same browser session (new behavior)- assert agent1.browser_session is agent2.browser_session- assert agent1.browser_session is browser_session- finally:- await browser_session.kill()-- async def test_two_event_loops_sequential(self):- """Test two event loops, with one agent per loop sequential"""- logger.info('Testing two event loops with one agent per loop sequential')-- # Create mock LLM- mock_llm = create_mock_llm()-- # Just run agents sequentially in the same event loop- # This still tests sequential execution without creating new loops- agent1 = Agent(- task='First loop task',- llm=mock_llm,- browser_profile=BrowserProfile(headless=True, user_data_dir=None),- )- result1 = await agent1.run()-- agent2 = Agent(- task='Second loop task',- llm=mock_llm,- browser_profile=BrowserProfile(headless=True, user_data_dir=None),- )- result2 = await agent2.run()-- # Verify both agents completed successfully- for result in [result1, result2]:- assert len(result.history) > 0- last_history = result.history[-1]- 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)-- async def test_two_event_loops_one_per_thread(self):- """Test two event loops, one per thread, with one agent in each loop"""- logger.info('Testing two event loops, one per thread')-- # Create mock LLM- mock_llm = create_mock_llm()-- results = {}- errors = {}-- def run_agent_in_thread(thread_name, task_description):- """Run an agent in a new thread with its own event loop"""- try:- # Create new event loop for this thread- loop = asyncio.new_event_loop()- asyncio.set_event_loop(loop)-- async def run_agent():- agent = Agent(- task=task_description,- llm=mock_llm,- browser_profile=BrowserProfile(headless=True, user_data_dir=None),- )- return await agent.run()-- # Run the agent in this thread's event loop- result = loop.run_until_complete(run_agent())- results[thread_name] = result- except Exception as e:- errors[thread_name] = e- finally:- # Give asyncio tasks a moment to complete- try:- loop.run_until_complete(asyncio.sleep(0.1))- except Exception:- pass- # Cancel all pending tasks- try:- pending = asyncio.all_tasks(loop)- for task in pending:- task.cancel()- if pending:- loop.run_until_complete(asyncio.gather(*pending, return_exceptions=True))- except Exception:- pass- loop.stop()- loop.close()-- # Use run_in_executor to run threads- loop = asyncio.get_event_loop()- with ThreadPoolExecutor(max_workers=2) as executor:- future1 = loop.run_in_executor(executor, run_agent_in_thread, 'thread1', 'Thread 1 task')- future2 = loop.run_in_executor(executor, run_agent_in_thread, 'thread2', 'Thread 2 task')-- # Wait for both to complete- await asyncio.gather(future1, future2)-- # Check for errors- assert len(errors) == 0, f'Errors occurred: {errors}'-- # Verify both agents completed successfully- assert len(results) == 2- for result in results.values():- assert len(result.history) > 0- last_history = result.history[-1]- 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)-- def test_two_subprocesses_one_agent_per_subprocess(self):- """Test two subprocesses, with one agent per subprocess"""- logger.info('Testing two subprocesses with one agent per subprocess')-- # Use multiprocessing to run agents in separate processes- with multiprocessing.Pool(processes=2) as pool:- tasks = ['Subprocess 1 task', 'Subprocess 2 task']- results = pool.map(run_agent_in_subprocess_module, tasks)-- # Verify both agents completed successfully- assert len(results) == 2- for i, result in enumerate(results):- assert result['error'] is None, f'Process {i} error: {result["error"]}'- assert result['success'] is True-- @pytest.mark.skip('TODO: fix this')- async def test_shared_browser_session_multiple_tabs(self):- """Test multiple agents sharing same browser session with different tabs"""- logger.info('Testing shared browser session with multiple tabs')-- # Create action sequences - each agent creates a new tab- tab_action = """- {- "thinking": "null",- "evaluation_previous_goal": "Starting task",- "memory": "Need new tab",- "next_goal": "Create new tab",- "action": [- {- "go_to_url": {- "url": "https://example.com",- "new_tab": true- }- }- ]- }- """-- done_action = """- {- "thinking": "null",- "evaluation_previous_goal": "Tab created",- "memory": "Task done",- "next_goal": "Complete",- "action": [- {- "done": {- "text": "Task completed",- "success": true- }- }- ]- }- """-- # Create mocks with tab creation actions- mock_llm1 = create_mock_llm([tab_action, done_action])- mock_llm2 = create_mock_llm([tab_action, done_action])-- # Create shared browser session- shared_session = BrowserSession(- browser_profile=BrowserProfile(- headless=True,- user_data_dir=None,- keep_alive=True,- )- )-- try:- await shared_session.start()-- # Create agents sharing the session- agent1 = Agent(- task='Task in tab 1',- llm=mock_llm1,- browser_session=shared_session,- )-- agent2 = Agent(- task='Task in tab 2',- llm=mock_llm2,- browser_session=shared_session,- )-- # Run in parallel- results = await asyncio.gather(agent1.run(), agent2.run())-- # Verify success- for result in results:- assert len(result.history) > 0- last_history = result.history[-1]- 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 multiple tabs were created- tabs = await shared_session.get_tabs()- assert len(tabs) >= 2 # At least 2 tabs-- # Verify same browser session was used- assert agent1.browser_session == agent2.browser_session- assert agent1.browser_session == shared_session-- finally:- # Give playwright tasks a moment to complete before killing- await asyncio.sleep(0.1)- await shared_session.kill()- # Give playwright.stop() time to complete cleanup- await asyncio.sleep(0.1)-- @pytest.mark.skip('TODO: fix this')- async def test_reuse_browser_session_sequentially(self):- """Test reusing a browser session sequentially with keep_alive"""- logger.info('Testing sequential browser session reuse')-- # Create mock LLM- mock_llm = create_mock_llm()-- # Create a session with keep_alive- session = BrowserSession(- browser_profile=BrowserProfile(- headless=True,- user_data_dir=None,- keep_alive=True,- )- )-- try:- await session.start()- # 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 agent- agent1 = Agent(- task='First task',- llm=mock_llm,- browser_session=session,- )- 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- )- assert current_browser_pid is not None- assert current_browser_pid == initial_browser_pid-- # Second agent reusing session- agent2 = Agent(- task='Second task',- llm=mock_llm,- browser_session=session,- )- result2 = await agent2.run()-- # Verify success and same browser- for result in [result1, result2]:- assert len(result.history) > 0- last_history = result.history[-1]- 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- )- assert final_browser_pid == initial_browser_pid-- finally:- await session.kill()---if __name__ == '__main__':- asyncio.run(TestParallelism().test_one_event_loop_with_asyncio_run_and_one_task())diff --git a/tests/ci/test_agent_concurrency_sequential.py b/tests/ci/test_agent_concurrency_sequential.pydeleted file mode 100644index aca019d48..000000000--- a/tests/ci/test_agent_concurrency_sequential.py+++ /dev/null@@ -1,233 +0,0 @@-"""-Simple test to verify that sequential agents can reuse the same BrowserSession-without it being closed prematurely due to garbage collection.-"""--import asyncio-import gc--from browser_use import Agent, BrowserProfile, BrowserSession-from tests.ci.conftest import create_mock_llm---class TestSequentialAgentsSimple:- """Test that sequential agents can properly reuse the same BrowserSession"""-- async def test_sequential_agents_share_browser_session_simple(self, httpserver):- """Test that multiple agents can reuse the same browser session without it being closed"""- # Set up test HTML pages- httpserver.expect_request('/page1').respond_with_data('<html><body><h1>Page 1</h1></body></html>')- httpserver.expect_request('/page2').respond_with_data('<html><body><h1>Page 2</h1></body></html>')-- # Create a browser session with keep_alive=True- browser_session = BrowserSession(- browser_profile=BrowserProfile(- keep_alive=True,- headless=True,- user_data_dir=None, # Use temporary directory- )- )- await browser_session.start()-- # 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- )- # Browser PID detection may fail in CI environments- # The important thing is that the browser is connected- # Verify session is connected- try:- url = await browser_session.get_current_page_url()- assert url is not None- except Exception:- assert False, 'Browser session is not connected'-- # Agent 1: Navigate to page 1- agent1_actions = [- f"""{{- "thinking": "Navigating to page 1",- "evaluation_previous_goal": "Starting task",- "memory": "Need to navigate to page 1",- "next_goal": "Navigate to page 1",- "action": [- {{"go_to_url": {{"url": "{httpserver.url_for('/page1')}", "new_tab": false}}}}- ]- }}"""- ]-- agent1 = Agent(- task='Navigate to page 1',- llm=create_mock_llm(agent1_actions),- browser_session=browser_session,- )- history1 = await agent1.run(max_steps=2)- assert len(history1.history) >= 1- assert history1.history[-1].state.url == httpserver.url_for('/page1')-- # Verify browser session is still alive- # Verify session is still connected (checking internal state for test)- url = await browser_session.get_current_page_url()- assert url is not None- if 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- )- assert current_pid == initial_pid-- # Delete agent1 and force garbage collection- del agent1- gc.collect()- await asyncio.sleep(0.1) # Give time for any async cleanup-- # Verify browser is STILL alive after garbage collection- # Verify session is still connected (checking internal state for test)- url = await browser_session.get_current_page_url()- assert url is not None- if 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- )- assert current_pid == initial_pid- # Verify session is still connected- url = await browser_session.get_current_page_url()- assert url is not None-- # Agent 2: Navigate to page 2- agent2_actions = [- f"""{{- "thinking": "Navigating to page 2",- "evaluation_previous_goal": "Previous agent successfully navigated",- "memory": "Browser is still open, need to go to page 2",- "next_goal": "Navigate to page 2",- "action": [- {{"go_to_url": {{"url": "{httpserver.url_for('/page2')}", "new_tab": false}}}}- ]- }}"""- ]-- agent2 = Agent(- task='Navigate to page 2',- llm=create_mock_llm(agent2_actions),- browser_session=browser_session,- )- history2 = await agent2.run(max_steps=2)- assert len(history2.history) >= 1- assert history2.history[-1].state.url == httpserver.url_for('/page2')-- # Verify browser session is still alive after second agent- # Verify session is still connected (checking internal state for test)- url = await browser_session.get_current_page_url()- assert url is not None- if 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- )- assert current_pid == initial_pid- # Verify session is still connected- url = await browser_session.get_current_page_url()- assert url is not None-- # Clean up- await browser_session.kill()-- async def test_multiple_tabs_sequential_agents(self, httpserver):- """Test that sequential agents can work with multiple tabs"""- # Set up test pages- httpserver.expect_request('/tab1').respond_with_data('<html><body><h1>Tab 1</h1></body></html>')- httpserver.expect_request('/tab2').respond_with_data('<html><body><h1>Tab 2</h1></body></html>')-- browser_session = BrowserSession(- browser_profile=BrowserProfile(- keep_alive=True,- headless=True,- user_data_dir=None, # Use temporary directory- )- )- await browser_session.start()-- # Agent 1: Open two tabs- agent1_actions = [- f"""{{- "thinking": "Opening two tabs",- "evaluation_previous_goal": "Starting task",- "memory": "Need to open two tabs",- "next_goal": "Open tab 1 and tab 2",- "action": [- {{"go_to_url": {{"url": "{httpserver.url_for('/tab1')}", "new_tab": false}}}},- {{"go_to_url": {{"url": "{httpserver.url_for('/tab2')}", "new_tab": true}}}}- ]- }}"""- ]-- agent1 = Agent(- task='Open two tabs',- llm=create_mock_llm(agent1_actions),- browser_session=browser_session,- )- await agent1.run(max_steps=2)-- # Verify 2 tabs are open- # Check number of tabs- tabs = await browser_session.get_tabs()- assert len(tabs) == 2- # Agent1 should be on the second tab (tab2)- assert agent1.browser_session is not None- # Check agent1 is on tab2- url1 = await agent1.browser_session.get_current_page_url()- assert url1 is not None- assert '/tab2' in url1-- # Clean up agent1- del agent1- gc.collect()- await asyncio.sleep(0.1)-- # Agent 2: Switch to first tab- first_target_id = tabs[0].target_id- agent2_actions = [- f"""{{- "thinking": "Switching to first tab",- "evaluation_previous_goal": "Two tabs are open",- "memory": "Need to switch to tab {first_target_id[-4:]}",- "next_goal": "Switch to tab {first_target_id[-4:]}",- "action": [- {{"switch_tab": {{"tab_id": "{first_target_id[-4:]}"}}- ]- }}"""- ]-- agent2 = Agent(- task='Switch to first tab',- llm=create_mock_llm(agent2_actions),- browser_session=browser_session,- )- history2 = await agent2.run(max_steps=3)-- # Verify agent2 is on the first tab- assert agent2.browser_session is not None- # Check agent2 is on tab1- url2 = await agent2.browser_session.get_current_page_url()- assert url2 is not None- assert '/tab1' in url2-- # Verify browser is still functional- # Verify session is still connected (checking internal state for test)- url = await browser_session.get_current_page_url()- assert url is not None- # Check number of tabs- tabs = await browser_session.get_tabs()- assert len(tabs) == 2-- await browser_session.kill()diff --git a/tests/ci/test_agent_concurrency_shutdown.py b/tests/ci/test_agent_concurrency_shutdown.pydeleted file mode 100644index 62a20d2ed..000000000--- a/tests/ci/test_agent_concurrency_shutdown.py+++ /dev/null@@ -1,163 +0,0 @@-"""Test agent shutdown and cleanup."""--import asyncio-import logging-import threading-import time--from browser_use import Agent, BrowserProfile, BrowserSession-from tests.ci.conftest import create_mock_llm--logger = logging.getLogger(__name__)---async def test_agent_exits_within_10s():- """Test that agent with done() exits within 10 seconds."""- browser_session = BrowserSession(browser_profile=BrowserProfile(headless=True, keep_alive=False))- await browser_session.start()-- # Create mock LLM that returns done immediately (default behavior)- mock_llm = create_mock_llm()-- agent = Agent(- task='Test task',- llm=mock_llm,- browser_session=browser_session,- )-- start_time = time.time()- result = await agent.run(max_steps=1)- exit_time = time.time() - start_time-- assert result.is_done(), 'Agent should have completed'- assert result.is_successful(), 'Agent should have succeeded'- assert exit_time < 10, f'Agent took {exit_time:.2f}s to exit (should be < 10s)'-- # Verify browser session was cleaned up- assert browser_session._cdp_client_root is None, 'Browser session should be cleaned up'---async def test_no_leaked_asyncio_tasks():- """Test that no asyncio tasks remain after agent shutdown."""- # Get initial tasks- initial_tasks = asyncio.all_tasks(asyncio.get_event_loop())- initial_count = len(initial_tasks)-- browser_session = BrowserSession(browser_profile=BrowserProfile(headless=True, keep_alive=False))- await browser_session.start()-- mock_llm = create_mock_llm()-- agent = Agent(- task='Test task',- llm=mock_llm,- browser_session=browser_session,- )-- await agent.run(max_steps=1)-- # Wait briefly for cleanup- await asyncio.sleep(1)-- # Get final tasks- final_tasks = asyncio.all_tasks(asyncio.get_event_loop())- final_count = len(final_tasks)-- # Allow some tolerance for framework tasks- assert final_count <= initial_count + 1, f'Too many tasks remain: {final_count} (initial: {initial_count})'---async def test_no_leaked_threads():- """Test that no threads remain after agent shutdown."""- # Get initial threads (excluding main and asyncio)- initial_threads = [t for t in threading.enumerate() if t.name not in ['MainThread', 'asyncio_0']]- initial_count = len(initial_threads)-- browser_session = BrowserSession(browser_profile=BrowserProfile(headless=True, keep_alive=False))- await browser_session.start()-- mock_llm = create_mock_llm()-- agent = Agent(- task='Test task',- llm=mock_llm,- browser_session=browser_session,- )-- await agent.run(max_steps=1)-- # Wait briefly for cleanup- await asyncio.sleep(1)-- # Get final threads- final_threads = [t for t in threading.enumerate() if t.name not in ['MainThread', 'asyncio_0']]- final_count = len(final_threads)-- # No new threads should remain- assert final_count <= initial_count, f'New threads remain: {final_count} (initial: {initial_count})'---async def test_multiple_agents_cleanup():- """Test cleanup with multiple agents run sequentially."""- exit_times = []-- for i in range(3):- browser_session = BrowserSession(browser_profile=BrowserProfile(headless=True, keep_alive=False))- await browser_session.start()-- mock_llm = create_mock_llm()-- agent = Agent(- task=f'Test task {i + 1}',- llm=mock_llm,- browser_session=browser_session,- )-- start_time = time.time()- result = await agent.run(max_steps=1)- exit_time = time.time() - start_time-- assert result.is_done(), f'Agent {i + 1} should have completed'- exit_times.append(exit_time)-- # Small delay between agents- await asyncio.sleep(0.5)-- # All agents should exit quickly- for i, exit_time in enumerate(exit_times):- assert exit_time < 10, f'Agent {i + 1} took {exit_time:.2f}s to exit'-- # Check final state- tasks = asyncio.all_tasks(asyncio.get_event_loop())- threads = [t for t in threading.enumerate() if t.name not in ['MainThread', 'asyncio_0']]-- # Should be minimal tasks and no extra threads- assert len(tasks) <= 3, f'Too many tasks remain: {len(tasks)}'- assert len(threads) == 0, f'Extra threads remain: {len(threads)}'---async def test_browser_session_stop_method():- """Test that BrowserSession.stop() clears event buses without killing browser."""- browser_session = BrowserSession(- browser_profile=BrowserProfile(- headless=True,- keep_alive=True, # Keep alive so we can test stop()- )- )- await browser_session.start()-- # Verify browser is connected- assert browser_session._cdp_client_root is not None, 'Browser should be connected'-- # Call stop() - should clear event buses but keep browser alive- await browser_session.stop()-- # Browser should still be connected (since we didn't kill it)- # But event bus should be fresh- assert browser_session.event_bus is not None, 'Should have fresh event bus'-- # Now kill to clean up- await browser_session.kill()-- # After kill, browser should be disconnected- assert browser_session._cdp_client_root is None, 'Browser should be disconnected after kill'diff --git a/tests/ci/test_agent_gif_filtering.py b/tests/ci/test_agent_gif_filtering.pydeleted file mode 100644index 7ed07eaca..000000000--- a/tests/ci/test_agent_gif_filtering.py+++ /dev/null@@ -1,231 +0,0 @@-"""Test GIF generation filters out about:blank screenshots."""--import base64-import io--import pytest-from PIL import Image--from browser_use import AgentHistoryList-from browser_use.agent.gif import create_history_gif-from browser_use.agent.views import ActionResult, AgentHistory, AgentOutput-from browser_use.browser.views import BrowserStateHistory, TabInfo---async def httpserver_url(httpserver):- """Simple test page."""- httpserver.expect_request('/').respond_with_data(- """- <!DOCTYPE html>- <html>- <head><title>Test Page</title></head>- <body>- <h1>Test GIF Filtering</h1>- <p>This is a real page, not about:blank</p>- </body>- </html>- """,- content_type='text/html',- )- return httpserver.url_for('/')---def test_dir(tmp_path):- """Create a test directory that gets cleaned up after each test."""- test_path = tmp_path / 'test_gif_filtering'- test_path.mkdir(exist_ok=True)- yield test_path---def create_test_screenshot(width: int = 800, height: int = 600, color: tuple = (100, 150, 200)) -> str:- """Create a test screenshot as base64 string."""- img = Image.new('RGB', (width, height), color)- buffer = io.BytesIO()- img.save(buffer, format='PNG')- return base64.b64encode(buffer.getvalue()).decode('utf-8')---async def test_gif_filters_out_placeholder_screenshots(test_dir):- """Test that 4px placeholder screenshots from about:blank pages are filtered out of GIFs."""- # Set up screenshot service for testing (still needed to create test files)- from browser_use.screenshots.service import ScreenshotService-- screenshot_service = ScreenshotService(test_dir)-- # Helper function to store test screenshots- async def store_test_screenshot(screenshot_b64: str, step: int) -> str:- return await screenshot_service.store_screenshot(screenshot_b64, step)-- # Create a history with mixed screenshots: real and placeholder- history_items = []-- # Store test screenshots- real_screenshot_1_path = await store_test_screenshot(create_test_screenshot(800, 600, (100, 150, 200)), 2)- real_screenshot_2_path = await store_test_screenshot(create_test_screenshot(800, 600, (200, 100, 50)), 4)-- # First item: about:blank placeholder (should be filtered)- history_items.append(- AgentHistory(- model_output=AgentOutput(- evaluation_previous_goal='',- memory='',- next_goal='Starting task',- action=[],- ),- result=[ActionResult()],- state=BrowserStateHistory(- screenshot_path=None, # Placeholder doesn't have a file path- url='about:blank',- title='New Tab',- tabs=[TabInfo(target_id=1, url='about:blank', title='New Tab')],- interacted_element=[None],- ),- )- )-- # Second item: real screenshot- history_items.append(- AgentHistory(- model_output=AgentOutput(- evaluation_previous_goal='',- memory='',- next_goal='Navigate to example.com',- action=[],- ),- result=[ActionResult()],- state=BrowserStateHistory(- screenshot_path=real_screenshot_1_path,- url='https://example.com',- title='Example',- tabs=[TabInfo(target_id=1, url='https://example.com', title='Example')],- interacted_element=[None],- ),- )- )-- # Third item: another about:blank placeholder (should be filtered)- history_items.append(- AgentHistory(- model_output=AgentOutput(- evaluation_previous_goal='',- memory='',- next_goal='Opening new tab',- action=[],- ),- result=[ActionResult()],- state=BrowserStateHistory(- screenshot_path=None, # Placeholder doesn't have a file path- url='about:blank',- title='New Tab',- tabs=[TabInfo(target_id=2, url='about:blank', title='New Tab')],- interacted_element=[None],- ),- )- )-- # Fourth item: another real screenshot- history_items.append(- AgentHistory(- model_output=AgentOutput(- evaluation_previous_goal='',- memory='',- next_goal='Click on button',- action=[],- ),- result=[ActionResult()],- state=BrowserStateHistory(- screenshot_path=real_screenshot_2_path,- url='https://example.com/page2',- title='Page 2',- tabs=[TabInfo(target_id=1, url='https://example.com/page2', title='Page 2')],- interacted_element=[None],- ),- )- )-- # Create history list- history = AgentHistoryList(history=history_items)-- # Generate GIF- gif_path = test_dir / 'test_filtered.gif'- create_history_gif(- task='Test filtering about:blank screenshots',- history=history,- output_path=str(gif_path),- duration=500, # Shorter duration for testing- show_goals=True,- show_task=True,- )-- # Verify GIF was created- assert gif_path.exists(), 'GIF was not created'-- # Open the GIF and check the frames- with Image.open(gif_path) as img:- # Count frames- frame_count = 0- frame_sizes = []- try:- while True:- frame_sizes.append(img.size)- frame_count += 1- img.seek(img.tell() + 1)- except EOFError:- pass-- # We should have 3 frames total:- # 1. Task frame (created from first real screenshot)- # 2. Second real screenshot- # 3. Fourth real screenshot- # The two placeholder screenshots should be filtered out- assert frame_count == 3, f'Expected 3 frames (1 task + 2 real screenshots), got {frame_count}'-- # All frames should have the same size (800x600), not 4x4- for size in frame_sizes:- assert size == (800, 600), f'Frame has incorrect size: {size}. Placeholder images may not have been filtered.'---async def test_gif_handles_all_placeholders(test_dir):- """Test that GIF generation handles case where all screenshots are placeholders."""- # Create a history with only placeholder screenshots- history_items = []-- for i in range(3):- history_items.append(- AgentHistory(- model_output=AgentOutput(- evaluation_previous_goal='',- memory='',- next_goal=f'Step {i + 1}',- action=[],- ),- result=[ActionResult()],- state=BrowserStateHistory(- screenshot_path=None, # Placeholder doesn't have a file path- url='about:blank',- title='New Tab',- tabs=[TabInfo(target_id=1, url='about:blank', title='New Tab')],- interacted_element=[None],- ),- )- )-- history = AgentHistoryList(history=history_items)-- # Generate GIF - should handle gracefully- gif_path = test_dir / 'test_all_placeholders.gif'- create_history_gif(- task='Test all placeholders',- history=history,- output_path=str(gif_path),- duration=500,- )-- # With all placeholders filtered, no GIF should be created- assert not gif_path.exists(), 'GIF should not be created when all screenshots are placeholders'---# Note: Removing the agent integration test due to sandbox issues in CI-# The unit tests above adequately verify the GIF filtering functionalitydiff --git a/tests/ci/test_agent_gif_generation_with_navigation.py b/tests/ci/test_agent_gif_generation_with_navigation.pydeleted file mode 100644index 440f83621..000000000--- a/tests/ci/test_agent_gif_generation_with_navigation.py+++ /dev/null@@ -1,221 +0,0 @@-"""Test that GIF generation works properly when real navigation happens."""--import asyncio--from browser_use import Agent, AgentHistoryList-from browser_use.browser import BrowserProfile, BrowserSession-from tests.ci.conftest import create_mock_llm---async def test_gif_generation_with_real_navigation(httpserver, tmp_path):- """Test that GIF is properly generated when agent navigates to a real page."""- # Set up a test page with visible content- httpserver.expect_request('/').respond_with_data(- """- <!DOCTYPE html>- <html>- <head>- <title>Test Page for GIF</title>- <style>- body {- background-color: #f0f0f0;- font-family: Arial, sans-serif;- padding: 50px;- }- h1 {- color: #333;- font-size: 48px;- }- .content {- background: white;- padding: 30px;- border-radius: 10px;- box-shadow: 0 2px 10px rgba(0,0,0,0.1);- }- </style>- </head>- <body>- <div class="content">- <h1>Test Page for GIF Generation</h1>- <p>This page has real content that should appear in the GIF.</p>- <button id="test-button">Click Me</button>- </div>- </body>- </html>- """,- content_type='text/html',- )-- # Create a mock LLM that navigates then completes- navigate_action = f'{{"action": [{{"go_to_url": {{"url": "{httpserver.url_for("/")}"}}}}]}}'- done_action = '{"action": [{"done": {"text": "Navigated successfully", "success": true}}]}'- llm_with_navigation = create_mock_llm(actions=[navigate_action, done_action])-- # Set up output path- gif_path = tmp_path / 'test_agent.gif'-- browser_session = BrowserSession(browser_profile=BrowserProfile(headless=True, disable_security=True, user_data_dir=None))- await browser_session.start()-- try:- agent = Agent(- task=f'Navigate to {httpserver.url_for("/")} and verify the page loads',- llm=llm_with_navigation,- browser_session=browser_session,- generate_gif=str(gif_path),- )-- history: AgentHistoryList = await agent.run(max_steps=3)-- # Verify the task completed- result = history.final_result()- assert result is not None- # result is a string, not an object with success attribute-- # Give a moment for GIF to be written- await asyncio.sleep(0.1)-- # Verify GIF was created- assert gif_path.exists(), f'GIF was not created at {gif_path}'-- # Verify GIF has substantial content (not just placeholders)- gif_size = gif_path.stat().st_size- assert gif_size > 10000, f'GIF file is too small ({gif_size} bytes), likely only contains placeholders'-- # Verify history contains real screenshots (not placeholders)- has_real_screenshot = False- for item in history.history:- screenshot_b64 = item.state.get_screenshot()- if (- screenshot_b64- and screenshot_b64- != 'iVBORw0KGgoAAAANSUhEUgAAAAQAAAAECAIAAAAmkwkpAAAAFElEQVR4nGP8//8/AwwwMSAB3BwAlm4DBfIlvvkAAAAASUVORK5CYII='- ):- has_real_screenshot = True- break-- assert has_real_screenshot, 'No real screenshots found in history, only placeholders'-- finally:- await browser_session.kill()---async def test_gif_generation_without_vision(httpserver, tmp_path):- """Test that GIF is generated even when use_vision=False (issue #2615)."""- # Set up a test page with visible content- httpserver.expect_request('/').respond_with_data(- """- <!DOCTYPE html>- <html>- <head>- <title>Test Page - No Vision Mode</title>- <style>- body {- background-color: #e0f0ff;- font-family: Arial, sans-serif;- padding: 50px;- }- h1 {- color: #0066cc;- font-size: 48px;- }- .content {- background: white;- padding: 30px;- border-radius: 10px;- box-shadow: 0 2px 10px rgba(0,0,0,0.1);- }- </style>- </head>- <body>- <div class="content">- <h1>No Vision Mode Test</h1>- <p>This page should generate a GIF even when use_vision=False.</p>- <button id="test-button">Test Button</button>- </div>- </body>- </html>- """,- content_type='text/html',- )-- # Create a mock LLM that navigates then completes- navigate_action = f'{{"action": [{{"go_to_url": {{"url": "{httpserver.url_for("/")}"}}}}]}}'- done_action = '{"action": [{"done": {"text": "Successfully tested without vision", "success": true}}]}'- llm_no_vision = create_mock_llm(actions=[navigate_action, done_action])-- # Set up output path- gif_path = tmp_path / 'no_vision_test.gif'-- browser_session = BrowserSession(browser_profile=BrowserProfile(headless=True, disable_security=True, user_data_dir=None))- await browser_session.start()-- try:- agent = Agent(- task=f'Navigate to {httpserver.url_for("/")} without using vision',- llm=llm_no_vision,- browser_session=browser_session,- use_vision=False, # Key: disable vision- generate_gif=str(gif_path), # Key: enable GIF generation- )-- history: AgentHistoryList = await agent.run(max_steps=3)-- # Verify the task completed- result = history.final_result()- assert result is not None-- # Give a moment for GIF to be written- await asyncio.sleep(0.1)-- # Verify GIF was created even without vision- assert gif_path.exists(), f'GIF was not created at {gif_path} when use_vision=False'-- # Verify GIF has content (non-zero size)- assert gif_path.stat().st_size > 0, f'GIF file is empty at {gif_path}'-- # Verify we have screenshots in history for GIF generation- screenshots = history.screenshots(return_none_if_not_screenshot=True)- assert screenshots, 'No screenshots found in history for GIF generation'-- # Verify at least one valid screenshot exists (not all placeholders)- valid_screenshots = [- s- for s in screenshots- if s and s != 'iVBORw0KGgoAAAANSUhEUgAAAAQAAAAECAYAAACp8Z5+AAAAE0lEQVR42mP8/5+BgYGBgYGBgQEAAP//AwMC/wE='- ]- assert valid_screenshots, 'No valid screenshots found for GIF generation'-- finally:- await browser_session.kill()---async def test_gif_not_created_when_only_placeholders(tmp_path):- """Test that no GIF is created when all screenshots are placeholders."""- # Use default mock LLM that just returns done without navigation- llm = create_mock_llm()-- gif_path = tmp_path / 'should_not_exist.gif'-- browser_session = BrowserSession(browser_profile=BrowserProfile(headless=True, disable_security=True, user_data_dir=None))- await browser_session.start()-- try:- agent = Agent(- task='Just complete without navigation',- llm=llm,- browser_session=browser_session,- generate_gif=str(gif_path),- )-- history: AgentHistoryList = await agent.run(max_steps=2)-- # Task should complete- result = history.final_result()- assert result is not None-- # GIF should NOT be created- assert not gif_path.exists(), 'GIF should not be created when all screenshots are placeholders'-- finally:- await browser_session.kill()diff --git a/tests/ci/test_browser_session_ownership.py b/tests/ci/test_browser_session_ownership.pydeleted file mode 100644index 2e0246272..000000000--- a/tests/ci/test_browser_session_ownership.py+++ /dev/null@@ -1,223 +0,0 @@-"""-Test browser resource ownership when BrowserSession is copied.-"""--import asyncio-import gc--import pytest--pytest.skip('TODO: fix - test timeouts', allow_module_level=True)--from browser_use import Agent, BrowserProfile, BrowserSession-from tests.ci.conftest import create_mock_llm---class TestBrowserOwnership:- """Test that browser resource ownership is properly managed when sessions are copied."""-- async def test_copied_session_doesnt_own_browser(self):- """Test that copied BrowserSession instances don't own the browser resources."""- # Create original browser session- original = BrowserSession(- browser_profile=BrowserProfile(- headless=True,- keep_alive=False, # Should still work even with keep_alive=False- user_data_dir=None,- )- )-- # Start the browser session to initialize watchdogs- await original.start()-- # Verify original owns the browser- assert original._owns_browser_resources is True-- # Create a copy- copy1 = original.model_copy()-- # Verify copy doesn't own the browser- assert copy1._owns_browser_resources is False- # Note: _original_browser_session is not implemented in current architecture-- # Create another copy- copy2 = original.model_copy()- assert copy2._owns_browser_resources is False- # Note: _original_browser_session is not implemented in current architecture-- # Clean up- await original.stop()-- @pytest.mark.skip('TODO: fix this')- async def test_agent_copies_dont_kill_browser(self, httpserver):- """Test that when agents use a browser session, they don't kill the browser when garbage collected."""- # Set up test page- httpserver.expect_request('/test').respond_with_data(- '<html><body><h1>Test Page</h1><button>Click me</button></body></html>'- )-- # Create original browser session with keep_alive=True to prevent agent from closing it- original_session = BrowserSession(- browser_profile=BrowserProfile(- headless=True,- keep_alive=True, # Keep browser alive when agents complete- user_data_dir=None,- )- )- await original_session.start()- initial_browser_pid = original_session.browser_pid- # assert initial_browser_pid is not None, 'Browser PID should always exist after launch'-- # Create first agent that will use the session- async def run_agent1():- agent1 = Agent(- task='Navigate to test page',- llm=create_mock_llm(- [- f'''{{- "thinking": "Navigating to test page",- "evaluation_previous_goal": "Starting task",- "memory": "Need to navigate",- "next_goal": "Navigate to test page",- "action": [- {{- "go_to_url": {{- "url": "{httpserver.url_for('/test')}",- "new_tab": false- }}- }}- ]- }}'''- ]- ),- browser_session=original_session,- )- await agent1.run(max_steps=1)- # agent1 uses the original session directly (no copy when owns_browser_resources=True)- assert agent1.browser_session is not None- assert agent1.browser_session is original_session- assert agent1.browser_session._owns_browser_resources is True- # Browser should still be running- assert original_session.browser_pid == initial_browser_pid-- await run_agent1()-- # Force garbage collection to ensure agent1 is cleaned up- gc.collect()- await asyncio.sleep(0.1)-- # Browser should still be alive because the original session still owns it- assert await original_session.is_connected(restart=False)- assert original_session.browser_pid == initial_browser_pid-- # Create second agent- async def run_agent2():- agent2 = Agent(- task='Click the button',- llm=create_mock_llm(- [- """{- "thinking": "Clicking button",- "evaluation_previous_goal": "On test page",- "memory": "Need to click button",- "next_goal": "Click button",- "action": [- {- "click_element_by_index": {- "index": 0- }- }- ]- }"""- ]- ),- browser_session=original_session,- )- await agent2.run(max_steps=1)- assert agent2.browser_session is not None- assert agent2.browser_session is original_session- assert agent2.browser_session._owns_browser_resources is True-- await run_agent2()-- # Force garbage collection again- gc.collect()- await asyncio.sleep(0.1)-- # Browser should still be alive- assert await original_session.is_connected(restart=False)- assert original_session.browser_pid == initial_browser_pid-- # Now clean up the original - use kill() since keep_alive=True- await original_session.kill()-- # Browser should be disconnected now- assert not await original_session.is_connected(restart=False)-- async def test_shared_browser_recovery_after_disconnect(self, httpserver):- """Test that copied sessions can recover if the shared browser is disconnected."""- # Set up test page- httpserver.expect_request('/test').respond_with_data('<html><body><h1>Recovery Test</h1></body></html>')-- # Create original browser session- original_session = BrowserSession(- browser_profile=BrowserProfile(- headless=True,- keep_alive=True, # Keep alive for sharing- user_data_dir=None,- )- )- await original_session.start()-- # Create a copy (simulating what Agent does)- copied_session = original_session.model_copy()- copied_session._playwright = original_session._playwright- copied_session._browser = original_session._browser- copied_session._browser_context = original_session._browser_context- # Note: page is a property, not a directly assignable attribute-- # Verify copy doesn't own the browser- assert copied_session._owns_browser_resources is False-- # Navigate using the copy- await copied_session.create_new_tab(httpserver.url_for('/test'))-- # Force disconnect the browser context- assert original_session.browser_context is not None- await original_session.browser_context.close()-- # Try to use the copy - it should recover- await copied_session.start() # Should reconnect- screenshot = await copied_session.take_screenshot()- assert screenshot is not None- assert len(screenshot) > 0-- # Clean up- await original_session.kill()-- async def test_multiple_copies_single_owner(self):- """Test that multiple copies all reference the same original owner."""- original = BrowserSession(- browser_profile=BrowserProfile(- headless=True,- user_data_dir=None,- )- )-- # Create multiple copies- copies = []- for i in range(5):- copy = original.model_copy()- copies.append(copy)-- # Verify each copy doesn't own the browser- assert copy._owns_browser_resources is False- # Note: _original_browser_session is not implemented in current architecture-- # Note: _original_browser_session is not implemented in current architecture- # All copies should reference the same original- # for copy in copies:- # assert copy._original_browser_session is original-- # Only the original owns the browser- assert original._owns_browser_resources is Truediff --git a/tests/ci/test_config_new.py b/tests/ci/test_config_new.pydeleted file mode 100644index 6492da30a..000000000--- a/tests/ci/test_config_new.py+++ /dev/null@@ -1,318 +0,0 @@-"""Tests for new configuration system with migration support."""--import json-import os-import tempfile-from pathlib import Path--import pytest--from browser_use.config import (- CONFIG,- FlatEnvConfig,- get_default_llm,- get_default_profile,- load_and_migrate_config,- load_browser_use_config,-)---class TestConfigCreation:- """Test configuration creation and handling."""-- def test_create_default_config(self):- """Test migrating simple old config format."""- with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f:- old_config = {- 'headless': True,- 'allowed_domains': ['example.com', 'test.com'],- 'api_key': 'sk-test123',- 'model': 'gpt-4o',- 'temperature': 0.7,- }- json.dump(old_config, f)- config_path = Path(f.name)-- try:- # Load - should detect old format and create fresh config- db_config = load_and_migrate_config(config_path)-- # Check fresh config was created- assert len(db_config.browser_profile) == 1- assert len(db_config.llm) == 1- assert len(db_config.agent) == 1-- # Get the default profile- profile = next(iter(db_config.browser_profile.values()))- assert profile.default is True- assert profile.headless is False # Default value, not from old config- assert profile.user_data_dir is None-- # Get the default LLM config- llm = next(iter(db_config.llm.values()))- assert llm.default is True- assert llm.api_key == 'your-openai-api-key-here' # Default placeholder- assert llm.model == 'gpt-4o'-- # Verify file was updated- with open(config_path) as f:- saved_config = json.load(f)- assert 'browser_profile' in saved_config- assert 'llm' in saved_config- assert 'agent' in saved_config-- finally:- os.unlink(config_path)-- def test_create_config_on_empty(self):- """Test creating default config when no file exists."""- with tempfile.TemporaryDirectory() as tmpdir:- config_path = Path(tmpdir) / 'config.json'-- # Load - should create fresh config- db_config = load_and_migrate_config(config_path)-- # Check fresh config was created- assert config_path.exists()- assert len(db_config.browser_profile) == 1- assert len(db_config.llm) == 1- assert len(db_config.agent) == 1-- # Verify defaults- profile = next(iter(db_config.browser_profile.values()))- assert profile.default is True- assert profile.headless is False-- llm = next(iter(db_config.llm.values()))- assert llm.default is True- assert llm.model == 'gpt-4o'-- def test_no_migration_for_new_format(self):- """Test that new format is not migrated."""- with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f:- new_config = {- 'browser_profile': {- 'uuid1': {'id': 'uuid1', 'default': True, 'created_at': '2024-01-01T00:00:00', 'headless': True}- },- 'llm': {'uuid2': {'id': 'uuid2', 'default': True, 'created_at': '2024-01-01T00:00:00', 'api_key': 'sk-new'}},- 'agent': {},- }- json.dump(new_config, f)- config_path = Path(f.name)-- try:- # Load without migration- original_content = json.dumps(new_config, sort_keys=True)- db_config = load_and_migrate_config(config_path)-- # Check it wasn't modified- with open(config_path) as f:- saved_content = json.dumps(json.load(f), sort_keys=True)-- # Should be the same (no migration)- assert len(db_config.browser_profile) == 1- assert db_config.browser_profile['uuid1'].id == 'uuid1'- assert db_config.llm['uuid2'].api_key == 'sk-new'-- finally:- os.unlink(config_path)---class TestConfigLazyLoading:- """Test lazy loading of environment variables."""-- def test_env_vars_reload_on_access(self):- """Test that env vars are re-read on every access."""- original = os.environ.get('BROWSER_USE_LOGGING_LEVEL', '')- try:- # Set initial value- os.environ['BROWSER_USE_LOGGING_LEVEL'] = 'debug'- assert CONFIG.BROWSER_USE_LOGGING_LEVEL == 'debug'-- # Change value- os.environ['BROWSER_USE_LOGGING_LEVEL'] = 'error'- assert CONFIG.BROWSER_USE_LOGGING_LEVEL == 'error'-- # Remove to test default- del os.environ['BROWSER_USE_LOGGING_LEVEL']- assert CONFIG.BROWSER_USE_LOGGING_LEVEL == 'info'-- finally:- if original:- os.environ['BROWSER_USE_LOGGING_LEVEL'] = original- else:- os.environ.pop('BROWSER_USE_LOGGING_LEVEL', None)-- def test_mcp_env_vars(self):- """Test MCP-specific environment variables."""- originals = {- 'BROWSER_USE_HEADLESS': os.environ.get('BROWSER_USE_HEADLESS', ''),- 'BROWSER_USE_ALLOWED_DOMAINS': os.environ.get('BROWSER_USE_ALLOWED_DOMAINS', ''),- 'BROWSER_USE_LLM_MODEL': os.environ.get('BROWSER_USE_LLM_MODEL', ''),- }-- try:- # Test headless- os.environ['BROWSER_USE_HEADLESS'] = 'true'- env_config = FlatEnvConfig()- assert env_config.BROWSER_USE_HEADLESS is True-- os.environ['BROWSER_USE_HEADLESS'] = 'false'- env_config = FlatEnvConfig()- assert env_config.BROWSER_USE_HEADLESS is False-- # Test allowed domains- os.environ['BROWSER_USE_ALLOWED_DOMAINS'] = 'example.com,test.com'- env_config = FlatEnvConfig()- assert env_config.BROWSER_USE_ALLOWED_DOMAINS == 'example.com,test.com'-- # Test LLM model- os.environ['BROWSER_USE_LLM_MODEL'] = 'gpt-4-turbo'- env_config = FlatEnvConfig()- assert env_config.BROWSER_USE_LLM_MODEL == 'gpt-4-turbo'-- finally:- for key, value in originals.items():- if value:- os.environ[key] = value- else:- os.environ.pop(key, None)---class TestConfigMerging:- """Test configuration merging with environment overrides."""-- def test_load_config_with_env_overrides(self):- """Test that env vars override config.json values."""- originals = {- 'BROWSER_USE_CONFIG_PATH': os.environ.get('BROWSER_USE_CONFIG_PATH', ''),- 'BROWSER_USE_HEADLESS': os.environ.get('BROWSER_USE_HEADLESS', ''),- 'OPENAI_API_KEY': os.environ.get('OPENAI_API_KEY', ''),- 'BROWSER_USE_LLM_MODEL': os.environ.get('BROWSER_USE_LLM_MODEL', ''),- }-- with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f:- config = {- 'browser_profile': {- 'uuid1': {'id': 'uuid1', 'default': True, 'created_at': '2024-01-01T00:00:00', 'headless': False}- },- 'llm': {- 'uuid2': {- 'id': 'uuid2',- 'default': True,- 'created_at': '2024-01-01T00:00:00',- 'api_key': 'sk-config',- 'model': 'gpt-3.5-turbo',- }- },- 'agent': {},- }- json.dump(config, f)- config_path = f.name-- try:- # Set env vars to override- os.environ['BROWSER_USE_CONFIG_PATH'] = config_path- os.environ['BROWSER_USE_HEADLESS'] = 'true'- os.environ['OPENAI_API_KEY'] = 'sk-env'- os.environ['BROWSER_USE_LLM_MODEL'] = 'gpt-4o'-- # Load config- merged_config = load_browser_use_config()-- # Check overrides- assert merged_config['browser_profile']['headless'] is True # overridden- assert merged_config['llm']['api_key'] == 'sk-env' # overridden- assert merged_config['llm']['model'] == 'gpt-4o' # overridden-- finally:- os.unlink(config_path)- for key, value in originals.items():- if value:- os.environ[key] = value- else:- os.environ.pop(key, None)-- def test_get_default_helpers(self):- """Test helper functions for getting default configs."""- config = {- 'browser_profile': {'headless': True, 'allowed_domains': ['test.com']},- 'llm': {'api_key': 'sk-test', 'model': 'gpt-4o'},- }-- profile = get_default_profile(config)- assert profile == {'headless': True, 'allowed_domains': ['test.com']}-- llm = get_default_llm(config)- assert llm == {'api_key': 'sk-test', 'model': 'gpt-4o'}-- # Test empty- assert get_default_profile({}) == {}- assert get_default_llm({}) == {}---class TestBackwardCompatibility:- """Test backward compatibility with old CONFIG usage."""-- def test_all_old_config_attributes_exist(self):- """Test that all attributes from old Config class are accessible."""- # Test all the attributes that exist in the old config- attrs_to_test = [- 'BROWSER_USE_LOGGING_LEVEL',- 'ANONYMIZED_TELEMETRY',- 'BROWSER_USE_CLOUD_SYNC',- 'BROWSER_USE_CLOUD_API_URL',- 'BROWSER_USE_CLOUD_UI_URL',- 'XDG_CACHE_HOME',- 'XDG_CONFIG_HOME',- 'BROWSER_USE_CONFIG_DIR',- 'BROWSER_USE_CONFIG_FILE',- 'BROWSER_USE_PROFILES_DIR',- 'BROWSER_USE_DEFAULT_USER_DATA_DIR',- 'OPENAI_API_KEY',- 'ANTHROPIC_API_KEY',- 'GOOGLE_API_KEY',- 'DEEPSEEK_API_KEY',- 'GROK_API_KEY',- 'NOVITA_API_KEY',- 'AZURE_OPENAI_ENDPOINT',- 'AZURE_OPENAI_KEY',- 'SKIP_LLM_API_KEY_VERIFICATION',- 'IN_DOCKER',- 'IS_IN_EVALS',- 'WIN_FONT_DIR',- ]-- for attr in attrs_to_test:- # Should not raise AttributeError- value = getattr(CONFIG, attr)- assert value is not None or value == '' or isinstance(value, (str, bool, Path))-- def test_computed_properties_work(self):- """Test computed properties like BROWSER_USE_CLOUD_SYNC."""- telemetry_orig = os.environ.get('ANONYMIZED_TELEMETRY', '')- sync_orig = os.environ.get('BROWSER_USE_CLOUD_SYNC', '')-- try:- # Test inheritance- os.environ['ANONYMIZED_TELEMETRY'] = 'true'- os.environ.pop('BROWSER_USE_CLOUD_SYNC', None)- assert CONFIG.BROWSER_USE_CLOUD_SYNC is True-- # Test override- os.environ['BROWSER_USE_CLOUD_SYNC'] = 'false'- assert CONFIG.BROWSER_USE_CLOUD_SYNC is False-- finally:- if telemetry_orig:- os.environ['ANONYMIZED_TELEMETRY'] = telemetry_orig- else:- os.environ.pop('ANONYMIZED_TELEMETRY', None)- if sync_orig:- os.environ['BROWSER_USE_CLOUD_SYNC'] = sync_orig- else:- os.environ.pop('BROWSER_USE_CLOUD_SYNC', None)---if __name__ == '__main__':- pytest.main([__file__, '-v', '-s'])diff --git a/tests/ci/test_mcp_client.py b/tests/ci/test_mcp_client.pydeleted file mode 100644index 92ab9b8d0..000000000--- a/tests/ci/test_mcp_client.py+++ /dev/null@@ -1,848 +0,0 @@-"""Tests for MCP (Model Context Protocol) client integration with real MCP server."""--import json-import sys-import tempfile-from pathlib import Path--# Import MCP SDK for creating test server-import mcp.server.stdio-import mcp.types as types-import pytest-from mcp.server import NotificationOptions, Server-from mcp.server.models import InitializationOptions-from pytest_httpserver import HTTPServer--from browser_use import ActionResult, Agent, BrowserProfile, BrowserSession, Tools-from browser_use.mcp.client import MCPClient---class MockMCPServer:- """A minimal MCP server for testing."""-- def __init__(self):- self.server = Server('test-mcp-server')- self.call_history = [] # Track all tool calls- self._setup_handlers()-- def _setup_handlers(self):- """Setup MCP server handlers."""-- @self.server.list_tools()- async def handle_list_tools() -> list[types.Tool]:- """List available test tools."""- return [- types.Tool(- name='count_to_n',- description='Count from 1 to n and return the numbers',- inputSchema={- 'type': 'object',- 'properties': {'n': {'type': 'integer', 'description': 'Number to count to'}},- 'required': ['n'],- },- ),- types.Tool(- name='echo_message',- description='Echo back a message with a prefix',- inputSchema={- 'type': 'object',- 'properties': {- 'message': {'type': 'string', 'description': 'Message to echo'},- 'prefix': {'type': 'string', 'description': 'Prefix to add', 'default': 'Echo:'},- },- 'required': ['message'],- },- ),- types.Tool(- name='get_test_data',- description='Get some test data as JSON',- inputSchema={'type': 'object', 'properties': {}},- ),- types.Tool(- name='process_trace_update',- description='Process a cognitive trace update with nested object parameter',- inputSchema={- 'type': 'object',- 'properties': {- 'trace': {- 'type': 'object',- 'properties': {- 'recent_actions': {- 'type': 'array',- 'items': {'type': 'string'},- 'description': 'List of recent action names',- },- 'current_context': {- 'type': 'string',- 'description': 'Current environment context or state',- },- 'goal': {- 'type': 'string',- 'description': 'Current goal being pursued',- },- },- 'required': ['recent_actions', 'goal'],- 'additionalProperties': False,- },- 'window_size': {- 'type': 'number',- 'description': 'Size of the monitoring window',- 'default': 10,- },- },- 'required': ['trace'],- 'additionalProperties': False,- },- ),- types.Tool(- name='process_array_data',- description='Process various array types',- inputSchema={- 'type': 'object',- 'properties': {- 'string_list': {- 'type': 'array',- 'items': {'type': 'string'},- 'description': 'List of strings',- },- 'number_list': {- 'type': 'array',- 'items': {'type': 'number'},- 'description': 'List of numbers',- },- 'config_list': {- 'type': 'array',- 'items': {- 'type': 'object',- 'properties': {- 'name': {'type': 'string'},- 'value': {'type': 'integer'},- 'enabled': {'type': 'boolean', 'default': True},- },- 'required': ['name', 'value'],- },- 'description': 'List of configuration objects',- },- 'simple_array': {- 'type': 'array',- 'description': 'Array without item type specified',- },- },- 'required': ['string_list'],- },- ),- ]-- @self.server.call_tool()- async def handle_call_tool(name: str, arguments: dict | None) -> list[types.TextContent]:- """Handle tool execution."""- # Record the call- self.call_history.append({'tool': name, 'arguments': arguments or {}})-- if name == 'count_to_n':- assert arguments is not None- n = arguments.get('n', 5)- numbers = ', '.join(str(i) for i in range(1, n + 1))- result = f'Counted to {n}: {numbers}'-- elif name == 'echo_message':- assert arguments is not None- message = arguments.get('message', '')- prefix = arguments.get('prefix', 'Echo:')- result = f'{prefix} {message}'-- elif name == 'get_test_data':- data = {'status': 'success', 'items': ['apple', 'banana', 'cherry'], 'count': 3}- result = json.dumps(data, indent=2)-- elif name == 'process_trace_update':- assert arguments is not None- trace = arguments.get('trace', {})- window_size = arguments.get('window_size', 10)-- recent_actions = trace.get('recent_actions', [])- current_context = trace.get('current_context', 'unknown')- goal = trace.get('goal', 'no goal')-- result = f'Processed trace update: {len(recent_actions)} actions, goal: {goal}, context: {current_context}, window: {window_size}'-- elif name == 'process_array_data':- assert arguments is not None- string_list = arguments.get('string_list', [])- number_list = arguments.get('number_list', [])- config_list = arguments.get('config_list', [])- simple_array = arguments.get('simple_array', [])-- config_summary = f'{len(config_list)} configs' if config_list else 'no configs'- result = f'Processed arrays: strings={len(string_list)}, numbers={len(number_list)}, {config_summary}, simple={len(simple_array)}'-- else:- result = f'Unknown tool: {name}'-- return [types.TextContent(type='text', text=result)]-- async def run(self):- """Run the MCP server."""- async with mcp.server.stdio.stdio_server() as (read_stream, write_stream):- await self.server.run(- read_stream,- write_stream,- InitializationOptions(- server_name='test-mcp-server',- server_version='0.1.0',- capabilities=self.server.get_capabilities(- notification_options=NotificationOptions(),- experimental_capabilities={},- ),- ),- )---# Create test server script that will run in subprocess-TEST_SERVER_CODE = """-import asyncio-import sys-sys.path.insert(0, "{project_root}")--from tests.ci.test_mcp_client import MockMCPServer--async def main():- server = MockMCPServer()- await server.run()--if __name__ == "__main__":- asyncio.run(main())-"""---async def test_mcp_server_script():- """Create a temporary script file for the test MCP server."""- with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:- project_root = str(Path(__file__).parent.parent.parent)- f.write(TEST_SERVER_CODE.format(project_root=project_root))- script_path = f.name-- yield script_path-- Path(script_path).unlink(missing_ok=True)---async def test_mcp_client_basic_connection(test_mcp_server_script):- """Test basic MCP client connection and tool discovery."""- tools = Tools()-- mcp_client = MCPClient(server_name='test-server', command=sys.executable, args=[test_mcp_server_script])-- try:- # Connect to server- await mcp_client.connect()-- # Verify tools were discovered- assert len(mcp_client._tools) == 5- assert 'count_to_n' in mcp_client._tools- assert 'echo_message' in mcp_client._tools- assert 'get_test_data' in mcp_client._tools- assert 'process_trace_update' in mcp_client._tools- assert 'process_array_data' in mcp_client._tools-- # Register tools to tools- await mcp_client.register_to_tools(tools)-- # Verify tools are registered as actions- actions = tools.registry.registry.actions- assert 'count_to_n' in actions- assert 'echo_message' in actions- assert 'get_test_data' in actions-- # Test executing a tool- echo_action = actions['echo_message']- # Need to create param model instance- params = echo_action.param_model(message='Hello MCP', prefix='Test:')- result = await echo_action.function(params=params)-- assert result.extracted_content == 'Test: Hello MCP'- assert 'echo_message' in result.long_term_memory- assert 'test-server' in result.long_term_memory-- finally:- await mcp_client.disconnect()---async def test_mcp_tools_with_agent(test_mcp_server_script, httpserver: HTTPServer):- """Test that agent can discover and use MCP tools."""- # Set up test webpage- httpserver.expect_request('/').respond_with_data(- """- <html>- <body>- <h1>Test Page</h1>- <p>Count to 5 and echo a message</p>- </body>- </html>- """,- content_type='text/html',- )-- browser_session = BrowserSession(browser_profile=BrowserProfile(headless=True, user_data_dir=None, keep_alive=True))- await browser_session.start()- tools = Tools()-- # Connect MCP client- mcp_client = MCPClient(server_name='test-server', command=sys.executable, args=[test_mcp_server_script])-- try:- await mcp_client.connect()- await mcp_client.register_to_tools(tools)-- # Import create_mock_llm from conftest- from tests.ci.conftest import create_mock_llm-- # Create mock LLM with specific actions- actions = [- f'{{"thinking": null, "evaluation_previous_goal": "Starting", "memory": "Starting task", "next_goal": "Navigate to test page", "action": [{{"go_to_url": {{"url": "{httpserver.url_for("/")}", "new_tab": false}}}}]}}',- '{"thinking": null, "evaluation_previous_goal": "Navigated", "memory": "On test page", "next_goal": "Count to 3", "action": [{"count_to_n": {"n": 3}}]}',- '{"thinking": null, "evaluation_previous_goal": "Counted", "memory": "Counted to 3", "next_goal": "Echo message", "action": [{"echo_message": {"message": "MCP works!"}}]}',- '{"thinking": null, "evaluation_previous_goal": "Echoed", "memory": "Message echoed", "next_goal": "Complete", "action": [{"done": {"text": "Completed MCP test", "success": true}}]}',- ]- mock_llm = create_mock_llm(actions=actions)-- # Create agent- agent = Agent(- task=f"Go to {httpserver.url_for('/')} then use count_to_n to count to 3, and echo_message to say 'MCP works!'",- llm=mock_llm,- browser_session=browser_session,- tools=tools,- )-- # Run agent- history = await agent.run(max_steps=10)-- # Verify the agent used MCP tools- action_names = []- for step in history.history:- if step.model_output and step.model_output.action:- for action in step.model_output.action:- action_dict = action.model_dump(exclude_unset=True)- action_names.extend(action_dict.keys())-- assert 'count_to_n' in action_names- assert 'echo_message' in action_names-- # Check results- results = []- for step in history.history:- if step.result:- for r in step.result:- if r.extracted_content:- results.append(r.extracted_content)-- # Verify MCP tool outputs- assert any('Counted to 3: 1, 2, 3' in r for r in results)- assert any('Echo: MCP works!' in r for r in results)-- finally:- await mcp_client.disconnect()- await browser_session.kill()---async def test_mcp_tool_parameter_validation(test_mcp_server_script):- """Test that MCP tool parameters are properly validated."""- tools = Tools()-- mcp_client = MCPClient(server_name='test-server', command=sys.executable, args=[test_mcp_server_script])-- try:- await mcp_client.connect()- await mcp_client.register_to_tools(tools)-- # Get the count_to_n action- count_action = tools.registry.registry.actions['count_to_n']-- # Test with valid parameter- params = count_action.param_model(n=7)- result = await count_action.function(params=params)- assert 'Counted to 7' in result.extracted_content-- # Test parameter model validation- param_model = count_action.param_model- assert param_model is not None-- # Verify required fields- assert 'n' in param_model.model_fields- assert param_model.model_fields['n'].is_required()-- # Test echo_message with optional parameter- echo_action = tools.registry.registry.actions['echo_message']-- # With default prefix- params = echo_action.param_model(message='Test')- result = await echo_action.function(params=params)- assert result.extracted_content == 'Echo: Test'-- # With custom prefix- params = echo_action.param_model(message='Test', prefix='Custom:')- result = await echo_action.function(params=params)- assert result.extracted_content == 'Custom: Test'-- finally:- await mcp_client.disconnect()---async def test_mcp_client_prefix_and_filtering(test_mcp_server_script):- """Test tool filtering and prefixing."""- tools = Tools()-- mcp_client = MCPClient(server_name='test-server', command=sys.executable, args=[test_mcp_server_script])-- try:- await mcp_client.connect()-- # Register only specific tools with prefix- await mcp_client.register_to_tools(tools, tool_filter=['count_to_n', 'echo_message'], prefix='mcp_')-- # Verify registration- actions = tools.registry.registry.actions- assert 'mcp_count_to_n' in actions- assert 'mcp_echo_message' in actions- assert 'mcp_get_test_data' not in actions # Filtered out- assert 'get_test_data' not in actions # Not registered without prefix-- # Test prefixed action works- action = actions['mcp_count_to_n']- params = action.param_model(n=2)- result = await action.function(params=params)- assert 'Counted to 2: 1, 2' in result.extracted_content-- finally:- await mcp_client.disconnect()---async def test_mcp_tool_error_handling(test_mcp_server_script):- """Test error handling in MCP tools."""- tools = Tools()-- mcp_client = MCPClient(server_name='test-server', command=sys.executable, args=[test_mcp_server_script])-- try:- await mcp_client.connect()- await mcp_client.register_to_tools(tools)-- # Disconnect to simulate connection loss- await mcp_client.disconnect()-- # Try to use tool after disconnect- echo_action = tools.registry.registry.actions['echo_message']- params = echo_action.param_model(message='Test')- result: ActionResult = await echo_action.function(params=params)-- # Should handle error gracefully- assert result.error is not None-- finally:- # Already disconnected- pass---async def test_mcp_client_context_manager(test_mcp_server_script):- """Test using MCP client as context manager."""- tools = Tools()-- # Use as context manager- async with MCPClient(server_name='test-server', command=sys.executable, args=[test_mcp_server_script]) as mcp_client:- # Should auto-connect- assert mcp_client.session is not None-- await mcp_client.register_to_tools(tools)-- # Test tool works- action = tools.registry.registry.actions['get_test_data']- # get_test_data has no parameters- result = await action.function()- assert 'apple' in result.extracted_content- assert 'banana' in result.extracted_content-- # Should auto-disconnect- assert mcp_client.session is None---async def test_multiple_mcp_servers(test_mcp_server_script):- """Test connecting multiple MCP servers to the same tools."""- tools = Tools()-- # First MCP server with prefix- mcp1 = MCPClient(server_name='server1', command=sys.executable, args=[test_mcp_server_script])-- # Second MCP server with different prefix- mcp2 = MCPClient(server_name='server2', command=sys.executable, args=[test_mcp_server_script])-- try:- # Connect and register both- await mcp1.connect()- await mcp1.register_to_tools(tools, prefix='s1_')-- await mcp2.connect()- await mcp2.register_to_tools(tools, prefix='s2_')-- # Verify both sets of tools are available- actions = tools.registry.registry.actions- assert 's1_count_to_n' in actions- assert 's2_count_to_n' in actions-- # Test both work independently- action1 = actions['s1_echo_message']- params1 = action1.param_model(message='Server 1')- result1 = await action1.function(params=params1)-- action2 = actions['s2_echo_message']- params2 = action2.param_model(message='Server 2')- result2 = await action2.function(params=params2)-- assert result1.extracted_content == 'Echo: Server 1'- assert result2.extracted_content == 'Echo: Server 2'-- # Verify results show correct server- assert 'server1' in result1.long_term_memory- assert 'server2' in result2.long_term_memory-- finally:- await mcp1.disconnect()- await mcp2.disconnect()---async def test_mcp_result_formatting(test_mcp_server_script):- """Test that different MCP result formats are handled correctly."""- tools = Tools()-- mcp_client = MCPClient(server_name='test-server', command=sys.executable, args=[test_mcp_server_script])-- try:- await mcp_client.connect()- await mcp_client.register_to_tools(tools)-- # Test JSON result formatting- action = tools.registry.registry.actions['get_test_data']- result = await action.function()-- # Should contain formatted JSON- assert '"status": "success"' in result.extracted_content- assert '"items"' in result.extracted_content- assert '"count": 3' in result.extracted_content-- finally:- await mcp_client.disconnect()---async def test_agent_with_multiple_mcp_servers(test_mcp_server_script, httpserver: HTTPServer):- """Test agent using tools from multiple MCP servers in a single task."""- # Set up test webpage- httpserver.expect_request('/').respond_with_data(- """- <html>- <body>- <h1>Multi-MCP Test</h1>- <p>Use tools from both servers</p>- </body>- </html>- """,- content_type='text/html',- )-- browser_session = BrowserSession(browser_profile=BrowserProfile(headless=True, user_data_dir=None))- await browser_session.start()- tools = Tools()-- # Connect two MCP servers with different prefixes- mcp_server1 = MCPClient(server_name='math-server', command=sys.executable, args=[test_mcp_server_script])- mcp_server2 = MCPClient(server_name='data-server', command=sys.executable, args=[test_mcp_server_script])-- try:- # Connect and register both servers- await mcp_server1.connect()- await mcp_server1.register_to_tools(tools, prefix='math_', tool_filter=['count_to_n'])-- await mcp_server2.connect()- await mcp_server2.register_to_tools(tools, prefix='data_', tool_filter=['echo_message', 'get_test_data'])-- # Import create_mock_llm from conftest- from tests.ci.conftest import create_mock_llm-- # Create mock LLM with actions using tools from both servers- actions = [- f'{{"thinking": null, "evaluation_previous_goal": "Starting", "memory": "Starting multi-MCP task", "next_goal": "Navigate to test page", "action": [{{"go_to_url": {{"url": "{httpserver.url_for("/")}"}}}}]}}',- '{"thinking": null, "evaluation_previous_goal": "Navigated", "memory": "On test page", "next_goal": "Use math server to count", "action": [{"math_count_to_n": {"n": 5}}]}',- '{"thinking": null, "evaluation_previous_goal": "Counted with math server", "memory": "Used math_count_to_n", "next_goal": "Use data server to echo", "action": [{"data_echo_message": {"message": "Counted successfully", "prefix": "Result:"}}]}',- '{"thinking": null, "evaluation_previous_goal": "Echoed with data server", "memory": "Used data_echo_message", "next_goal": "Get test data from data server", "action": [{"data_get_test_data": {}}]}',- '{"thinking": null, "evaluation_previous_goal": "Got test data", "memory": "Retrieved JSON data", "next_goal": "Complete", "action": [{"done": {"text": "Used tools from both MCP servers successfully", "success": true}}]}',- ]- mock_llm = create_mock_llm(actions=actions)-- # Create agent with extended system message- agent = Agent(- task=f'Go to {httpserver.url_for("/")}, use math_count_to_n to count to 5, then use data_echo_message and data_get_test_data',- llm=mock_llm,- browser_session=browser_session,- tools=tools,- extend_system_message="""You have access to tools from two MCP servers:-- math server: Provides math_count_to_n for counting-- data server: Provides data_echo_message for echoing and data_get_test_data for JSON data--Use tools from both servers to complete the task.""",- )-- # Run agent- history = await agent.run(max_steps=10)-- # Verify the agent used tools from both servers- action_names = []- for step in history.history:- if step.model_output and step.model_output.action:- for action in step.model_output.action:- action_dict = action.model_dump(exclude_unset=True)- action_names.extend(action_dict.keys())-- # Should have used tools from both servers- assert 'math_count_to_n' in action_names- assert 'data_echo_message' in action_names- assert 'data_get_test_data' in action_names-- # Check results contain outputs from both servers- results = []- memory_entries = []- for step in history.history:- if step.result:- for r in step.result:- if r.extracted_content:- results.append(r.extracted_content)- if r.long_term_memory:- memory_entries.append(r.long_term_memory)-- # Verify outputs from both servers- assert any('Counted to 5: 1, 2, 3, 4, 5' in r for r in results)- assert any('Result: Counted successfully' in r for r in results)- assert any('"status": "success"' in r for r in results)-- # Verify both server names appear in memory- all_memory = ' '.join(memory_entries)- assert 'math-server' in all_memory- assert 'data-server' in all_memory-- finally:- await mcp_server1.disconnect()- await mcp_server2.disconnect()- await browser_session.kill()---async def test_mcp_nested_object_parameters(test_mcp_server_script):- """Test that MCP tools with nested object parameters are properly handled."""- tools = Tools()-- mcp_client = MCPClient(server_name='test-server', command=sys.executable, args=[test_mcp_server_script])-- try:- await mcp_client.connect()- await mcp_client.register_to_tools(tools)-- # Get the process_trace_update action- trace_action = tools.registry.registry.actions['process_trace_update']-- # Verify the parameter model has nested structure- param_model = trace_action.param_model- assert param_model is not None-- # Verify the main parameters exist- assert 'trace' in param_model.model_fields- assert 'window_size' in param_model.model_fields-- # Verify trace is required and window_size is optional- assert param_model.model_fields['trace'].is_required()- assert not param_model.model_fields['window_size'].is_required()-- # Verify window_size has a default value of 10- assert param_model.model_fields['window_size'].default == 10-- # Verify the trace parameter has nested structure- trace_field = param_model.model_fields['trace']- trace_annotation = trace_field.annotation-- # Should be a nested pydantic model, not just dict- assert trace_annotation is not dict- assert trace_annotation is not None-- # Verify nested model has the expected fields- nested_model = trace_annotation- assert hasattr(nested_model, 'model_fields')- nested_fields = nested_model.model_fields- assert 'recent_actions' in nested_fields- assert 'current_context' in nested_fields- assert 'goal' in nested_fields-- # Verify field requirements in nested model- assert nested_fields['recent_actions'].is_required()- assert nested_fields['goal'].is_required()- assert not nested_fields['current_context'].is_required()-- # Test creating an instance with nested parameters- assert callable(nested_model)- trace_data = nested_model(recent_actions=['click', 'type', 'navigate'], current_context='web page', goal='complete form')-- # Test the full parameter model- assert callable(param_model)- params = param_model(trace=trace_data, window_size=5)-- # Verify the parameter structure- assert hasattr(params, 'trace') and hasattr(params, 'window_size')- trace_obj = getattr(params, 'trace')- assert trace_obj.recent_actions == ['click', 'type', 'navigate']- assert trace_obj.current_context == 'web page'- assert trace_obj.goal == 'complete form'- assert getattr(params, 'window_size') == 5-- # Test calling the tool with nested parameters- result = await trace_action.function(params=params)-- # Verify the result contains the nested data- assert result.success is not False- assert 'Processed trace update' in result.extracted_content- assert '3 actions' in result.extracted_content- assert 'goal: complete form' in result.extracted_content- assert 'context: web page' in result.extracted_content- assert 'window: 5' in result.extracted_content-- # Test with default window_size- params_default = param_model(trace=trace_data)- result_default = await trace_action.function(params=params_default)- assert 'window: 10' in result_default.extracted_content-- # Test with minimal required parameters- minimal_trace = nested_model(recent_actions=['action1', 'action2'], goal='test goal')- params_minimal = param_model(trace=minimal_trace)- result_minimal = await trace_action.function(params=params_minimal)- assert 'goal: test goal' in result_minimal.extracted_content- assert 'context: unknown' in result_minimal.extracted_content # Default from handler-- finally:- await mcp_client.disconnect()---async def test_mcp_array_type_inference(test_mcp_server_script):- """Test that MCP tools with array parameters have proper type inference."""- tools = Tools()-- mcp_client = MCPClient(server_name='test-server', command=sys.executable, args=[test_mcp_server_script])-- try:- await mcp_client.connect()- await mcp_client.register_to_tools(tools)-- # Get the process_array_data action- array_action = tools.registry.registry.actions['process_array_data']-- # Verify the parameter model has array types- param_model = array_action.param_model- assert param_model is not None-- # Verify the main parameters exist- assert 'string_list' in param_model.model_fields- assert 'number_list' in param_model.model_fields- assert 'config_list' in param_model.model_fields- assert 'simple_array' in param_model.model_fields-- # Verify string_list is required and others are optional- assert param_model.model_fields['string_list'].is_required()- assert not param_model.model_fields['number_list'].is_required()- assert not param_model.model_fields['config_list'].is_required()- assert not param_model.model_fields['simple_array'].is_required()-- # Check array type annotations- string_list_field = param_model.model_fields['string_list']- number_list_field = param_model.model_fields['number_list']- config_list_field = param_model.model_fields['config_list']- simple_array_field = param_model.model_fields['simple_array']-- # Import typing_extensions for better type inspection- import typing- from typing import get_args, get_origin-- # Check string_list is list[str]- string_list_type = string_list_field.annotation- assert get_origin(string_list_type) is list- assert get_args(string_list_type) == (str,)-- # Check number_list is list[float] | None (since it's optional)- number_list_type = number_list_field.annotation- # For optional fields, the type is Union[list[float], None]- assert get_origin(number_list_type) in (typing.Union, type(list[float] | None))- union_args = get_args(number_list_type)- assert type(None) in union_args- # Find the list type in the union- list_type = next((arg for arg in union_args if get_origin(arg) is list), None)- assert list_type is not None- assert get_args(list_type) == (float,)-- # Check config_list is list[ConfigModel] | None- config_list_type = config_list_field.annotation- assert get_origin(config_list_type) in (typing.Union, type(list[object] | None))- union_args = get_args(config_list_type)- assert type(None) in union_args- # Find the list type in the union- list_type = next((arg for arg in union_args if get_origin(arg) is list), None)- assert list_type is not None- # The item type should be a Pydantic model with the expected fields- config_item_type = get_args(list_type)[0]- assert hasattr(config_item_type, 'model_fields')- config_fields = config_item_type.model_fields- assert 'name' in config_fields- assert 'value' in config_fields- assert 'enabled' in config_fields- assert config_fields['name'].is_required()- assert config_fields['value'].is_required()- assert not config_fields['enabled'].is_required()- assert config_fields['enabled'].default is True-- # Check simple_array is just list | None (no item type)- simple_array_type = simple_array_field.annotation- assert get_origin(simple_array_type) in (typing.Union, type(list | None))- union_args = get_args(simple_array_type)- assert type(None) in union_args- # Should have plain list without type args- assert list in union_args-- # Test creating instances with proper types- config_item_model = config_item_type- config1 = config_item_model(name='setting1', value=42)- config2 = config_item_model(name='setting2', value=100, enabled=False)-- # Create full parameter instance- params = param_model(- string_list=['hello', 'world'],- number_list=[1.5, 2.7, 3.14],- config_list=[config1, config2],- simple_array=['mixed', 123, True],- )-- # Verify the parameter structure- assert getattr(params, 'string_list') == ['hello', 'world']- assert getattr(params, 'number_list') == [1.5, 2.7, 3.14]- config_list = getattr(params, 'config_list')- assert len(config_list) == 2- assert getattr(config_list[0], 'name') == 'setting1'- assert getattr(config_list[0], 'value') == 42- assert getattr(config_list[0], 'enabled') is True # default- assert getattr(config_list[1], 'name') == 'setting2'- assert getattr(config_list[1], 'value') == 100- assert getattr(config_list[1], 'enabled') is False- assert getattr(params, 'simple_array') == ['mixed', 123, True]-- # Test calling the tool- result = await array_action.function(params=params)- assert result.success is not False- assert 'Processed arrays: strings=2, numbers=3, 2 configs, simple=3' in result.extracted_content-- # Test with minimal parameters (only required string_list)- minimal_params = param_model(string_list=['test'])- result_minimal = await array_action.function(params=minimal_params)- assert 'Processed arrays: strings=1, numbers=0, no configs, simple=0' in result_minimal.extracted_content-- finally:- await mcp_client.disconnect()diff --git a/tests/ci/test_mcp_server.py b/tests/ci/test_mcp_server.pydeleted file mode 100644index 82ad9bd5f..000000000--- a/tests/ci/test_mcp_server.py+++ /dev/null@@ -1,697 +0,0 @@-"""-Test the MCP server functionality with real browser sessions.--Tests cover:-- Navigation (current tab and new tab)-- Clicking elements (normal and new tab with modifiers)-- Typing text-- Getting browser state-- Content extraction-- Scrolling-- Tab management-- Browser session lifecycle-"""--import asyncio-import json--import pytest-from pytest_httpserver import HTTPServer--from browser_use.mcp.server import BrowserUseServer---class TestMCPServerNavigation:- """Test MCP server navigation functionality."""-- @pytest.fixture- async def mcp_server_with_session(self):- """Create an MCP server with a real browser session."""- server = BrowserUseServer()-- # Initialize browser session with the server's method- await server._init_browser_session(headless=True, user_data_dir=None, keep_alive=False)-- yield server-- # Cleanup- if server.browser_session:- await server.browser_session.kill()-- async def test_navigate_current_tab(self, mcp_server_with_session, httpserver: HTTPServer):- """Test navigation in current tab."""- # Set up test page- httpserver.expect_request('/test').respond_with_data('<h1>Test Page</h1>', content_type='text/html')- test_url = httpserver.url_for('/test')-- # Navigate- result = await mcp_server_with_session._navigate(test_url, new_tab=False)-- assert result == f'Navigated to: {test_url}'-- # Verify we're on the right page- page = await mcp_server_with_session.browser_session.get_current_page()- assert page.url == test_url-- async def test_navigate_new_tab(self, mcp_server_with_session, httpserver: HTTPServer):- """Test navigation in new tab."""- # Set up test pages- httpserver.expect_request('/page1').respond_with_data('<h1>Page 1</h1>', content_type='text/html')- httpserver.expect_request('/page2').respond_with_data('<h1>Page 2</h1>', content_type='text/html')-- # Navigate to first page- page1_url = httpserver.url_for('/page1')- await mcp_server_with_session._navigate(page1_url, new_tab=False)-- # Navigate to second page in new tab- page2_url = httpserver.url_for('/page2')- result = await mcp_server_with_session._navigate(page2_url, new_tab=True)-- assert 'Opened new tab #1' in result- assert page2_url in result-- # Verify we have 2 tabs- assert len(mcp_server_with_session.browser_session.tabs) == 2-- # Verify current page is the new tab- page = await mcp_server_with_session.browser_session.get_current_page()- assert page.url == page2_url-- async def test_navigate_no_session(self):- """Test navigation without browser session."""- server = BrowserUseServer()- # Don't initialize session-- result = await server._navigate('https://example.com')- assert result == 'Error: No browser session active'---class TestMCPServerClick:- """Test MCP server click functionality."""-- @pytest.fixture- async def mcp_server_with_page(self, httpserver: HTTPServer):- """Create an MCP server with a test page loaded."""- server = BrowserUseServer()- await server._init_browser_session(headless=True, user_data_dir=None, keep_alive=False)-- # Set up test page with clickable elements- test_html = """- <html>- <body>- <button id="btn1">Click Me</button>- <a href="/linked-page" id="link1">Link to Page</a>- <div id="div1" onclick="console.log('clicked')">Clickable Div</div>- <input type="text" id="input1" placeholder="Type here">- </body>- </html>- """- httpserver.expect_request('/test').respond_with_data(test_html, content_type='text/html')- httpserver.expect_request('/linked-page').respond_with_data('<h1>Linked Page</h1>', content_type='text/html')-- # Navigate to test page- assert server.browser_session is not None- await server.browser_session.navigate_to(httpserver.url_for('/test'))-- # Wait for page to load- await asyncio.sleep(0.5)-- yield server-- # Cleanup- assert server.browser_session is not None- await server.browser_session.kill()-- async def test_click_normal(self, mcp_server_with_page):- """Test normal click on button."""- # Get browser state to find button index- assert mcp_server_with_page.browser_session is not None- state = await mcp_server_with_page.browser_session.get_state_summary(cache_clickable_elements_hashes=False)-- # Find button index- button_index = None- for idx, elem in state.selector_map.items():- if elem.tag_name == 'button':- button_index = idx- break-- assert button_index is not None, 'Button not found in selector map'-- # Click button- result = await mcp_server_with_page._click(button_index, new_tab=False)- assert result == f'Clicked element {button_index}'-- async def test_click_link_new_tab(self, mcp_server_with_page):- """Test click on link with new_tab=True."""- # Get browser state to find link index- state = await mcp_server_with_page.browser_session.get_state_summary(cache_clickable_elements_hashes=False)-- # Find link index- link_index = None- for idx, elem in state.selector_map.items():- if elem.tag_name == 'a' and elem.attributes.get('href'):- link_index = idx- break-- assert link_index is not None, 'Link not found in selector map'-- # Click link in new tab- result = await mcp_server_with_page._click(link_index, new_tab=True)-- assert 'opened in new tab' in result- assert len(mcp_server_with_page.browser_session.tabs) == 2-- async def test_click_non_link_new_tab(self, mcp_server_with_page):- """Test click on non-link element with new_tab=True."""- # Get browser state to find div index- state = await mcp_server_with_page.browser_session.get_state_summary(cache_clickable_elements_hashes=False)-- # Find div index- div_index = None- for idx, elem in state.selector_map.items():- if elem.tag_name == 'div' and elem.attributes.get('id') == 'div1':- div_index = idx- break-- assert div_index is not None, 'Div not found in selector map'-- # Click div with new tab modifier- result = await mcp_server_with_page._click(div_index, new_tab=True)-- # Should use modifier key- assert 'key (new tab if supported)' in result-- async def test_click_element_not_found(self, mcp_server_with_page):- """Test click when element is not found."""- result = await mcp_server_with_page._click(9999) # Non-existent index- assert result == 'Element with index 9999 not found'-- async def test_click_no_session(self):- """Test click without browser session."""- server = BrowserUseServer()- result = await server._click(0)- assert result == 'Error: No browser session active'---class TestMCPServerType:- """Test MCP server typing functionality."""-- @pytest.fixture- async def mcp_server_with_input(self, httpserver: HTTPServer):- """Create an MCP server with a test page containing input field."""- server = BrowserUseServer()- await server._init_browser_session(headless=True, user_data_dir=None, keep_alive=False)-- # Set up test page- test_html = """- <html>- <body>- <input type="text" id="input1" placeholder="Type here">- <textarea id="textarea1" placeholder="Enter text"></textarea>- </body>- </html>- """- httpserver.expect_request('/test').respond_with_data(test_html, content_type='text/html')-- # Navigate to test page- assert server.browser_session is not None- await server.browser_session.navigate_to(httpserver.url_for('/test'))- await asyncio.sleep(0.5)-- yield server-- # Cleanup- assert server.browser_session is not None- await server.browser_session.kill()-- async def test_type_text_in_input(self, mcp_server_with_input):- """Test typing text into input field."""- # Get browser state to find input index- state = await mcp_server_with_input.browser_session.get_state_summary(cache_clickable_elements_hashes=False)-- # Find input index- input_index = None- for idx, elem in state.selector_map.items():- if elem.tag_name == 'input':- input_index = idx- break-- assert input_index is not None, 'Input not found in selector map'-- # Type text- test_text = 'Hello MCP Server!'- result = await mcp_server_with_input._type_text(input_index, test_text)-- assert result == f"Typed '{test_text}' into element {input_index}"-- # Verify text was typed- page = await mcp_server_with_input.browser_session.get_current_page()- value = await page.locator('#input1').input_value()- assert value == test_text-- async def test_type_element_not_found(self, mcp_server_with_input):- """Test typing when element is not found."""- result = await mcp_server_with_input._type_text(9999, 'test')- assert result == 'Element with index 9999 not found'-- async def test_type_no_session(self):- """Test typing without browser session."""- server = BrowserUseServer()- result = await server._type_text(0, 'test')- assert result == 'Error: No browser session active'---class TestMCPServerState:- """Test MCP server browser state functionality."""-- @pytest.fixture- async def mcp_server_with_complex_page(self, httpserver: HTTPServer):- """Create an MCP server with a complex test page."""- server = BrowserUseServer()- await server._init_browser_session(headless=True, user_data_dir=None, keep_alive=False)-- # Set up test page- test_html = """- <html>- <head><title>Test Page Title</title></head>- <body>- <h1>Test Page</h1>- <button>Button 1</button>- <a href="/page2">Link 1</a>- <input type="text" placeholder="Input field">- <select>- <option>Option 1</option>- <option>Option 2</option>- </select>- </body>- </html>- """- httpserver.expect_request('/test').respond_with_data(test_html, content_type='text/html')-- # Navigate to test page- assert server.browser_session is not None- await server.browser_session.navigate_to(httpserver.url_for('/test'))- await asyncio.sleep(0.5)-- yield server-- # Cleanup- assert server.browser_session is not None- await server.browser_session.kill()-- async def test_get_browser_state(self, mcp_server_with_complex_page):- """Test getting browser state."""- result = await mcp_server_with_complex_page._get_browser_state(include_screenshot=False)-- # Parse result- state_data = json.loads(result)-- # Verify state structure- assert 'url' in state_data- assert 'title' in state_data- assert state_data['title'] == 'Test Page Title'- assert 'tabs' in state_data- assert 'interactive_elements' in state_data-- # Verify we have interactive elements- assert len(state_data['interactive_elements']) > 0-- # Check element structure- first_elem = state_data['interactive_elements'][0]- assert 'index' in first_elem- assert 'tag' in first_elem- assert 'text' in first_elem-- async def test_get_browser_state_no_session(self):- """Test getting state without browser session."""- server = BrowserUseServer()- result = await server._get_browser_state()- assert result == 'Error: No browser session active'---class TestMCPServerScroll:- """Test MCP server scrolling functionality."""-- @pytest.fixture- async def mcp_server_with_scrollable_page(self, httpserver: HTTPServer):- """Create an MCP server with a scrollable test page."""- server = BrowserUseServer()- await server._init_browser_session(headless=True, user_data_dir=None, keep_alive=False)-- # Set up scrollable test page- test_html = """- <html>- <body style="height: 3000px;">- <h1>Top of Page</h1>- <div style="margin-top: 1500px;">- <h2>Middle of Page</h2>- </div>- <div style="margin-top: 1000px;">- <h2>Bottom of Page</h2>- </div>- </body>- </html>- """- httpserver.expect_request('/test').respond_with_data(test_html, content_type='text/html')-- # Navigate to test page- assert server.browser_session is not None- await server.browser_session.navigate_to(httpserver.url_for('/test'))- await asyncio.sleep(0.5)-- yield server-- # Cleanup- assert server.browser_session is not None- await server.browser_session.kill()-- async def test_scroll_down(self, mcp_server_with_scrollable_page):- """Test scrolling down."""- # Get initial scroll position- page = await mcp_server_with_scrollable_page.browser_session.get_current_page()- initial_scroll = await page.evaluate('() => window.pageYOffset')-- # Scroll down- result = await mcp_server_with_scrollable_page._scroll(direction='down')- assert result == 'Scrolled down'-- # Verify scroll position changed- new_scroll = await page.evaluate('() => window.pageYOffset')- assert new_scroll > initial_scroll-- async def test_scroll_up(self, mcp_server_with_scrollable_page):- """Test scrolling up."""- page = await mcp_server_with_scrollable_page.browser_session.get_current_page()-- # First scroll down- await mcp_server_with_scrollable_page._scroll(direction='down')- down_scroll = await page.evaluate('() => window.pageYOffset')-- # Then scroll up- result = await mcp_server_with_scrollable_page._scroll(direction='up')- assert result == 'Scrolled up'-- # Verify scroll position changed- up_scroll = await page.evaluate('() => window.pageYOffset')- assert up_scroll < down_scroll-- async def test_scroll_no_session(self):- """Test scrolling without browser session."""- server = BrowserUseServer()- result = await server._scroll()- assert result == 'Error: No browser session active'---class TestMCPServerTabManagement:- """Test MCP server tab management functionality."""-- @pytest.fixture- async def mcp_server_with_tabs(self, httpserver: HTTPServer):- """Create an MCP server with multiple tabs."""- server = BrowserUseServer()- await server._init_browser_session(headless=True, user_data_dir=None, keep_alive=False)-- # Set up test pages- httpserver.expect_request('/tab1').respond_with_data('<h1>Tab 1</h1>', content_type='text/html')- httpserver.expect_request('/tab2').respond_with_data('<h1>Tab 2</h1>', content_type='text/html')- httpserver.expect_request('/tab3').respond_with_data('<h1>Tab 3</h1>', content_type='text/html')-- # Open multiple tabs- assert server.browser_session is not None- await server.browser_session.navigate_to(httpserver.url_for('/tab1'))- await server.browser_session.create_new_tab(httpserver.url_for('/tab2'))- await server.browser_session.create_new_tab(httpserver.url_for('/tab3'))-- yield server-- # Cleanup- assert server.browser_session is not None- await server.browser_session.kill()-- async def test_list_tabs(self, mcp_server_with_tabs):- """Test listing tabs."""- result = await mcp_server_with_tabs._list_tabs()-- # Parse result- tabs = json.loads(result)-- assert len(tabs) == 3- for i, tab in enumerate(tabs):- assert tab['index'] == i- assert 'url' in tab- assert 'title' in tab- assert f'/tab{i + 1}' in tab['url']-- async def test_switch_tab(self, mcp_server_with_tabs):- """Test switching tabs."""- # Switch to first tab- result = await mcp_server_with_tabs._switch_tab(0)-- assert 'Switched to tab 0' in result- assert '/tab1' in result-- # Verify current tab- page = await mcp_server_with_tabs.browser_session.get_current_page()- assert '/tab1' in page.url-- async def test_close_tab(self, mcp_server_with_tabs):- """Test closing a tab."""- # Close middle tab- result = await mcp_server_with_tabs._close_tab(1)-- assert 'Closed tab 1' in result- assert '/tab2' in result-- # Verify tab count- assert len(mcp_server_with_tabs.browser_session.tabs) == 2-- async def test_close_tab_invalid_index(self, mcp_server_with_tabs):- """Test closing tab with invalid index."""- result = await mcp_server_with_tabs._close_tab(10)- assert result == 'Invalid tab index: 10'-- async def test_tab_operations_no_session(self):- """Test tab operations without browser session."""- server = BrowserUseServer()-- assert await server._list_tabs() == 'Error: No browser session active'- assert await server._switch_tab(0) == 'Error: No browser session active'- assert await server._close_tab(0) == 'Error: No browser session active'---class TestMCPServerHistory:- """Test MCP server browser history navigation."""-- @pytest.fixture- async def mcp_server_with_history(self, httpserver: HTTPServer):- """Create an MCP server with navigation history."""- server = BrowserUseServer()- await server._init_browser_session(headless=True, user_data_dir=None, keep_alive=False)-- # Set up test pages- httpserver.expect_request('/page1').respond_with_data('<h1>Page 1</h1>', content_type='text/html')- httpserver.expect_request('/page2').respond_with_data('<h1>Page 2</h1>', content_type='text/html')-- # Navigate to create history- assert server.browser_session is not None- await server.browser_session.navigate_to(httpserver.url_for('/page1'))- await server.browser_session.navigate_to(httpserver.url_for('/page2'))-- yield server-- # Cleanup- assert server.browser_session is not None- await server.browser_session.kill()-- async def test_go_back(self, mcp_server_with_history):- """Test going back in history."""- # Verify we're on page2- page = await mcp_server_with_history.browser_session.get_current_page()- assert '/page2' in page.url-- # Go back- result = await mcp_server_with_history._go_back()- assert result == 'Navigated back'-- # Verify we're on page1- page = await mcp_server_with_history.browser_session.get_current_page()- assert '/page1' in page.url-- async def test_go_back_no_session(self):- """Test going back without browser session."""- server = BrowserUseServer()- result = await server._go_back()- assert result == 'Error: No browser session active'---class TestMCPServerExtraction:- """Test MCP server content extraction functionality."""-- @pytest.fixture- async def mcp_server_with_content(self, httpserver: HTTPServer, mock_llm):- """Create an MCP server with content to extract."""- server = BrowserUseServer()- await server._init_browser_session(headless=True, user_data_dir=None, keep_alive=False)-- # Set up the mock LLM for extraction- server.llm = mock_llm-- # Set up test page with content- test_html = """- <html>- <body>- <h1>Product Page</h1>- <div class="product">- <h2>Amazing Widget</h2>- <p class="price">$99.99</p>- <p class="description">This is an amazing widget that does amazing things.</p>- <a href="/buy">Buy Now</a>- </div>- </body>- </html>- """- httpserver.expect_request('/test').respond_with_data(test_html, content_type='text/html')-- # Navigate to test page- assert server.browser_session is not None- await server.browser_session.navigate_to(httpserver.url_for('/test'))- await asyncio.sleep(0.5)-- yield server-- # Cleanup- assert server.browser_session is not None- await server.browser_session.kill()-- async def test_extract_content_no_llm(self):- """Test extraction without LLM."""- server = BrowserUseServer()- await server._init_browser_session(headless=True, user_data_dir=None, keep_alive=False)- server.llm = None-- result = await server._extract_content('query')- assert result == 'Error: LLM not initialized (set OPENAI_API_KEY)'-- async def test_extract_content_no_filesystem(self, mock_llm):- """Test extraction without filesystem."""- server = BrowserUseServer()- await server._init_browser_session(headless=True, user_data_dir=None, keep_alive=False)- server.llm = mock_llm # Use the mock from conftest- server.file_system = None-- result = await server._extract_content('query')- assert result == 'Error: FileSystem not initialized'-- async def test_extract_content_no_tools(self, mock_llm):- """Test extraction without tools."""- server = BrowserUseServer()- await server._init_browser_session(headless=True, user_data_dir=None, keep_alive=False)- server.llm = mock_llm # Use mock LLM so we get past the LLM check- server.tools = None-- result = await server._extract_content('query')- assert result == 'Error: Tools not initialized'-- async def test_extract_content_no_session(self):- """Test extraction without browser session."""- server = BrowserUseServer()- # Don't set LLM since that check comes first- # server.llm = None- # server.file_system = None-- result = await server._extract_content('query')- assert result == 'Error: LLM not initialized (set OPENAI_API_KEY)'---class TestMCPServerLifecycle:- """Test MCP server lifecycle operations."""-- async def test_init_browser_session(self):- """Test browser session initialization."""- server = BrowserUseServer()-- # Verify no session initially- assert server.browser_session is None- assert server.tools is None- assert server.file_system is None-- # Initialize session- await server._init_browser_session(allowed_domains=['example.com'], headless=True, user_data_dir=None, keep_alive=False)-- # Verify all components initialized- assert server.browser_session is not None- assert server.tools is not None- assert server.file_system is not None-- # Verify browser profile settings- assert server.browser_session is not None- assert server.browser_session.browser_profile.allowed_domains == ['example.com']-- # Cleanup- assert server.browser_session is not None- await server.browser_session.kill() # type: ignore[reportGeneralTypeIssues]-- async def test_init_browser_session_already_exists(self):- """Test that browser session is not recreated if it already exists."""- server = BrowserUseServer()-- # Initialize first session- await server._init_browser_session(headless=True, user_data_dir=None, keep_alive=False)- first_session = server.browser_session-- # Try to initialize again- await server._init_browser_session(headless=True, user_data_dir=None, keep_alive=False)-- # Should be the same session- assert server.browser_session is first_session-- # Cleanup- assert server.browser_session is not None- await server.browser_session.kill()-- async def test_close_browser(self):- """Test closing browser session."""- server = BrowserUseServer()-- # Initialize session- await server._init_browser_session(headless=True, user_data_dir=None, keep_alive=False)-- # Close browser- result = await server._close_browser()- assert result == 'Browser closed'-- # Verify everything cleaned up- assert server.browser_session is None- assert server.tools is None-- async def test_close_browser_no_session(self):- """Test closing when no browser session."""- server = BrowserUseServer()-- result = await server._close_browser()- assert result == 'No browser session to close'---class TestMCPServerWithLLM:- """Test MCP server with LLM functionality."""-- async def test_init_with_mock_llm(self, mock_llm):- """Test initialization with mock LLM."""- server = BrowserUseServer()-- # Initialize browser session- await server._init_browser_session(headless=True, user_data_dir=None, keep_alive=False)-- # Manually set the mock LLM since we're not using real API key- server.llm = mock_llm-- # Verify LLM is set- assert server.llm is not None-- # Cleanup- assert server.browser_session is not None- await server.browser_session.kill()---if __name__ == '__main__':- pytest.main([__file__, '-v', '-s'])