Skip to content

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

fix tests by using temp dirs and headless for all tests

Nick Sweeting02b4f2c5ded3
diff --git a/tests/ci/test_browser_session_allowed_domains.py b/tests/ci/test_browser_session_allowed_domains.py
index a157243eb..080acd3b3 100644
--- a/tests/ci/test_browser_session_allowed_domains.py
+++ b/tests/ci/test_browser_session_allowed_domains.py
@@ -7,7 +7,7 @@ class TestUrlAllowlistSecurity:
def test_authentication_bypass_prevention(self):
"""Test that the URL allowlist cannot be bypassed using authentication credentials."""
# Create a context config with a sample allowed domain
- browser_profile = BrowserProfile(allowed_domains=['example.com'])
+ browser_profile = BrowserProfile(allowed_domains=['example.com'], headless=True, user_data_dir=None)
browser_session = BrowserSession(browser_profile=browser_profile)
# Security vulnerability test cases
@@ -23,7 +23,7 @@ class TestUrlAllowlistSecurity:
def test_glob_pattern_matching(self):
"""Test that glob patterns in allowed_domains work correctly."""
# Test *.example.com pattern (should match subdomains and main domain)
- browser_profile = BrowserProfile(allowed_domains=['*.example.com'])
+ browser_profile = BrowserProfile(allowed_domains=['*.example.com'], headless=True, user_data_dir=None)
browser_session = BrowserSession(browser_profile=browser_profile)
# Should match subdomains
@@ -39,7 +39,9 @@ class TestUrlAllowlistSecurity:
# Test more complex glob patterns
browser_profile = BrowserProfile(
- allowed_domains=['*.google.com', 'https://wiki.org', 'https://good.com', 'chrome://version', 'brave://*']
+ allowed_domains=['*.google.com', 'https://wiki.org', 'https://good.com', 'chrome://version', 'brave://*'],
+ headless=True,
+ user_data_dir=None,
)
browser_session = BrowserSession(browser_profile=browser_profile)
@@ -75,7 +77,7 @@ class TestUrlAllowlistSecurity:
def test_glob_pattern_edge_cases(self):
"""Test edge cases for glob pattern matching to ensure proper behavior."""
# Test with domains containing glob pattern in the middle
- browser_profile = BrowserProfile(allowed_domains=['*.google.com', 'https://wiki.org'])
+ browser_profile = BrowserProfile(allowed_domains=['*.google.com', 'https://wiki.org'], headless=True, user_data_dir=None)
browser_session = BrowserSession(browser_profile=browser_profile)
# Verify that 'wiki*' pattern doesn't match domains that merely contain 'wiki' in the middle
@@ -87,7 +89,7 @@ class TestUrlAllowlistSecurity:
assert browser_session._is_url_allowed('https://mygoogle.company.com') is False
# Create context with potentially risky glob pattern that demonstrates security concerns
- browser_profile = BrowserProfile(allowed_domains=['*.google.com', '*.google.co.uk'])
+ browser_profile = BrowserProfile(allowed_domains=['*.google.com', '*.google.co.uk'], headless=True, user_data_dir=None)
browser_session = BrowserSession(browser_profile=browser_profile)
# Should match legitimate Google domains
diff --git a/tests/ci/test_browser_session_downloads.py b/tests/ci/test_browser_session_downloads.py
index fb6f8d747..79059538e 100644
--- a/tests/ci/test_browser_session_downloads.py
+++ b/tests/ci/test_browser_session_downloads.py
@@ -5,7 +5,7 @@ import time
import pytest
-from browser_use.browser import BrowserProfile, BrowserSession
+from browser_use.browser import BrowserSession
@pytest.fixture
@@ -38,11 +38,9 @@ async def test_download_detection_timing(test_server, tmp_path):
# Test 1: With downloads_dir set (default behavior)
browser_with_downloads = BrowserSession(
- browser_profile=BrowserProfile(
- headless=True,
- downloads_dir=str(tmp_path / 'downloads'),
- user_data_dir=None,
- )
+ headless=True,
+ downloads_dir=str(tmp_path / 'downloads'),
+ user_data_dir=None,
)
await browser_with_downloads.start()
@@ -75,11 +73,9 @@ async def test_download_detection_timing(test_server, tmp_path):
# Test 2: With downloads_dir set to empty string (disables download detection)
browser_no_downloads = BrowserSession(
- browser_profile=BrowserProfile(
- headless=True,
- downloads_dir=None,
- user_data_dir=None,
- )
+ headless=True,
+ downloads_dir=None,
+ user_data_dir=None,
)
await browser_no_downloads.start()
@@ -130,11 +126,9 @@ async def test_actual_download_detection(test_server, tmp_path):
downloads_dir.mkdir()
browser_session = BrowserSession(
- browser_profile=BrowserProfile(
- headless=True,
- downloads_dir=str(downloads_dir),
- user_data_dir=None,
- )
+ headless=True,
+ downloads_dir=str(downloads_dir),
+ user_data_dir=None,
)
await browser_session.start()
diff --git a/tests/ci/test_browser_session_element_cache.py b/tests/ci/test_browser_session_element_cache.py
index f96017e89..d0201f958 100644
--- a/tests/ci/test_browser_session_element_cache.py
+++ b/tests/ci/test_browser_session_element_cache.py
@@ -7,7 +7,7 @@ import os
import pytest
-from browser_use.browser import BrowserProfile, BrowserSession
+from browser_use.browser import BrowserSession
from browser_use.controller.service import Controller
@@ -62,11 +62,9 @@ def httpserver(make_httpserver):
async def browser_session():
"""Create a real browser session for testing."""
session = BrowserSession(
- browser_profile=BrowserProfile(
- executable_path=os.getenv('BROWSER_PATH'),
- user_data_dir=None, # Use temporary profile
- headless=True,
- )
+ executable_path=os.getenv('BROWSER_PATH'),
+ user_data_dir=None, # Use temporary profile
+ headless=True,
)
async with session:
yield session
diff --git a/tests/ci/test_browser_session_tab_management.py b/tests/ci/test_browser_session_tab_management.py
index 3fa5649c4..e484be87b 100644
--- a/tests/ci/test_browser_session_tab_management.py
+++ b/tests/ci/test_browser_session_tab_management.py
@@ -55,6 +55,7 @@ class TestTabManagement:
browser_session = BrowserSession(
browser_profile=browser_profile,
user_data_dir=None,
+ headless=True,
)
await browser_session.start()
diff --git a/tests/ci/test_browser_session_viewport_and_proxy.py b/tests/ci/test_browser_session_viewport_and_proxy.py
index e7fad6a4a..a757570c9 100644
--- a/tests/ci/test_browser_session_viewport_and_proxy.py
+++ b/tests/ci/test_browser_session_viewport_and_proxy.py
@@ -33,6 +33,7 @@ async def test_window_size_with_real_browser():
"""
# Create browser profile with headless mode and specific dimensions
browser_profile = BrowserProfile(
+ user_data_dir=None,
headless=True, # window size gets converted to viewport size in headless mode
window_size={'width': 999, 'height': 888},
maximum_wait_page_load_time=2.0,
@@ -125,6 +126,7 @@ async def test_proxy_with_real_browser():
browser_profile = BrowserProfile(
headless=True,
proxy=proxy_settings,
+ user_data_dir=None,
)
# Create browser session
diff --git a/tests/ci/test_controller_action_parameter_injection.py b/tests/ci/test_controller_action_parameter_injection.py
index 9f0af6c8f..552f313f0 100644
--- a/tests/ci/test_controller_action_parameter_injection.py
+++ b/tests/ci/test_controller_action_parameter_injection.py
@@ -72,7 +72,7 @@ class TestBrowserContext:
the allowed domains configuration.
"""
# Scenario 1: allowed_domains is None, any URL should be allowed.
- config1 = BrowserProfile(allowed_domains=None)
+ config1 = BrowserProfile(allowed_domains=None, headless=True, user_data_dir=None)
context1 = BrowserSession(browser_profile=config1)
assert context1._is_url_allowed('http://anydomain.com') is True
assert context1._is_url_allowed('https://anotherdomain.org/path') is True
@@ -80,7 +80,7 @@ class TestBrowserContext:
# Scenario 2: allowed_domains is provided.
# Note: match_url_with_domain_pattern defaults to https:// scheme when none is specified
allowed = ['https://example.com', 'http://example.com', 'http://*.mysite.org', 'https://*.mysite.org']
- config2 = BrowserProfile(allowed_domains=allowed)
+ config2 = BrowserProfile(allowed_domains=allowed, headless=True, user_data_dir=None)
context2 = BrowserSession(browser_profile=config2)
# URL exactly matching