Skip to content

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

Merge branch 'main' into pre-commit_run_pre-commit-hooks_--all-files

Nick Sweeting588a9f9f50e7
diff --cc tests/test_browser.py
index da5036d9b,d8c5a000b..b3e41b175
--- a/tests/test_browser.py
+++ b/tests/test_browser.py
@@@ -1,16 -1,19 +1,19 @@@
import asyncio
+ import subprocess
+
import pytest
import requests
- import subprocess
+ from playwright._impl._api_structures import ProxySettings
+
from browser_use.browser.browser import Browser, BrowserConfig
from browser_use.browser.context import BrowserContext, BrowserContextConfig
- from playwright._impl._api_structures import ProxySettings
+
@pytest.mark.asyncio
--async def test_standard_browser_launch(monkeypatch):
++async def test_builtin_browser_launch(monkeypatch):
"""
Test that the standard browser is launched correctly:
- When no remote (cdp or wss) or chrome instance is provided, the Browser class uses _setup_standard_browser.
+ When no remote (cdp or wss) or chrome instance is provided, the Browser class uses _setup_builtin_browser.
This test monkeypatches async_playwright to return dummy objects, and asserts that get_playwright_browser returns the expected DummyBrowser.
"""
class DummyBrowser:
@@@ -30,8 -33,8 +33,9 @@@
config = BrowserConfig(headless=True, disable_security=False, extra_browser_args=["--test"])
browser_obj = Browser(config=config)
result_browser = await browser_obj.get_playwright_browser()
- assert isinstance(result_browser, DummyBrowser), "Expected DummyBrowser from _setup_standard_browser"
+ assert isinstance(result_browser, DummyBrowser), "Expected DummyBrowser from _setup_builtin_browser"
await browser_obj.close()
++
@pytest.mark.asyncio
async def test_cdp_browser_launch(monkeypatch):
"""
@@@ -58,11 -61,11 +62,12 @@@
result_browser = await browser_obj.get_playwright_browser()
assert isinstance(result_browser, DummyBrowser), "Expected DummyBrowser from _setup_cdp"
await browser_obj.close()
++
@pytest.mark.asyncio
async def test_wss_browser_launch(monkeypatch):
"""
Test that when a WSS URL is provided in the configuration,
-- the Browser uses _setup_wss and returns the expected DummyBrowser.
++ the Browser uses setup_wss and returns the expected DummyBrowser.
"""
class DummyBrowser:
pass
@@@ -84,11 -87,11 +89,12 @@@
result_browser = await browser_obj.get_playwright_browser()
assert isinstance(result_browser, DummyBrowser), "Expected DummyBrowser from _setup_wss"
await browser_obj.close()
++
@pytest.mark.asyncio
--async def test_chrome_instance_browser_launch(monkeypatch):
++async def test_user_provided_browser_launch(monkeypatch):
"""
- Test that when a chrome instance path is provided the Browser class uses
- _setup_browser_with_instance branch and returns the expected DummyBrowser object
- Test that when a chrome instance path is provided the Browser class uses
++ Test that when a browser_binary_path is provided the Browser class uses
+ _setup_user_provided_browser branch and returns the expected DummyBrowser object
by reusing an existing Chrome instance.
"""
# Dummy response for requests.get when checking chrome debugging endpoint.
@@@ -114,19 -117,19 +120,20 @@@
async def start(self):
return DummyPlaywright()
monkeypatch.setattr("browser_use.browser.browser.async_playwright", lambda: DummyAsyncPlaywrightContext())
- config = BrowserConfig(browser_instance_path="dummy/chrome", extra_browser_args=["--dummy-arg"])
+ config = BrowserConfig(browser_binary_path="dummy/chrome", extra_browser_args=["--dummy-arg"])
browser_obj = Browser(config=config)
result_browser = await browser_obj.get_playwright_browser()
- assert isinstance(result_browser, DummyBrowser), "Expected DummyBrowser from _setup_browser_with_instance"
+ assert isinstance(result_browser, DummyBrowser), "Expected DummyBrowser from _setup_user_provided_browser"
await browser_obj.close()
++
@pytest.mark.asyncio
--async def test_standard_browser_disable_security_args(monkeypatch):
++async def test_builtin_browser_disable_security_args(monkeypatch):
"""
Test that the standard browser launch includes disable-security arguments when disable_security is True.
- This verifies that _setup_standard_browser correctly appends the security disabling arguments along with
+ This verifies that _setup_builtin_browser correctly appends the security disabling arguments along with
the base arguments and any extra arguments provided.
"""
- # These are the base arguments defined in _setup_standard_browser.
+ # These are the base arguments defined in _setup_builtin_browser.
base_args = [
'--no-sandbox',
'--disable-blink-features=AutomationControlled',
@@@ -172,8 -175,8 +179,9 @@@
config = BrowserConfig(headless=True, disable_security=True, extra_browser_args=extra_args)
browser_obj = Browser(config=config)
result_browser = await browser_obj.get_playwright_browser()
- assert isinstance(result_browser, DummyBrowser), "Expected DummyBrowser from _setup_standard_browser with disable_security active"
+ assert isinstance(result_browser, DummyBrowser), "Expected DummyBrowser from _setup_builtin_browser with disable_security active"
await browser_obj.close()
++
@pytest.mark.asyncio
async def test_new_context_creation():
"""
@@@ -188,11 -191,11 +196,12 @@@
assert context.browser is browser_obj, "Expected the context's browser attribute to be the Browser instance"
assert context.config == custom_context_config, "Expected the context's config attribute to be the provided config"
await browser_obj.close()
++
@pytest.mark.asyncio
--async def test_chrome_instance_browser_launch_failure(monkeypatch):
++async def test_user_provided_browser_launch_failure(monkeypatch):
"""
Test that when a Chrome instance cannot be started or connected to,
- the Browser._setup_browser_with_instance branch eventually raises a RuntimeError.
+ the Browser._setup_user_provided_browser branch eventually raises a RuntimeError.
We simulate failure by:
- Forcing requests.get to always raise a ConnectionError (so no existing instance is found).
- Monkeypatching subprocess.Popen to do nothing.
@@@ -223,6 -226,6 +232,7 @@@
with pytest.raises(RuntimeError, match="To start chrome in Debug mode"):
await browser_obj.get_playwright_browser()
await browser_obj.close()
++
@pytest.mark.asyncio
async def test_get_playwright_browser_caching(monkeypatch):
"""
@@@ -250,6 -253,6 +260,7 @@@
second_browser = await browser_obj.get_playwright_browser()
assert first_browser is second_browser, "Expected the browser to be cached and reused across calls."
await browser_obj.close()
++
@pytest.mark.asyncio
async def test_close_error_handling(monkeypatch):
"""
@@@ -270,6 -273,6 +281,7 @@@
await browser_obj.close()
assert browser_obj.playwright_browser is None, "Expected playwright_browser to be None after close"
assert browser_obj.playwright is None, "Expected playwright to be None after close"
++
@pytest.mark.asyncio
async def test_standard_browser_launch_with_proxy(monkeypatch):
"""
@@@ -302,5 -305,5 +314,5 @@@
browser_obj = Browser(config=config)
# Call get_playwright_browser and verify that the returned browser is as expected.
result_browser = await browser_obj.get_playwright_browser()
- assert isinstance(result_browser, DummyBrowser), "Expected DummyBrowser from _setup_standard_browser with proxy provided"
+ assert isinstance(result_browser, DummyBrowser), "Expected DummyBrowser from _setup_builtin_browser with proxy provided"
- await browser_obj.close()
+ await browser_obj.close()