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
tweak test fixture scope
Nick Sweetingd958260c42b9
diff --git a/examples/custom-functions/hover_element.py b/examples/custom-functions/hover_element.pyindex d363ba058..588b3fefa 100644--- a/examples/custom-functions/hover_element.py+++ b/examples/custom-functions/hover_element.py@@ -85,7 +85,7 @@ async def main():)await agent.run()- await browser_session.stop()+ await browser_session.kill()input('Press Enter to close...')diff --git a/tests/ci/conftest.py b/tests/ci/conftest.pyindex 426143ba5..f0ab359f1 100644--- a/tests/ci/conftest.py+++ b/tests/ci/conftest.py@@ -165,7 +165,7 @@ async def browser_session():)await session.start()yield session- await session.stop()+ await session.kill()@pytest.fixture(scope='function')diff --git a/tests/ci/evaluate_tasks.py b/tests/ci/evaluate_tasks.pyindex c17f7ff2f..29d1d068a 100644--- a/tests/ci/evaluate_tasks.py+++ b/tests/ci/evaluate_tasks.py@@ -163,14 +163,14 @@ If the agent provided no output, explain what might have gone wrong.}# Clean up session before returning- await session.stop()+ await session.kill()return resultexcept Exception as e:# Ensure session cleanup even on errortry:- await session.stop()+ await session.kill()except Exception:passdiff --git a/tests/ci/test_action_parameter_injection.py b/tests/ci/test_action_parameter_injection.pyindex b67a57a6c..066f01aa5 100644--- a/tests/ci/test_action_parameter_injection.py+++ b/tests/ci/test_action_parameter_injection.py@@ -61,11 +61,12 @@ class TestBrowserContext:browser_profile=BrowserProfile(headless=True,user_data_dir=None,+ keep_alive=True,))await browser_session.start()yield browser_session- await browser_session.stop()+ await browser_session.kill()def test_is_url_allowed(self):"""diff --git a/tests/ci/test_browser_session_cookies.py b/tests/ci/test_browser_session_cookies.pyindex a93f4dba6..dd79dc3d5 100644--- a/tests/ci/test_browser_session_cookies.py+++ b/tests/ci/test_browser_session_cookies.py@@ -72,7 +72,7 @@ class TestBrowserSessionCookies:yield session# Cleanuptry:- await session.stop()+ await session.kill()except Exception:pass@@ -153,7 +153,7 @@ class TestBrowserSessionCookies:# Cleanupsave_path.unlink(missing_ok=True)- await session.stop()+ await session.kill()async def test_nonexistent_cookies_file(self):"""Test that browser starts normally when cookies_file doesn't exist."""@@ -170,7 +170,7 @@ class TestBrowserSessionCookies:localhost_cookies = [c for c in cookies if c['domain'] in ['localhost', '.localhost']]assert len(localhost_cookies) == 0, f'Expected no localhost cookies, but found: {localhost_cookies}'- await session.stop()+ await session.kill()async def test_invalid_cookies_file(self, tmp_path):"""Test that browser handles invalid cookie file gracefully."""@@ -182,7 +182,7 @@ class TestBrowserSessionCookies:session = BrowserSession(browser_profile=profile)# Should start without errors (warning logged)- await session.start()+ await session.kill()# Should have no cookies from localhost (our test domain)# Note: Browser may have cookies from default pages like Google's new tab page@@ -190,7 +190,7 @@ class TestBrowserSessionCookies:localhost_cookies = [c for c in cookies if c['domain'] in ['localhost', '.localhost']]assert len(localhost_cookies) == 0, f'Expected no localhost cookies, but found: {localhost_cookies}'- await session.stop()+ await session.kill()async def test_relative_cookies_file_path(self, browser_profile_with_cookies):"""Test that relative cookies_file paths work correctly."""@@ -218,4 +218,4 @@ class TestBrowserSessionCookies:# Cleanupexpected_path.unlink(missing_ok=True)- await session.stop()+ await session.kill()diff --git a/tests/ci/test_browser_session_crashed_page_recovery.py b/tests/ci/test_browser_session_crashed_page_recovery.pyindex 72fdcf24d..bbc15acd6 100644--- a/tests/ci/test_browser_session_crashed_page_recovery.py+++ b/tests/ci/test_browser_session_crashed_page_recovery.py@@ -25,7 +25,7 @@ from browser_use.browser.session import BrowserSessionwarnings.filterwarnings('ignore', category=RuntimeWarning, message='.*TargetClosedError.*')[email protected](scope='module')[email protected](scope='session')async def playwright():async with async_playwright() as p:yield pdiff --git a/tests/ci/test_browser_session_output_paths.py b/tests/ci/test_browser_session_output_paths.pyindex ce616b500..c29ba18fa 100644--- a/tests/ci/test_browser_session_output_paths.py+++ b/tests/ci/test_browser_session_output_paths.py@@ -154,7 +154,7 @@ class TestAgentRecordings:conversation_files = list(conversation_path.glob('conversation_*.txt'))assert len(conversation_files) > 0, f'{path_type}: conversation file was not created in {conversation_path}'finally:- await browser_session.stop()+ await browser_session.kill()@pytest.mark.parametrize('generate_gif', [False, True, 'custom_path'])async def test_generate_gif(self, test_dir, httpserver_url, llm, generate_gif):@@ -198,7 +198,7 @@ class TestAgentRecordings:assert expected_gif_path is not None, 'expected_gif_path should be set for custom_path'assert expected_gif_path.exists(), f'GIF was not created at {expected_gif_path}'finally:- await browser_session.stop()+ await browser_session.kill()class TestBrowserProfileRecordings:@@ -228,7 +228,7 @@ class TestBrowserProfileRecordings:await browser_session.navigate(httpserver_url)await asyncio.sleep(0.5)finally:- await browser_session.stop()+ await browser_session.kill()# Add delay for video processingawait asyncio.sleep(1)@@ -271,7 +271,7 @@ class TestBrowserProfileRecordings:await browser_session.navigate(httpserver_url)await asyncio.sleep(0.5)finally:- await browser_session.stop()+ await browser_session.kill()# HAR files should be createdassert har_path.exists(), f'HAR file was not created at {har_path}'@@ -324,7 +324,7 @@ class TestBrowserProfileRecordings:)await agent.run(max_steps=5)finally:- await browser_session.stop()+ await browser_session.kill()# Check trace file - should be created automatically in the directoryassert trace_dir.exists(), f'Trace directory was not created at {trace_dir}'@@ -387,7 +387,7 @@ class TestCombinedRecordings:# Check video directoryassert video_dir.exists(), 'Video directory was not created'finally:- await browser_session.stop()+ await browser_session.kill()# Check files created after browser closevideo_files = list(video_dir.glob('*.webm'))diff --git a/tests/ci/test_browser_session_screenshots.py b/tests/ci/test_browser_session_screenshots.pyindex 2b59836f5..0dcecdab3 100644--- a/tests/ci/test_browser_session_screenshots.py+++ b/tests/ci/test_browser_session_screenshots.py@@ -68,7 +68,7 @@ class TestHeadlessScreenshots:assert len(full_page_bytes) > 5000finally:- await browser_session.stop()+ await browser_session.kill()async def test_screenshot_with_state_summary_in_headless(self, httpserver):"""Test that get_state_summary includes screenshots in headless mode"""@@ -104,7 +104,7 @@ class TestHeadlessScreenshots:assert len(screenshot_bytes) > 1000finally:- await browser_session.stop()+ await browser_session.kill()async def test_screenshot_graceful_handling_in_headless(self):"""Test that screenshot handling works correctly in headless mode even with closed pages"""@@ -139,7 +139,7 @@ class TestHeadlessScreenshots:assert isinstance(state.screenshot, str)finally:- await browser_session.stop()+ await browser_session.kill()async def test_parallel_screenshots_long_page(self, httpserver):"""Test screenshots in a highly parallel environment with a very long page"""@@ -372,4 +372,4 @@ class TestHeadlessScreenshots:print('✅ All screenshot positions tested successfully!')finally:- await browser_session.stop()+ await browser_session.kill()diff --git a/tests/ci/test_controller.py b/tests/ci/test_controller.pyindex 7be12dacc..9f20dc7a1 100644--- a/tests/ci/test_controller.py+++ b/tests/ci/test_controller.py@@ -81,11 +81,12 @@ async def browser_session():browser_profile=BrowserProfile(headless=True,user_data_dir=None,+ keep_alive=True,))await browser_session.start()yield browser_session- await browser_session.stop()+ await browser_session.kill()@pytest.fixture(scope='function')diff --git a/tests/ci/test_mcp_client.py b/tests/ci/test_mcp_client.pyindex 7631cc548..27378aaf4 100644--- a/tests/ci/test_mcp_client.py+++ b/tests/ci/test_mcp_client.py@@ -188,7 +188,7 @@ async def test_mcp_tools_with_agent(test_mcp_server_script, httpserver: HTTPServcontent_type='text/html',)- browser_session = BrowserSession(browser_profile=BrowserProfile(headless=True, user_data_dir=None))+ browser_session = BrowserSession(browser_profile=BrowserProfile(headless=True, user_data_dir=None, keep_alive=True))await browser_session.start()controller = Controller()@@ -247,7 +247,7 @@ async def test_mcp_tools_with_agent(test_mcp_server_script, httpserver: HTTPServfinally:await mcp_client.disconnect()- await browser_session.stop()+ await browser_session.kill()async def test_mcp_tool_parameter_validation(test_mcp_server_script):@@ -535,4 +535,4 @@ Use tools from both servers to complete the task.""",finally:await mcp_server1.disconnect()await mcp_server2.disconnect()- await browser_session.stop()+ await browser_session.kill()diff --git a/tests/ci/test_registry.py b/tests/ci/test_registry.pyindex aca228ea0..1f8c148cf 100644--- a/tests/ci/test_registry.py+++ b/tests/ci/test_registry.py@@ -73,6 +73,7 @@ def http_server():)yield server+server.stop()@@ -94,17 +95,18 @@ def registry():return Registry[TestContext]()[email protected](scope='session')[email protected](scope='function')async def browser_session(base_url):"""Create a real BrowserSession for testing"""browser_session = BrowserSession(browser_profile=BrowserProfile(headless=True,user_data_dir=None,+ keep_alive=True,))await browser_session.start()- await browser_session.create_new_tab(f'{base_url}/test')+ await browser_session.navigate(f'{base_url}/test', new_tab=True)yield browser_sessionawait browser_session.kill()diff --git a/tests/old/test_agent_actions.py b/tests/old/test_agent_actions.pyindex 9825133c3..0e6b0b513 100644--- a/tests/old/test_agent_actions.py+++ b/tests/old/test_agent_actions.py@@ -12,13 +12,11 @@ def llm():"""Initialize language model for testing"""# return ChatAnthropic(model_name='claude-3-5-sonnet-20240620', timeout=25, stop=None)- return ChatAzureOpenAI(- model='gpt-4.1',- )+ return ChatAzureOpenAI(model='gpt-4.1')# return ChatOpenAI(model='gpt-4.1-mini')[email protected](scope='module')async def browser_session():from browser_use.browser.profile import BrowserProfile@@ -26,7 +24,7 @@ async def browser_session():browser_session = BrowserSession(browser_profile=profile)await browser_session.start()yield browser_session- await browser_session.stop()+ await browser_session.kill()# pytest tests/test_agent_actions.py -v -k "test_ecommerce_interaction" --capture=no