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
Refactor test_agent_real_task to use temporary browser session and improve judge context handling
Magnus Müller80dbad0811de
diff --git a/tests/ci/test_agent_real_tasks.py b/tests/ci/test_agent_real_tasks.pyindex 418ba83ea..c38efbf93 100644--- a/tests/ci/test_agent_real_tasks.py+++ b/tests/ci/test_agent_real_tasks.py@@ -1,5 +1,6 @@import globimport os+import tempfileimport aiofilesimport pytest@@ -9,6 +10,7 @@ from pydantic import BaseModelfrom browser_use.agent.service import Agentfrom browser_use.agent.views import AgentHistoryList+from browser_use.browser.session import BrowserSession# Directory containing contributed taskstask_dir = os.path.join(os.path.dirname(__file__), '../agent_tasks')@@ -20,39 +22,31 @@ class JudgeResponse(BaseModel):explanation: str[email protected]('task_file', task_files)@pytest.mark.asyncio[email protected]('task_file', task_files)async def test_agent_real_task(task_file):async with aiofiles.open(task_file, 'r') as f:content = await f.read()task_data = yaml.safe_load(content)-task = task_data['task']- judge_context = task_data.get('judge_context', [])- if not judge_context:- judge_context = ['The agent must solve the task']-- # Use Llama 4 (Groq) for the agent- # agent_llm = ChatOpenAI(- # model='meta-llama/llama-4-maverick-17b-128e-instruct',- # base_url='https://api.groq.com/openai/v1',- # api_key=os.environ.get('GROQ_API_KEY'),- # temperature=0.0,- # )+ judge_context = task_data.get('judge_context', ['The agent must solve the task'])+ max_steps = task_data.get('max_steps', 15)agent_llm = ChatOpenAI(model='gpt-4o-mini')-- # Use gpt-4o-mini as the judgejudge_llm = ChatOpenAI(model='gpt-4o-mini')- # Run the agent- agent = Agent(task=task, llm=agent_llm)- history: AgentHistoryList = await agent.run(max_steps=20)- agent_output = history.final_result() or ''- if not agent_output:- pytest.fail('Agent did not return any output')-- # Compose judge prompt- judge_prompt = f"""+ 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"""You are a evaluator of a browser agent task inside a ci/cd pipeline. Here was the agent's task:{task}@@ -64,7 +58,8 @@ 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 is True, f'Judge failed the agent: {judge_response.explanation}'+ 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()