Skip to content

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

Fix RuntimeError: no running event loop

Anirudha619c7941adec611
diff --git a/tests/test_vision.py b/tests/test_vision.py
index 9851b1a6f..cc0b2568c 100644
--- a/tests/test_vision.py
+++ b/tests/test_vision.py
@@ -17,6 +17,7 @@ sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from langchain_openai import ChatOpenAI
from browser_use import Agent, AgentHistoryList, Controller
+import asyncio
llm = ChatOpenAI(model='gpt-4o')
controller = Controller()
@@ -39,14 +40,24 @@ async def done(text: str) -> str:
return 'call explain_screen'
-agent = Agent(
- task='call explain_screen all the time the user asks you questions e.g. about the page like bbox which you see are labels - your task is to explain it and get the next question',
- llm=llm,
- controller=controller,
- browser=Browser(config=BrowserConfig(disable_security=True, headless=False)),
-)
[email protected](scope='function')
+def event_loop():
+ """Create an instance of the default event loop for each test case."""
+ loop = asyncio.get_event_loop_policy().new_event_loop()
+ yield loop
+ loop.close()
@pytest.mark.skip(reason='this is for local testing only')
async def test_vision():
- history: AgentHistoryList = await agent.run(20)
+ agent = Agent(
+ task='call explain_screen all the time the user asks you questions e.g. about the page like bbox which you see are labels - your task is to explain it and get the next question',
+ llm=llm,
+ controller=controller,
+ browser=Browser(config=BrowserConfig(disable_security=True, headless=False)),
+ )
+ try:
+ history: AgentHistoryList = await agent.run(20)
+ finally:
+ # Make sure to close the browser
+ await agent.browser.close()