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
Clean up tests
magmuellerd24f46a083a3
diff --git a/tests/test_service.py b/tests/test_service.pyindex 4cdce7a27..ec2632077 100644--- a/tests/test_service.py+++ b/tests/test_service.py@@ -1,4 +1,9 @@+import os+import sys+from unittest.mock import MagicMock, Mock+import pytest+from langchain_core.language_models.chat_models import BaseChatModelfrom browser_use.agent.service import Agentfrom browser_use.browser.browser import Browser@@ -6,494 +11,68 @@ from browser_use.browser.context import BrowserContextfrom 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 unittest.mock import MagicMock, Mock--class TestAgent:- @pytest.fixture- def mock_controller(self):- controller = Mock(spec=Controller)- registry_mock = MagicMock()- registry_mock.registry.actions = {- 'test_action': MagicMock(param_model=MagicMock())- }- controller.registry = registry_mock- return controller-- @pytest.fixture- def mock_llm(self):- return Mock(spec=BaseChatModel)-- def test_convert_initial_actions(self, mock_controller, mock_llm):- """- Test that the _convert_initial_actions method correctly converts- dictionary-based actions to ActionModel instances.- """- # Arrange- agent = Agent(task="Test task", llm=mock_llm, controller=mock_controller)- initial_actions = [- {"test_action": {"param1": "value1", "param2": "value2"}}- ]-- # Mock the ActionModel- mock_action_model = Mock(spec=ActionModel)- agent.ActionModel = mock_action_model-- # Act- result = agent._convert_initial_actions(initial_actions)-- # Assert- assert len(result) == 1- mock_action_model.assert_called_once()- mock_controller.registry.registry.actions['test_action'].param_model.assert_called_once_with(param1="value1", param2="value2")-- # Check that the result is a list of ActionModel instances- assert isinstance(result[0], Mock)- assert result[0] == mock_action_model.return_value-- @pytest.fixture- def mock_controller(self):- controller = Mock(spec=Controller)- controller.registry = MagicMock()- controller.registry.get_prompt_description.return_value = "Test prompt description"- controller.registry.create_action_model.return_value = MagicMock()- return controller-- @pytest.fixture- def mock_llm(self):- return Mock(spec=BaseChatModel)-- @pytest.fixture- def mock_browser(self):- return Mock(spec=Browser)-- @pytest.fixture- def mock_browser_context(self):- return Mock(spec=BrowserContext)-- def test_convert_initial_actions(self, mock_controller, mock_llm, mock_browser, mock_browser_context):- """- Test that the _convert_initial_actions method correctly converts- dictionary-based actions to ActionModel instances.- """- # Arrange- agent = Agent(- task="Test task",- llm=mock_llm,- controller=mock_controller,- browser=mock_browser,- browser_context=mock_browser_context- )- initial_actions = [- {"test_action": {"param1": "value1", "param2": "value2"}}- ]-- # Mock the ActionModel and its instance- mock_action_model_class = MagicMock()- mock_action_model_instance = MagicMock()- mock_action_model_class.return_value = mock_action_model_instance- agent.ActionModel = mock_action_model_class-- # Mock the param_model- mock_param_model = MagicMock()- mock_controller.registry.registry.actions = {- 'test_action': MagicMock(param_model=mock_param_model)- }-- # Act- result = agent._convert_initial_actions(initial_actions)-- # Assert- assert len(result) == 1- mock_param_model.assert_called_once_with(param1="value1", param2="value2")- mock_action_model_class.assert_called_once_with(test_action=mock_param_model.return_value)- assert result[0] == mock_action_model_instance-- @pytest.fixture- def mock_controller(self):- controller = Mock(spec=Controller)- registry_mock = MagicMock()- registry_mock.registry.actions = {- 'test_action': MagicMock(param_model=MagicMock())- }- controller.registry = registry_mock- return controller-- @pytest.fixture- def mock_llm(self):- return Mock(spec=BaseChatModel)-- def test_convert_initial_actions(self, mock_controller, mock_llm):- """- Test that the _convert_initial_actions method correctly converts- dictionary-based actions to ActionModel instances.- """- # Arrange- agent = Agent(task="Test task", llm=mock_llm, controller=mock_controller)- initial_actions = [- {"test_action": {"param1": "value1", "param2": "value2"}}- ]-- # Mock the ActionModel- mock_action_model = Mock(spec=ActionModel)- agent.ActionModel = mock_action_model-- # Act- result = agent._convert_initial_actions(initial_actions)-- # Assert- assert len(result) == 1- mock_action_model.assert_called_once()- mock_controller.registry.registry.actions['test_action'].param_model.assert_called_once_with(param1="value1", param2="value2")-- # Check that the result is a list of ActionModel instances- assert isinstance(result[0], Mock)- assert result[0] == mock_action_model.return_value-- @pytest.fixture- def mock_controller(self):- controller = Mock(spec=Controller)- registry_mock = MagicMock()- registry_mock.registry.actions = {- 'test_action': MagicMock(param_model=MagicMock())- }- controller.registry = registry_mock- return controller-- @pytest.fixture- def mock_llm(self):- return Mock(spec=BaseChatModel)-- def test_convert_initial_actions(self, mock_controller, mock_llm):- """- Test that the _convert_initial_actions method correctly converts- dictionary-based actions to ActionModel instances.- """- # Arrange- agent = Agent(task="Test task", llm=mock_llm, controller=mock_controller)- initial_actions = [- {"test_action": {"param1": "value1", "param2": "value2"}}- ]-- # Mock the ActionModel- mock_action_model = Mock()- agent.ActionModel = mock_action_model-- # Act- result = agent._convert_initial_actions(initial_actions)-- # Assert- assert len(result) == 1- mock_action_model.assert_called_once()- mock_controller.registry.registry.actions['test_action'].param_model.assert_called_once_with(param1="value1", param2="value2")-- # Check that the result is a list of ActionModel instances- assert isinstance(result[0], Mock)- assert result[0] == mock_action_model.return_value-- @pytest.fixture- def mock_controller(self):- controller = Mock(spec=Controller)- registry_mock = MagicMock()- param_model_mock = MagicMock()- param_model_mock.return_value = MagicMock(name="ValidatedParams")- action_info_mock = MagicMock(param_model=param_model_mock)- registry_mock.registry.actions = {- 'test_action': action_info_mock- }- controller.registry = registry_mock- return controller-- @pytest.fixture- def mock_llm(self):- return Mock(spec=BaseChatModel)-- def test_convert_initial_actions(self, mock_controller, mock_llm):- """- Test that the _convert_initial_actions method correctly converts- dictionary-based actions to ActionModel instances.- """- # Arrange- agent = Agent(task="Test task", llm=mock_llm, controller=mock_controller)- initial_actions = [- {"test_action": {"param1": "value1", "param2": "value2"}}- ]-- # Mock the ActionModel- mock_action_model = MagicMock(name="ActionModel")- mock_action_model_instance = MagicMock(name="ActionModelInstance")- mock_action_model.return_value = mock_action_model_instance- agent.ActionModel = mock_action_model- # Act- result = agent._convert_initial_actions(initial_actions)- # Assert- assert len(result) == 1- mock_controller.registry.registry.actions['test_action'].param_model.assert_called_once_with(param1="value1", param2="value2")- mock_action_model.assert_called_once()- assert isinstance(result[0], MagicMock)- assert result[0] == mock_action_model_instance-- # Check that the ActionModel was called with the correct parameters- call_args = mock_action_model.call_args[1]- assert 'test_action' in call_args- assert call_args['test_action'] == mock_controller.registry.registry.actions['test_action'].param_model.return_value-- @pytest.fixture- def mock_controller(self):- controller = MagicMock(spec=Controller)- registry_mock = MagicMock()- param_model_mock = MagicMock()- param_model_mock.return_value = MagicMock(name="ValidatedParams")- action_info_mock = MagicMock(param_model=param_model_mock)- registry_mock.registry.actions = {- 'test_action': action_info_mock- }- controller.registry = registry_mock- return controller-- @pytest.fixture- def mock_llm(self):- return Mock(spec=BaseChatModel)-- def test_convert_initial_actions(self, mock_controller, mock_llm):- """- Test that the _convert_initial_actions method correctly converts- dictionary-based actions to ActionModel instances.- """- # Arrange- agent = Agent(task="Test task", llm=mock_llm, controller=mock_controller)- initial_actions = [- {"test_action": {"param1": "value1", "param2": "value2"}}- ]-- # Mock the ActionModel- mock_action_model = MagicMock(name="ActionModel")- mock_action_model_instance = MagicMock(name="ActionModelInstance")- mock_action_model.return_value = mock_action_model_instance- agent.ActionModel = mock_action_model-- # Act- result = agent._convert_initial_actions(initial_actions)-- # Assert- assert len(result) == 1- mock_controller.registry.registry.actions['test_action'].param_model.assert_called_once_with(param1="value1", param2="value2")- mock_action_model.assert_called_once()- assert isinstance(result[0], MagicMock)- assert result[0] == mock_action_model_instance-- # Check that the ActionModel was called with the correct parameters- call_args = mock_action_model.call_args[1]- assert 'test_action' in call_args- assert call_args['test_action'] == mock_controller.registry.registry.actions['test_action'].param_model.return_value-- @pytest.fixture- def mock_controller(self):- controller = MagicMock(spec=Controller)- registry_mock = MagicMock()- param_model_mock = MagicMock()- param_model_mock.return_value = MagicMock(name="ValidatedParams")- action_info_mock = MagicMock(param_model=param_model_mock)- registry_mock.registry.actions = {- 'test_action': action_info_mock- }- controller.registry = registry_mock- return controller-- @pytest.fixture- def mock_llm(self):- return Mock(spec=BaseChatModel)-- def test_convert_initial_actions(self, mock_controller, mock_llm):- """- Test that the _convert_initial_actions method correctly converts- dictionary-based actions to ActionModel instances.- """- # Arrange- agent = Agent(task="Test task", llm=mock_llm, controller=mock_controller)- initial_actions = [- {"test_action": {"param1": "value1", "param2": "value2"}}- ]-- # Mock the ActionModel- mock_action_model = MagicMock(name="ActionModel")- mock_action_model_instance = MagicMock(name="ActionModelInstance")- mock_action_model.return_value = mock_action_model_instance- agent.ActionModel = mock_action_model-- # Act- result = agent._convert_initial_actions(initial_actions)-- # Assert- assert len(result) == 1- mock_controller.registry.registry.actions['test_action'].param_model.assert_called_once_with(param1="value1", param2="value2")- mock_action_model.assert_called_once()- assert isinstance(result[0], MagicMock)- assert result[0] == mock_action_model_instance-- # Check that the ActionModel was called with the correct parameters- call_args = mock_action_model.call_args[1]- assert 'test_action' in call_args- assert call_args['test_action'] == mock_controller.registry.registry.actions['test_action'].param_model.return_value-- @pytest.fixture- def mock_controller(self):- controller = Mock(spec=Controller)- registry = Mock(spec=Registry)- registry.registry = MagicMock()- registry.registry.actions = {- 'test_action': MagicMock(param_model=MagicMock())- }- controller.registry = registry- return controller-- @pytest.fixture- def mock_llm(self):- return Mock(spec=BaseChatModel)-- def test_convert_initial_actions(self, mock_controller, mock_llm):- """- Test that the _convert_initial_actions method correctly converts- dictionary-based actions to ActionModel instances.-- This test ensures that:- 1. The method processes the initial actions correctly.- 2. The correct param_model is called with the right parameters.- 3. The ActionModel is created with the validated parameters.- 4. The method returns a list of ActionModel instances.- """- # Arrange- agent = Agent(task="Test task", llm=mock_llm, controller=mock_controller)- initial_actions = [- {"test_action": {"param1": "value1", "param2": "value2"}}- ]-- # Mock the ActionModel- mock_action_model = MagicMock(spec=ActionModel)- mock_action_model_instance = MagicMock()- mock_action_model.return_value = mock_action_model_instance- agent.ActionModel = mock_action_model-- # Act- result = agent._convert_initial_actions(initial_actions)-- # Assert- assert len(result) == 1- mock_controller.registry.registry.actions['test_action'].param_model.assert_called_once_with(param1="value1", param2="value2")- mock_action_model.assert_called_once()- assert isinstance(result[0], MagicMock)- assert result[0] == mock_action_model_instance-- # Check that the ActionModel was called with the correct parameters- call_args = mock_action_model.call_args[1]- assert 'test_action' in call_args- assert call_args['test_action'] == mock_controller.registry.registry.actions['test_action'].param_model.return_value-- @pytest.fixture- def mock_controller(self):- controller = Mock(spec=Controller)- registry_mock = MagicMock()- param_model_mock = MagicMock()- param_model_mock.return_value = MagicMock(name="ValidatedParams")- action_info_mock = MagicMock(param_model=param_model_mock)- registry_mock.registry.actions = {- 'test_action': action_info_mock- }- controller.registry = registry_mock- return controller-- @pytest.fixture- def mock_llm(self):- return Mock(spec=BaseChatModel)-- def test_convert_initial_actions(self, mock_controller, mock_llm):- """- Test that the _convert_initial_actions method correctly converts- dictionary-based actions to ActionModel instances.-- This test ensures that:- 1. The method processes the initial actions correctly.- 2. The correct param_model is called with the right parameters.- 3. The ActionModel is created with the validated parameters.- 4. The method returns a list of ActionModel instances.- """- # Arrange- agent = Agent(task="Test task", llm=mock_llm, controller=mock_controller)- initial_actions = [- {"test_action": {"param1": "value1", "param2": "value2"}}- ]-- # Mock the ActionModel- mock_action_model = MagicMock(name="ActionModel")- mock_action_model_instance = MagicMock(name="ActionModelInstance")- mock_action_model.return_value = mock_action_model_instance- agent.ActionModel = mock_action_model-- # Act- result = agent._convert_initial_actions(initial_actions)-- # Assert- assert len(result) == 1- mock_controller.registry.registry.actions['test_action'].param_model.assert_called_once_with(param1="value1", param2="value2")- mock_action_model.assert_called_once()- assert isinstance(result[0], MagicMock)- assert result[0] == mock_action_model_instance-- # Check that the ActionModel was called with the correct parameters- call_args = mock_action_model.call_args[1]- assert 'test_action' in call_args- assert call_args['test_action'] == mock_controller.registry.registry.actions['test_action'].param_model.return_value-- @pytest.fixture- def mock_controller(self):- controller = Mock(spec=Controller)- registry_mock = MagicMock()- registry_mock.registry.actions = {- 'test_action': MagicMock(param_model=MagicMock())- }- controller.registry = registry_mock- return controller-- @pytest.fixture- def mock_llm(self):- return Mock(spec=BaseChatModel)-- def test_convert_initial_actions(self, mock_controller, mock_llm):- """- Test that the _convert_initial_actions method correctly converts- dictionary-based actions to ActionModel instances.-- This test ensures that:- 1. The method processes the initial actions correctly.- 2. The correct param_model is called with the right parameters.- 3. The ActionModel is created with the validated parameters.- 4. The method returns a list of ActionModel instances.- """- # Arrange- agent = Agent(task="Test task", llm=mock_llm, controller=mock_controller)- initial_actions = [- {"test_action": {"param1": "value1", "param2": "value2"}}- ]-- # Mock the ActionModel- mock_action_model = MagicMock(name="ActionModel")- mock_action_model_instance = MagicMock(name="ActionModelInstance")- mock_action_model.return_value = mock_action_model_instance- agent.ActionModel = mock_action_model-- # Act- result = agent._convert_initial_actions(initial_actions)-- # Assert- assert len(result) == 1- mock_controller.registry.registry.actions['test_action'].param_model.assert_called_once_with(param1="value1", param2="value2")- mock_action_model.assert_called_once()- assert isinstance(result[0], MagicMock)- assert result[0] == mock_action_model_instance-- # Check that the ActionModel was called with the correct parameters- call_args = mock_action_model.call_args[1]- assert 'test_action' in call_args- assert call_args['test_action'] == mock_controller.registry.registry.actions['test_action'].param_model.return_value\ No newline at end of file+# run test with:+# python -m pytest tests/test_service.py+class TestAgent:+ @pytest.fixture+ def mock_controller(self):+ controller = Mock(spec=Controller)+ registry = Mock(spec=Registry)+ registry.registry = MagicMock()+ registry.registry.actions = {'test_action': MagicMock(param_model=MagicMock())} # type: ignore+ controller.registry = registry+ return controller++ @pytest.fixture+ def mock_llm(self):+ return Mock(spec=BaseChatModel)++ @pytest.fixture+ def mock_browser(self):+ return Mock(spec=Browser)++ @pytest.fixture+ def mock_browser_context(self):+ return Mock(spec=BrowserContext)++ def test_convert_initial_actions(self, mock_controller, mock_llm, mock_browser, mock_browser_context): # type: ignore+ """+ Test that the _convert_initial_actions method correctly converts+ dictionary-based actions to ActionModel instances.++ This test ensures that:+ 1. The method processes the initial actions correctly.+ 2. The correct param_model is called with the right parameters.+ 3. The ActionModel is created with the validated parameters.+ 4. The method returns a list of ActionModel instances.+ """+ # Arrange+ agent = Agent(+ task='Test task', llm=mock_llm, controller=mock_controller, browser=mock_browser, browser_context=mock_browser_context+ )+ initial_actions = [{'test_action': {'param1': 'value1', 'param2': 'value2'}}]++ # Mock the ActionModel+ mock_action_model = MagicMock(spec=ActionModel)+ mock_action_model_instance = MagicMock()+ mock_action_model.return_value = mock_action_model_instance+ agent.ActionModel = mock_action_model # type: ignore++ # Act+ result = agent._convert_initial_actions(initial_actions)++ # Assert+ assert len(result) == 1+ mock_controller.registry.registry.actions['test_action'].param_model.assert_called_once_with( # type: ignore+ param1='value1', param2='value2'+ )+ mock_action_model.assert_called_once()+ assert isinstance(result[0], MagicMock)+ assert result[0] == mock_action_model_instance++ # Check that the ActionModel was called with the correct parameters+ call_args = mock_action_model.call_args[1]+ assert 'test_action' in call_args+ assert call_args['test_action'] == mock_controller.registry.registry.actions['test_action'].param_model.return_value # type: ignore