Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

try and tweak test_registry to fix deadlock tests

Nick Sweeting683a97e475ef
diff --git a/tests/ci/test_registry.py b/tests/ci/test_registry.py
index 8474303e5..31bccffab 100644
--- a/tests/ci/test_registry.py
+++ b/tests/ci/test_registry.py
@@ -89,18 +89,6 @@ def base_url(http_server):
return f'http://{http_server.host}:{http_server.port}'
[email protected](scope='module')
-async def browser_session():
- """Create and provide a real BrowserSession instance."""
- browser_session = BrowserSession(
- headless=True,
- user_data_dir=None,
- )
- await browser_session.start()
- yield browser_session
- await browser_session.kill()
-
-
@pytest.fixture(scope='module')
def mock_llm():
"""Create a mock LLM"""
@@ -114,14 +102,13 @@ def registry():
@pytest.fixture(scope='module')
-async def test_browser(base_url):
+async def browser_session(base_url):
"""Create a real BrowserSession for testing"""
browser_session = BrowserSession(
headless=True,
user_data_dir=None,
)
await browser_session.start()
- # Create tab and navigate to test page
await browser_session.create_new_tab(f'{base_url}/test')
yield browser_session
await browser_session.kill()
@@ -227,11 +214,10 @@ class TestActionRegistryParameterPatterns:
@registry.action('Action with multiple special params')
async def multi_special_action(
text: str,
- browser_session: BrowserSession,
+ page: Page,
page_extraction_llm: BaseChatModel,
available_file_paths: list,
):
- page = await browser_session.get_current_page()
llm_response = await page_extraction_llm.ainvoke('test')
files = available_file_paths or []
@@ -257,24 +243,23 @@ class TestActionRegistryParameterPatterns:
assert 'LLM: Mocked LLM response' in result.extracted_content
assert 'Files: 2' in result.extracted_content
- async def test_no_params_action(self, registry, test_browser):
+ async def test_no_params_action(self, registry, browser_session):
"""Test action with NoParamsAction model"""
@registry.action('No params action', param_model=NoParamsAction)
- async def no_params_action(params: NoParamsAction, browser_session: BrowserSession):
- page = await browser_session.get_current_page()
+ async def no_params_action(params: NoParamsAction, page: Page):
return ActionResult(extracted_content=f'No params action executed on {page.url}')
# Test execution with any parameters (should be ignored)
result = await registry.execute_action(
- 'no_params_action', {'random': 'data', 'should': 'be', 'ignored': True}, browser_session=test_browser
+ 'no_params_action', {'random': 'data', 'should': 'be', 'ignored': True}, browser_session=browser_session
)
assert isinstance(result, ActionResult)
assert 'No params action executed on' in result.extracted_content
assert '/test' in result.extracted_content
- async def test_legacy_browser_parameter_names(self, registry, test_browser):
+ async def test_legacy_browser_parameter_names(self, registry, browser_session):
"""Test that legacy browser parameter names still work"""
@registry.action('Action with legacy browser param')
@@ -288,21 +273,21 @@ class TestActionRegistryParameterPatterns:
return ActionResult(extracted_content=f'Legacy context: {text}, URL: {page.url}')
# Test legacy browser parameter
- result1 = await registry.execute_action('legacy_browser_action', {'text': 'test1'}, browser_session=test_browser)
+ result1 = await registry.execute_action('legacy_browser_action', {'text': 'test1'}, browser_session=browser_session)
assert 'Legacy browser: test1, URL:' in result1.extracted_content
assert '/test' in result1.extracted_content
# Test legacy browser_context parameter
- result2 = await registry.execute_action('legacy_context_action', {'text': 'test2'}, browser_session=test_browser)
+ result2 = await registry.execute_action('legacy_context_action', {'text': 'test2'}, browser_session=browser_session)
assert 'Legacy context: test2, URL:' in result2.extracted_content
assert '/test' in result2.extracted_content
- async def test_page_parameter_optimization(self, test_browser: BrowserSession, httpserver: HTTPServer):
+ async def test_page_parameter_optimization(self, browser_session: BrowserSession, httpserver: HTTPServer):
"""Test that actions can use page: Page parameter directly instead of browser_session"""
registry = Registry()
httpserver.expect_request('/test').respond_with_data('<html><body>Test Page</body></html>')
- page = await test_browser.get_current_page()
+ page = await browser_session.get_current_page()
await page.goto(httpserver.url_for('/test'))
# Action that takes page directly (optimized pattern)
@@ -318,12 +303,12 @@ class TestActionRegistryParameterPatterns:
return ActionResult(extracted_content=f'Browser session: {text}, URL: {page.url}')
# Test direct page parameter
- result1 = await registry.execute_action('direct_page_action', {'text': 'optimized'}, browser_session=test_browser)
+ result1 = await registry.execute_action('direct_page_action', {'text': 'optimized'}, browser_session=browser_session)
assert 'Direct page: optimized, URL:' in result1.extracted_content
assert '/test' in result1.extracted_content
# Test browser_session parameter (should still work)
- result2 = await registry.execute_action('browser_session_action', {'text': 'legacy'}, browser_session=test_browser)
+ result2 = await registry.execute_action('browser_session_action', {'text': 'legacy'}, browser_session=browser_session)
assert 'Browser session: legacy, URL:' in result2.extracted_content
assert '/test' in result2.extracted_content
@@ -335,7 +320,7 @@ class TestActionRegistryParameterPatterns:
async def pydantic_page_action(params: PageActionParams, page: Page):
return ActionResult(extracted_content=f'Pydantic page: {params.message}, URL: {page.url}')
- result3 = await registry.execute_action('pydantic_page_action', {'message': 'pydantic'}, browser_session=test_browser)
+ result3 = await registry.execute_action('pydantic_page_action', {'message': 'pydantic'}, browser_session=browser_session)
assert 'Pydantic page: pydantic, URL:' in result3.extracted_content
assert '/test' in result3.extracted_content
@@ -343,7 +328,7 @@ class TestActionRegistryParameterPatterns:
class TestActionToActionCalling:
"""Test scenarios where actions call other actions"""
- async def test_action_calling_action_with_kwargs(self, registry, test_browser):
+ async def test_action_calling_action_with_kwargs(self, registry, browser_session):
"""Test action calling another action using kwargs (current problematic pattern)"""
# Helper function that actions can call
@@ -366,13 +351,13 @@ class TestActionToActionCalling:
return ActionResult(extracted_content=f'Called result: {intermediate_result.extracted_content}')
# Test the calling chain
- result = await registry.execute_action('calling_action', {'message': 'test'}, browser_session=test_browser)
+ result = await registry.execute_action('calling_action', {'message': 'test'}, browser_session=browser_session)
assert isinstance(result, ActionResult)
assert 'Called result: First: Helper processed: test on' in result.extracted_content
assert '/test' in result.extracted_content
- async def test_google_sheets_style_calling_pattern(self, registry, test_browser):
+ async def test_google_sheets_style_calling_pattern(self, registry, browser_session):
"""Test the specific pattern from Google Sheets actions that causes the error"""
# Simulate the _select_cell_or_range helper function
@@ -401,26 +386,26 @@ class TestActionToActionCalling:
# Test the fixed version (should work)
result_fixed = await registry.execute_action(
- 'select_cell_or_range_fixed', {'cell_or_range': 'A1:F100'}, browser_session=test_browser
+ 'select_cell_or_range_fixed', {'cell_or_range': 'A1:F100'}, browser_session=browser_session
)
assert 'Selected cell A1:F100 on' in result_fixed.extracted_content
assert '/test' in result_fixed.extracted_content
# Test the chained calling pattern
result_chain = await registry.execute_action(
- 'update_range_contents', {'range_name': 'B2:D4', 'new_contents': 'test data'}, browser_session=test_browser
+ 'update_range_contents', {'range_name': 'B2:D4', 'new_contents': 'test data'}, browser_session=browser_session
)
assert 'Updated range B2:D4 with test data' in result_chain.extracted_content
# Test the problematic version (should work with enhanced registry)
result_problematic = await registry.execute_action(
- 'select_cell_or_range', {'cell_or_range': 'A1:F100'}, browser_session=test_browser
+ 'select_cell_or_range', {'cell_or_range': 'A1:F100'}, browser_session=browser_session
)
# With the enhanced registry, this should succeed
assert 'Selected cell A1:F100 on' in result_problematic.extracted_content
assert '/test' in result_problematic.extracted_content
- async def test_complex_action_chain(self, registry, test_browser):
+ async def test_complex_action_chain(self, registry, browser_session):
"""Test a complex chain of actions calling other actions"""
@registry.action('Base action')
@@ -445,7 +430,7 @@ class TestActionToActionCalling:
return ActionResult(extracted_content=f'Top: {middle_result.extracted_content}')
# Test the full chain
- result = await registry.execute_action('top_action', {'original': 'test'}, browser_session=test_browser)
+ result = await registry.execute_action('top_action', {'original': 'test'}, browser_session=browser_session)
assert isinstance(result, ActionResult)
assert 'Top: Middle: Base: processed-enhanced-test on' in result.extracted_content
@@ -455,7 +440,7 @@ class TestActionToActionCalling:
class TestRegistryEdgeCases:
"""Test edge cases and error conditions"""
- async def test_decorated_action_rejects_positional_args(self, registry, test_browser):
+ async def test_decorated_action_rejects_positional_args(self, registry, browser_session):
"""Test that decorated actions reject positional arguments"""
@registry.action('Action that should reject positional args')
@@ -467,10 +452,10 @@ class TestRegistryEdgeCases:
with pytest.raises(
TypeError, match='test_action\\(\\) does not accept positional arguments, only keyword arguments are allowed'
):
- await test_action(test_browser, 'A1:B2')
+ await test_action(browser_session, 'A1:B2')
# Test that calling with keyword arguments works
- result = await test_action(browser_session=test_browser, cell_or_range='A1:B2')
+ result = await test_action(browser_session=browser_session, cell_or_range='A1:B2')
assert isinstance(result, ActionResult)
assert 'Selected cell A1:B2 on' in result.extracted_content
@@ -490,7 +475,7 @@ class TestRegistryEdgeCases:
# No browser_session provided
)
- async def test_missing_required_llm(self, registry, test_browser):
+ async def test_missing_required_llm(self, registry, browser_session):
"""Test that actions requiring page_extraction_llm fail appropriately when not provided"""
from langchain_core.language_models.chat_models import BaseChatModel
@@ -506,11 +491,11 @@ class TestRegistryEdgeCases:
await registry.execute_action(
'requires_llm',
{'text': 'test'},
- browser_session=test_browser,
+ browser_session=browser_session,
# No page_extraction_llm provided
)
- async def test_invalid_parameters(self, registry, test_browser):
+ async def test_invalid_parameters(self, registry, browser_session):
"""Test handling of invalid parameters"""
@registry.action('Typed action')
@@ -522,16 +507,16 @@ class TestRegistryEdgeCases:
await registry.execute_action(
'typed_action',
{'number': 'not a number'}, # Invalid type
- browser_session=test_browser,
+ browser_session=browser_session,
)
- async def test_nonexistent_action(self, registry, test_browser):
+ async def test_nonexistent_action(self, registry, browser_session):
"""Test calling a non-existent action"""
with pytest.raises(ValueError, match='Action nonexistent_action not found'):
- await registry.execute_action('nonexistent_action', {'param': 'value'}, browser_session=test_browser)
+ await registry.execute_action('nonexistent_action', {'param': 'value'}, browser_session=browser_session)
- async def test_sync_action_wrapper(self, registry, test_browser):
+ async def test_sync_action_wrapper(self, registry, browser_session):
"""Test that sync functions are properly wrapped to be async"""
@registry.action('Sync action')
@@ -540,12 +525,12 @@ class TestRegistryEdgeCases:
return ActionResult(extracted_content=f'Sync: {text}')
# Should work even though the original function is sync
- result = await registry.execute_action('sync_action', {'text': 'test'}, browser_session=test_browser)
+ result = await registry.execute_action('sync_action', {'text': 'test'}, browser_session=browser_session)
assert isinstance(result, ActionResult)
assert 'Sync: test' in result.extracted_content
- async def test_excluded_actions(self, test_browser):
+ async def test_excluded_actions(self, browser_session):
"""Test that excluded actions are not registered"""
registry_with_exclusions = Registry[TestContext](exclude_actions=['excluded_action'])
@@ -574,7 +559,7 @@ class TestRegistryEdgeCases:
class TestExistingControllerActions:
"""Test that existing controller actions continue to work"""
- async def test_existing_action_models(self, registry, test_browser):
+ async def test_existing_action_models(self, registry, browser_session):
"""Test that existing action parameter models work correctly"""
@registry.action('Test search', param_model=SearchGoogleAction)
@@ -590,18 +575,18 @@ class TestExistingControllerActions:
return ActionResult(extracted_content=f'Input text: {params.text} at index: {params.index}')
# Test SearchGoogleAction
- result1 = await registry.execute_action('test_search', {'query': 'python testing'}, browser_session=test_browser)
+ result1 = await registry.execute_action('test_search', {'query': 'python testing'}, browser_session=browser_session)
assert 'Searched for: python testing' in result1.extracted_content
# Test ClickElementAction
- result2 = await registry.execute_action('test_click', {'index': 42}, browser_session=test_browser)
+ result2 = await registry.execute_action('test_click', {'index': 42}, browser_session=browser_session)
assert 'Clicked element: 42' in result2.extracted_content
# Test InputTextAction
- result3 = await registry.execute_action('test_input', {'index': 5, 'text': 'test input'}, browser_session=test_browser)
+ result3 = await registry.execute_action('test_input', {'index': 5, 'text': 'test input'}, browser_session=browser_session)
assert 'Input text: test input at index: 5' in result3.extracted_content
- async def test_pydantic_vs_individual_params_consistency(self, registry, test_browser):
+ async def test_pydantic_vs_individual_params_consistency(self, registry, browser_session):
"""Test that pydantic and individual parameter patterns produce consistent results"""
# Action using individual parameters
@@ -621,9 +606,9 @@ class TestExistingControllerActions:
# Both should produce similar results
test_data = {'text': 'hello', 'number': 42}
- result1 = await registry.execute_action('individual_params_action', test_data, browser_session=test_browser)
+ result1 = await registry.execute_action('individual_params_action', test_data, browser_session=browser_session)
- result2 = await registry.execute_action('pydantic_params_action', test_data, browser_session=test_browser)
+ result2 = await registry.execute_action('pydantic_params_action', test_data, browser_session=browser_session)
# Both should extract the same content (just different prefixes)
assert 'hello-42' in result1.extracted_content
@@ -983,16 +968,12 @@ class TestParameterOrdering:
registry = Registry()
@registry.action('Params at end')
- async def params_at_end(browser_session: BrowserSession, page: Page, query: str, limit: int = 10):
+ async def params_at_end(page: Page, query: str, limit: int = 10):
return ActionResult()
action = registry.registry.actions['params_at_end']
assert set(action.param_model.model_fields.keys()) == {'query', 'limit'}
-
-class TestExtractContentPattern:
- """Test the extract_content pattern without async - just test registration"""
-
def test_extract_content_pattern_registration(self):
"""Test that the extract_content pattern with mixed params registers correctly"""
registry = Registry()