Skip to content

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

Refactor test_agent_real_tasks to utilize BrowserProfile for session management and improve browser session handling

Magnus Müllerb04d110d57be
diff --git a/tests/ci/test_agent_real_tasks.py b/tests/ci/test_agent_real_tasks.py
index 9d55442e8..6b5130fde 100644
--- a/tests/ci/test_agent_real_tasks.py
+++ b/tests/ci/test_agent_real_tasks.py
@@ -1,6 +1,5 @@
import glob
import os
-import tempfile
import aiofiles
import pytest
@@ -10,6 +9,7 @@ from pydantic import BaseModel
from browser_use.agent.service import Agent
from browser_use.agent.views import AgentHistoryList
+from browser_use.browser.profile import BrowserProfile
from browser_use.browser.session import BrowserSession
# Directory containing contributed tasks
@@ -34,19 +34,17 @@ async def test_agent_real_task(task_file):
agent_llm = ChatOpenAI(model='gpt-4o-mini')
judge_llm = ChatOpenAI(model='gpt-4o-mini')
- with tempfile.TemporaryDirectory() as tmp_profile:
- session = BrowserSession(
- # headless=True,
- user_data_dir=tmp_profile,
- channel='chromium',
- )
- await session.start()
- try:
- agent = Agent(task=task, llm=agent_llm)
- history: AgentHistoryList = await agent.run(max_steps=max_steps)
- agent_output = history.final_result() or ''
- assert agent_output, 'Agent did not return any output'
- judge_prompt = f"""
+ shared_profile = BrowserProfile(
+ headless=False,
+ user_data_dir=None, # use dedicated tmp user_data_dir per session
+ keep_alive=True, # don't close the browser after the agent finishes (only needed to save the session's updated cookies to disk after the run, see below)
+ )
+ session = BrowserSession(browser_profile=shared_profile)
+ agent = Agent(task=task, llm=agent_llm, browser_session=session)
+ history: AgentHistoryList = await agent.run(max_steps=max_steps)
+ agent_output = history.final_result() or ''
+ assert agent_output, 'Agent did not return any output'
+ judge_prompt = f"""
You are a evaluator of a browser agent task inside a ci/cd pipeline. Here was the agent's task:
{task}
@@ -58,8 +56,6 @@ Criteria for success:
Reply in JSON with keys: success (true/false), explanation (string).
"""
- structured_llm = judge_llm.with_structured_output(JudgeResponse)
- judge_response = await structured_llm.ainvoke(judge_prompt)
- assert judge_response.success, f'Judge failed: {judge_response.explanation}'
- finally:
- await session.stop()
+ structured_llm = judge_llm.with_structured_output(JudgeResponse)
+ judge_response = await structured_llm.ainvoke(judge_prompt)
+ assert judge_response.success, f'Judge failed: {judge_response.explanation}'