Skip to content

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

parallelize playwright tests using loop scope=session and pytest-xdist

Nick Sweeting8504bc4c7ba6
diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml
index 23b72f7f0..4df26acc3 100644
--- a/.github/workflows/test.yaml
+++ b/.github/workflows/test.yaml
@@ -63,7 +63,7 @@ jobs:
enable-cache: true
activate-environment: true
- - run: uv sync
+ - run: uv sync --dev
- name: Detect installed Playwright version
run: echo "PLAYWRIGHT_VERSION=$(uv pip list --format json | jq -r '.[] | select(.name == "playwright") | .version')" >> $GITHUB_ENV
@@ -78,4 +78,4 @@ jobs:
- run: playwright install chrome
- run: playwright install chromium
- - run: pytest tests/ci/${{ matrix.test_filename }}.py
+ - run: pytest --numprocesses auto -x tests/ci/${{ matrix.test_filename }}.py
diff --git a/bin/test.sh b/bin/test.sh
index 4d2c33c15..32f07f973 100755
--- a/bin/test.sh
+++ b/bin/test.sh
@@ -6,4 +6,4 @@
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
cd "$SCRIPT_DIR/.." || exit 1
-exec uv run pytest tests/ci
+exec uv run pytest -x --numprocesses auto tests/ci
diff --git a/pyproject.toml b/pyproject.toml
index d07366152..ebedac2be 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -116,8 +116,8 @@ include = [
[tool.pytest.ini_options]
asyncio_mode = "auto"
-asyncio_default_fixture_loop_scope = "module"
-asyncio_default_test_loop_scope = "module"
+asyncio_default_fixture_loop_scope = "session"
+asyncio_default_test_loop_scope = "session"
markers = [
"slow: marks tests as slow (deselect with `-m 'not slow'`)",
"integration: marks tests as integration tests",
@@ -147,7 +147,7 @@ dev-dependencies = [
"tokencost>=0.1.16",
"build>=1.2.2",
"pytest>=8.3.5",
- "pytest-asyncio>=0.24.0",
+ "pytest-asyncio>=0.26.0",
"pytest-httpserver>=1.0.8",
"fastapi>=0.115.8",
"inngest>=0.4.19",
@@ -158,4 +158,6 @@ dev-dependencies = [
"codespell>=2.4.1",
"pyright>=1.1.399",
"ty>=0.0.1a1",
+ "pytest-playwright-asyncio>=0.7.0",
+ "pytest-xdist>=3.7.0",
]
diff --git a/tests/ci/test_browser_session_tab_management.py b/tests/ci/test_browser_session_tab_management.py
index 0549ccad0..741e85117 100644
--- a/tests/ci/test_browser_session_tab_management.py
+++ b/tests/ci/test_browser_session_tab_management.py
@@ -425,6 +425,7 @@ class TestTabManagement:
assert 'closed' in results
await browser_session.stop()
+ await asyncio.sleep(0.5)
async def test_browser_context_state_after_error(self):
"""Test browser context state remains consistent after errors"""
@@ -453,4 +454,5 @@ class TestTabManagement:
assert browser_session.browser_context != original_context
assert browser_session.initialized is True
- await browser_session.kill()
+ await browser_session.stop()
+ await asyncio.sleep(0.5)