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
test: Update coverage improvement test for tests/test_service.py
codebeaver-ai[bot]1ec222849498
diff --git a/tests/test_service.py b/tests/test_service.pyindex 6e05e65f9..65ddd5873 100644--- a/tests/test_service.py+++ b/tests/test_service.py@@ -1,9 +1,6 @@import os-import sys-from unittest.mock import AsyncMock, MagicMock, Mock, patch-import pytest-from langchain_core.language_models.chat_models import BaseChatModel+import sysfrom browser_use.agent.message_manager.service import MessageManagerfrom browser_use.agent.service import Agent@@ -14,10 +11,12 @@ from browser_use.browser.views import BrowserStatefrom browser_use.controller.registry.service import Registryfrom browser_use.controller.registry.views import ActionModelfrom browser_use.controller.service import Controller+from langchain_core.language_models.chat_models import BaseChatModel+from pydantic import BaseModel+from unittest.mock import AsyncMock, MagicMock, Mock, patch# run with python -m pytest tests/test_service.py-# run test with:# python -m pytest tests/test_service.pyclass TestAgent:@@ -125,3 +124,94 @@ class TestAgent:assert isinstance(agent._last_result[0], ActionResult)assert 'Test error' in agent._last_result[0].errorassert agent._last_result[0].include_in_memory == True++class TestRegistry:+ @pytest.fixture+ def registry_with_excludes(self):+ return Registry(exclude_actions=['excluded_action'])++ def test_action_decorator_with_excluded_action(self, registry_with_excludes):+ """+ Test that the action decorator does not register an action+ if it's in the exclude_actions list.+ """+ # Define a function to be decorated+ def excluded_action():+ pass++ # Apply the action decorator+ decorated_func = registry_with_excludes.action(description="This should be excluded")(excluded_action)++ # Assert that the decorated function is the same as the original+ assert decorated_func == excluded_action++ # Assert that the action was not added to the registry+ assert 'excluded_action' not in registry_with_excludes.registry.actions++ # Define another function that should be included+ def included_action():+ pass++ # Apply the action decorator to an included action+ registry_with_excludes.action(description="This should be included")(included_action)++ # Assert that the included action was added to the registry+ assert 'included_action' in registry_with_excludes.registry.actions++ @pytest.mark.asyncio+ async def test_execute_action_with_and_without_browser_context(self):+ """+ Test that the execute_action method correctly handles actions with and without a browser context.+ This test ensures that:+ 1. An action requiring a browser context is executed correctly.+ 2. An action not requiring a browser context is executed correctly.+ 3. The browser context is passed to the action function when required.+ 4. The action function receives the correct parameters.+ 5. The method raises an error when a browser context is required but not provided.+ """+ registry = Registry()++ # Define a mock action model+ class TestActionModel(BaseModel):+ param1: str++ # Define mock action functions+ async def test_action_with_browser(param1: str, browser):+ return f"Action executed with {param1} and browser"++ async def test_action_without_browser(param1: str):+ return f"Action executed with {param1}"++ # Register the actions+ registry.registry.actions['test_action_with_browser'] = MagicMock(+ requires_browser=True,+ function=AsyncMock(side_effect=test_action_with_browser),+ param_model=TestActionModel,+ description="Test action with browser"+ )++ registry.registry.actions['test_action_without_browser'] = MagicMock(+ requires_browser=False,+ function=AsyncMock(side_effect=test_action_without_browser),+ param_model=TestActionModel,+ description="Test action without browser"+ )++ # Mock BrowserContext+ mock_browser = MagicMock()++ # Execute the action with a browser context+ result_with_browser = await registry.execute_action('test_action_with_browser', {'param1': 'test_value'}, browser=mock_browser)+ assert result_with_browser == "Action executed with test_value and browser"++ # Execute the action without a browser context+ result_without_browser = await registry.execute_action('test_action_without_browser', {'param1': 'test_value'})+ assert result_without_browser == "Action executed with test_value"++ # Test error when browser is required but not provided+ with pytest.raises(RuntimeError, match="Action test_action_with_browser requires browser but none provided"):+ await registry.execute_action('test_action_with_browser', {'param1': 'test_value'})++ # Verify that the action functions were called with correct parameters+ registry.registry.actions['test_action_with_browser'].function.assert_called_once_with(param1='test_value', browser=mock_browser)+ registry.registry.actions['test_action_without_browser'].function.assert_called_once_with(param1='test_value')\ No newline at end of file