Skip to content

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

Refactor tests to utilize direct action calls in Tools

Magnus Müller5a8f30d3783b
diff --git a/tests/ci/interaction/test_dropdown_react_submit.py b/tests/ci/interaction/test_dropdown_react_submit.py
index 808c0d446..f220e2afd 100644
--- a/tests/ci/interaction/test_dropdown_react_submit.py
+++ b/tests/ci/interaction/test_dropdown_react_submit.py
@@ -133,16 +133,9 @@ class TestReactDropdownSubmit:
httpserver.expect_request('/submit', method='POST').respond_with_handler(handle_submit)
# Navigate to the React form
- from browser_use.agent.views import ActionModel
- from browser_use.tools.views import GoToUrlAction
-
base_url = httpserver.url_for('/')
- class NavigateActionModel(ActionModel):
- navigate: GoToUrlAction | None = None
-
- action = NavigateActionModel(navigate=GoToUrlAction(url=f'{base_url}react-dropdown', new_tab=False))
- result = await tools.act(action, browser_session)
+ result = await tools.navigate(url=f'{base_url}react-dropdown', new_tab=False, browser_session=browser_session)
assert result.error is None
# Wait for React to initialize
@@ -152,37 +145,20 @@ class TestReactDropdownSubmit:
# Get browser state to find dropdown elements
await browser_session.get_browser_state_summary()
- selector_map = await browser_session.get_selector_map()
- # Find the dropdowns
- employment_status_element = None
- hire_type_element = None
-
- for idx, element in selector_map.items():
- if element.attributes.get('id') == 'employment-status':
- employment_status_element = idx
- elif element.attributes.get('id') == 'hire-type':
- hire_type_element = idx
+ # Find the dropdowns by ID
+ employment_status_element = await browser_session.get_index_by_id('employment-status')
+ hire_type_element = await browser_session.get_index_by_id('hire-type')
assert employment_status_element is not None, 'Could not find employment-status dropdown'
assert hire_type_element is not None, 'Could not find hire-type dropdown'
- # Create a model for the standard select_dropdown action
- class SelectDropdownModel(ActionModel):
- select_dropdown: dict
-
# Select "Part Time" from employment status dropdown
- result = await tools.act(
- SelectDropdownModel(select_dropdown={'index': employment_status_element, 'text': 'Part Time'}),
- browser_session,
- )
+ result = await tools.select_dropdown(index=employment_status_element, text='Part Time', browser_session=browser_session)
assert result.error is None, f'Failed to select employment status: {result.error}'
# Select "W-2" from hire type dropdown
- result = await tools.act(
- SelectDropdownModel(select_dropdown={'index': hire_type_element, 'text': 'W-2'}),
- browser_session,
- )
+ result = await tools.select_dropdown(index=hire_type_element, text='W-2', browser_session=browser_session)
assert result.error is None, f'Failed to select hire type: {result.error}'
# Find and click the submit button
@@ -197,14 +173,8 @@ class TestReactDropdownSubmit:
assert submit_button is not None, 'Could not find submit button'
- # Create a model for the standard click action
- class ClickModel(ActionModel):
- click: dict
-
- result = await tools.act(
- ClickModel(click={'index': submit_button}),
- browser_session,
- )
+ # Click the submit button
+ result = await tools.click(index=submit_button, browser_session=browser_session)
assert result.error is None, f'Failed to click submit: {result.error}'
# Wait for submission
diff --git a/tests/ci/interaction/test_dropdown_vue_submit.py b/tests/ci/interaction/test_dropdown_vue_submit.py
index 3b90a5389..3e69713cb 100644
--- a/tests/ci/interaction/test_dropdown_vue_submit.py
+++ b/tests/ci/interaction/test_dropdown_vue_submit.py
@@ -110,16 +110,9 @@ class TestVueDropdownSubmit:
httpserver.expect_request('/submit', method='POST').respond_with_handler(handle_submit)
# Navigate to the Vue.js form
- from browser_use.agent.views import ActionModel
- from browser_use.tools.views import GoToUrlAction
-
base_url = httpserver.url_for('/')
- class NavigateActionModel(ActionModel):
- navigate: GoToUrlAction | None = None
-
- action = NavigateActionModel(navigate=GoToUrlAction(url=f'{base_url}vue-dropdown', new_tab=False))
- result = await tools.act(action, browser_session)
+ result = await tools.navigate(url=f'{base_url}vue-dropdown', new_tab=False, browser_session=browser_session)
assert result.error is None
# Wait for Vue to initialize
@@ -129,37 +122,20 @@ class TestVueDropdownSubmit:
# Get browser state to find dropdown elements
await browser_session.get_browser_state_summary()
- selector_map = await browser_session.get_selector_map()
- # Find the pay frequency dropdown (should be index 3 or similar)
- pay_freq_element = None
- hire_type_element = None
-
- for idx, element in selector_map.items():
- if element.attributes.get('id') == 'pay-frequency':
- pay_freq_element = idx
- elif element.attributes.get('id') == 'hire-type':
- hire_type_element = idx
+ # Find the dropdowns by ID
+ pay_freq_element = await browser_session.get_index_by_id('pay-frequency')
+ hire_type_element = await browser_session.get_index_by_id('hire-type')
assert pay_freq_element is not None, 'Could not find pay-frequency dropdown'
assert hire_type_element is not None, 'Could not find hire-type dropdown'
- # Create a model for the standard select_dropdown action
- class SelectDropdownModel(ActionModel):
- select_dropdown: dict
-
# Select "per hour" from pay frequency dropdown
- result = await tools.act(
- SelectDropdownModel(select_dropdown={'index': pay_freq_element, 'text': 'per hour'}),
- browser_session,
- )
+ result = await tools.select_dropdown(index=pay_freq_element, text='per hour', browser_session=browser_session)
assert result.error is None, f'Failed to select pay frequency: {result.error}'
# Select "1099" from hire type dropdown
- result = await tools.act(
- SelectDropdownModel(select_dropdown={'index': hire_type_element, 'text': '1099'}),
- browser_session,
- )
+ result = await tools.select_dropdown(index=hire_type_element, text='1099', browser_session=browser_session)
assert result.error is None, f'Failed to select hire type: {result.error}'
# Find and click the submit button
@@ -174,14 +150,8 @@ class TestVueDropdownSubmit:
assert submit_button is not None, 'Could not find submit button'
- # Create a model for the standard click action
- class ClickModel(ActionModel):
- click: dict
-
- result = await tools.act(
- ClickModel(click={'index': submit_button}),
- browser_session,
- )
+ # Click the submit button
+ result = await tools.click(index=submit_button, browser_session=browser_session)
assert result.error is None, f'Failed to click submit: {result.error}'
# Wait for submission
diff --git a/tests/ci/test_browser_watchdog_downloads_upload_full_circle.py b/tests/ci/test_browser_watchdog_downloads_upload_full_circle.py
index 92b15750e..a427c25d6 100644
--- a/tests/ci/test_browser_watchdog_downloads_upload_full_circle.py
+++ b/tests/ci/test_browser_watchdog_downloads_upload_full_circle.py
@@ -8,13 +8,11 @@ from pathlib import Path
import pytest
from pytest_httpserver import HTTPServer
-from browser_use.agent.views import ActionModel
from browser_use.browser import BrowserSession
from browser_use.browser.events import BrowserStateRequestEvent, FileDownloadedEvent
from browser_use.browser.profile import BrowserProfile
from browser_use.filesystem.file_system import FileSystem
from browser_use.tools.service import Tools
-from browser_use.tools.views import ClickElementAction, GoToUrlAction, UploadFileAction
@pytest.fixture(scope='function')
@@ -161,12 +159,7 @@ class TestDownloadUploadFullCircle:
base_url = f'http://{download_upload_server.host}:{download_upload_server.port}'
# Step 1: Navigate to download page
- class NavigateActionModel(ActionModel):
- navigate: GoToUrlAction | None = None
-
- result = await tools.act(
- NavigateActionModel(navigate=GoToUrlAction(url=f'{base_url}/download-page', new_tab=False)), browser_session
- )
+ result = await tools.navigate(url=f'{base_url}/download-page', new_tab=False, browser_session=browser_session)
assert result.error is None, f'Navigation to download page failed: {result.error}'
await asyncio.sleep(0.5)
@@ -176,23 +169,13 @@ class TestDownloadUploadFullCircle:
state_result = await event.event_result()
assert state_result is not None
assert state_result.dom_state is not None
- assert state_result.dom_state.selector_map is not None
-
- # Find download link
- download_link_index = None
- for idx, element in state_result.dom_state.selector_map.items():
- if element.attributes and element.attributes.get('id') == 'downloadLink':
- download_link_index = idx
- break
+ # Find download link by ID
+ download_link_index = await browser_session.get_index_by_id('downloadLink')
assert download_link_index is not None, 'Download link not found'
# Step 2: Click download link and wait for download
- class ClickActionModel(ActionModel):
- click: ClickElementAction | None = None
-
- # Click the download link
- result = await tools.act(ClickActionModel(click=ClickElementAction(index=download_link_index)), browser_session)
+ result = await tools.click(index=download_link_index, browser_session=browser_session)
assert result.error is None, f'Click on download link failed: {result.error}'
# Wait for the download event
@@ -227,9 +210,7 @@ class TestDownloadUploadFullCircle:
print(f'📑 Tabs before navigation: {len(tabs_before)} tabs')
for i, tab in enumerate(tabs_before):
print(f' Tab {i}: {tab.url}')
- result = await tools.act(
- NavigateActionModel(navigate=GoToUrlAction(url=f'{base_url}/upload-page', new_tab=True)), browser_session
- )
+ result = await tools.navigate(url=f'{base_url}/upload-page', new_tab=True, browser_session=browser_session)
assert result.error is None, f'Navigation to upload page failed: {result.error}'
print(f'✅ Navigation result: {result.extracted_content}')
@@ -269,13 +250,11 @@ class TestDownloadUploadFullCircle:
assert file_input_index is not None, 'File input not found'
# Step 4: Upload the downloaded file
- class UploadActionModel(ActionModel):
- upload_file: UploadFileAction | None = None
-
# The downloaded file should be automatically available for upload
- result = await tools.act(
- UploadActionModel(upload_file=UploadFileAction(index=file_input_index, path=downloaded_file_path)),
- browser_session,
+ result = await tools.upload_file(
+ index=file_input_index,
+ path=downloaded_file_path,
+ browser_session=browser_session,
available_file_paths=[], # Empty, but file is in downloaded_files
file_system=file_system,
)
@@ -302,7 +281,7 @@ class TestDownloadUploadFullCircle:
assert submit_button_index is not None, 'Submit button not found'
# Click the submit button
- result = await tools.act(ClickActionModel(click=ClickElementAction(index=submit_button_index)), browser_session)
+ result = await tools.click(index=submit_button_index, browser_session=browser_session)
assert result.error is None, f'Click on submit button failed: {result.error}'
# Wait for JavaScript to process the upload