Skip to content

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

fix tests

Nick Sweetingf94e86f3a967
diff --git a/browser_use/browser/session.py b/browser_use/browser/session.py
index 8191c8f56..bc532b854 100644
--- a/browser_use/browser/session.py
+++ b/browser_use/browser/session.py
@@ -413,9 +413,10 @@ class BrowserSession(BaseModel):
except TimeoutError:
self.logger.warning('⏱️ Timeout while closing browser/context, has it become unresponsive?')
except Exception as e:
- self.logger.warning(
- f'❌ Error closing playwright browser_context={self.browser_context}: {type(e).__name__}: {e}'
- )
+ if 'browser has been closed' not in str(e):
+ self.logger.warning(
+ f'❌ Error closing playwright browser_context={self.browser_context}: {type(e).__name__}: {e}'
+ )
finally:
# Always clear references to ensure a fresh start next time
self.browser_context = None
@@ -1282,8 +1283,10 @@ class BrowserSession(BaseModel):
# Check if the browser_context itself is closed/unusable
try:
- _ = self.browser_context.pages
- return True
+ # TODO: figure out a better synchronous test for whether browser_context is usable
+ # this is a hacky workaround for the fact that playwright's browser_context has no is_connected() method
+ # and browser_context.browser is None when we launch with a persistent context (basically always)
+ return bool(self.browser_context.pages)
except Exception:
return False
diff --git a/tests/ci/test_browser_session_tab_management.py b/tests/ci/test_browser_session_tab_management.py
index fd4b258f5..2104174c0 100644
--- a/tests/ci/test_browser_session_tab_management.py
+++ b/tests/ci/test_browser_session_tab_management.py
@@ -364,10 +364,10 @@ class TestTabManagement:
return f'connected: {connected}'
# Run all operations concurrently
- results = await asyncio.gather(close_context(), access_pages(), check_connection(), return_exceptions=True)
+ results = list(await asyncio.gather(close_context(), access_pages(), check_connection(), return_exceptions=True))
# All operations should complete without crashes
- assert all(not isinstance(r, Exception) for r in results)
+ assert results and all(not isinstance(r, Exception) for r in results)
assert 'closed' in results
await browser_session.kill()