mirror of
https://github.com/browser-use/browser-use.git
synced 2026-09-14 19:59:47 +08:00
tweak test fixture scope
This commit is contained in:
@@ -85,7 +85,7 @@ async def main():
|
||||
)
|
||||
|
||||
await agent.run()
|
||||
await browser_session.stop()
|
||||
await browser_session.kill()
|
||||
|
||||
input('Press Enter to close...')
|
||||
|
||||
|
||||
@@ -165,7 +165,7 @@ async def browser_session():
|
||||
)
|
||||
await session.start()
|
||||
yield session
|
||||
await session.stop()
|
||||
await session.kill()
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
|
||||
@@ -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 result
|
||||
|
||||
except Exception as e:
|
||||
# Ensure session cleanup even on error
|
||||
try:
|
||||
await session.stop()
|
||||
await session.kill()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
@@ -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):
|
||||
"""
|
||||
|
||||
@@ -72,7 +72,7 @@ class TestBrowserSessionCookies:
|
||||
yield session
|
||||
# Cleanup
|
||||
try:
|
||||
await session.stop()
|
||||
await session.kill()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
@@ -153,7 +153,7 @@ class TestBrowserSessionCookies:
|
||||
|
||||
# Cleanup
|
||||
save_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:
|
||||
|
||||
# Cleanup
|
||||
expected_path.unlink(missing_ok=True)
|
||||
await session.stop()
|
||||
await session.kill()
|
||||
|
||||
@@ -25,7 +25,7 @@ from browser_use.browser.session import BrowserSession
|
||||
warnings.filterwarnings('ignore', category=RuntimeWarning, message='.*TargetClosedError.*')
|
||||
|
||||
|
||||
@pytest.fixture(scope='module')
|
||||
@pytest.fixture(scope='session')
|
||||
async def playwright():
|
||||
async with async_playwright() as p:
|
||||
yield p
|
||||
|
||||
@@ -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 processing
|
||||
await 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 created
|
||||
assert 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 directory
|
||||
assert trace_dir.exists(), f'Trace directory was not created at {trace_dir}'
|
||||
@@ -387,7 +387,7 @@ class TestCombinedRecordings:
|
||||
# Check video directory
|
||||
assert video_dir.exists(), 'Video directory was not created'
|
||||
finally:
|
||||
await browser_session.stop()
|
||||
await browser_session.kill()
|
||||
|
||||
# Check files created after browser close
|
||||
video_files = list(video_dir.glob('*.webm'))
|
||||
|
||||
@@ -68,7 +68,7 @@ class TestHeadlessScreenshots:
|
||||
assert len(full_page_bytes) > 5000
|
||||
|
||||
finally:
|
||||
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) > 1000
|
||||
|
||||
finally:
|
||||
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()
|
||||
|
||||
@@ -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')
|
||||
|
||||
@@ -188,7 +188,7 @@ async def test_mcp_tools_with_agent(test_mcp_server_script, httpserver: HTTPServ
|
||||
content_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: HTTPServ
|
||||
|
||||
finally:
|
||||
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()
|
||||
|
||||
@@ -73,6 +73,7 @@ def http_server():
|
||||
)
|
||||
|
||||
yield server
|
||||
|
||||
server.stop()
|
||||
|
||||
|
||||
@@ -94,17 +95,18 @@ def registry():
|
||||
return Registry[TestContext]()
|
||||
|
||||
|
||||
@pytest.fixture(scope='session')
|
||||
@pytest.fixture(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_session
|
||||
await browser_session.kill()
|
||||
|
||||
|
||||
@@ -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')
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@pytest.fixture(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
|
||||
|
||||
Reference in New Issue
Block a user