Skip to content

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

Update tests with easier output functions

magmueller9525e21f5bd1
diff --git a/tests/test_core_functionality.py b/tests/test_core_functionality.py
index e2bfb10bb..5b58624d4 100644
--- a/tests/test_core_functionality.py
+++ b/tests/test_core_functionality.py
@@ -2,6 +2,7 @@ import pytest
from langchain_openai import ChatOpenAI
from browser_use.agent.service import Agent
+from browser_use.agent.views import AgentHistoryList
from browser_use.controller.service import Controller
@@ -13,8 +14,10 @@ def llm():
@pytest.fixture
async def controller():
- """Initialize the controller"""
- controller = Controller(keep_open=False)
+ """Initialize the controller with persistent browser"""
+ controller = Controller(keep_open=True)
+ await controller.browser._initialize_session()
+
try:
yield controller
finally:
@@ -30,9 +33,8 @@ async def test_search_google(llm, controller):
llm=llm,
controller=controller,
)
- history = await agent.run(max_steps=2)
- actions = [h.model_output.action for h in history if h.model_output and h.model_output.action]
- action_names = [list(action.model_dump(exclude_unset=True).keys())[0] for action in actions]
+ history: AgentHistoryList = await agent.run(max_steps=2)
+ action_names = history.all_model_output_action_names()
assert 'search_google' in action_names
@@ -45,8 +47,7 @@ async def test_go_to_url(llm, controller):
controller=controller,
)
history = await agent.run(max_steps=2)
- actions = [h.model_output.action for h in history if h.model_output and h.model_output.action]
- action_names = [list(action.model_dump(exclude_unset=True).keys())[0] for action in actions]
+ action_names = history.all_model_output_action_names()
assert 'go_to_url' in action_names
@@ -59,8 +60,7 @@ async def test_go_back(llm, controller):
controller=controller,
)
history = await agent.run(max_steps=3)
- actions = [h.model_output.action for h in history if h.model_output and h.model_output.action]
- action_names = [list(action.model_dump(exclude_unset=True).keys())[0] for action in actions]
+ action_names = history.all_model_output_action_names()
assert 'go_to_url' in action_names
assert 'go_back' in action_names
@@ -74,8 +74,7 @@ async def test_click_element(llm, controller):
controller=controller,
)
history = await agent.run(max_steps=4)
- actions = [h.model_output.action for h in history if h.model_output and h.model_output.action]
- action_names = [list(action.model_dump(exclude_unset=True).keys())[0] for action in actions]
+ action_names = history.all_model_output_action_names()
assert 'go_to_url' in action_names
assert 'click_element' in action_names
@@ -89,8 +88,7 @@ async def test_input_text(llm, controller):
controller=controller,
)
history = await agent.run(max_steps=4)
- actions = [h.model_output.action for h in history if h.model_output and h.model_output.action]
- action_names = [list(action.model_dump(exclude_unset=True).keys())[0] for action in actions]
+ action_names = history.all_model_output_action_names()
assert 'go_to_url' in action_names
assert 'input_text' in action_names
@@ -104,8 +102,7 @@ async def test_switch_tab(llm, controller):
controller=controller,
)
history = await agent.run(max_steps=6)
- actions = [h.model_output.action for h in history if h.model_output and h.model_output.action]
- action_names = [list(action.model_dump(exclude_unset=True).keys())[0] for action in actions]
+ action_names = history.all_model_output_action_names()
open_tab_count = action_names.count('open_tab')
assert open_tab_count >= 2
assert 'switch_tab' in action_names
@@ -120,8 +117,7 @@ async def test_open_new_tab(llm, controller):
controller=controller,
)
history = await agent.run(max_steps=3)
- actions = [h.model_output.action for h in history if h.model_output and h.model_output.action]
- action_names = [list(action.model_dump(exclude_unset=True).keys())[0] for action in actions]
+ action_names = history.all_model_output_action_names()
assert 'open_tab' in action_names
@@ -134,12 +130,12 @@ async def test_extract_page_content(llm, controller):
controller=controller,
)
history = await agent.run(max_steps=3)
- actions = [h.model_output.action for h in history if h.model_output and h.model_output.action]
- action_names = [list(action.model_dump(exclude_unset=True).keys())[0] for action in actions]
+ action_names = history.all_model_output_action_names()
assert 'go_to_url' in action_names
assert 'extract_content' in action_names
+# pytest -k test_done_action
@pytest.mark.asyncio
async def test_done_action(llm, controller):
"""Test 'Complete task' action"""
@@ -148,16 +144,16 @@ async def test_done_action(llm, controller):
llm=llm,
controller=controller,
)
+
history = await agent.run(max_steps=3)
- actions = [h.model_output.action for h in history if h.model_output and h.model_output.action]
- action_names = [list(action.model_dump(exclude_unset=True).keys())[0] for action in actions]
+ action_names = history.all_model_output_action_names()
assert 'go_to_url' in action_names
assert 'done' in action_names
# run with: pytest -k test_scroll_down
@pytest.mark.asyncio
-async def test_scroll_down(llm, controller):
+async def test_scroll_down(llm, controller: Controller):
"""Test 'Scroll down' action and validate that the page actually scrolled"""
agent = Agent(
task="Go to 'https://en.wikipedia.org/wiki/Internet' and scroll down the page.",