Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion tests/integration/browsers/test_stagehand_browser_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

_PATCH_MODULE = 'crawlee.browsers._stagehand_browser_plugin'

pytestmark = pytest.mark.usefixtures('stagehand_binary')


@pytest.fixture
def stagehand_client_mock() -> MagicMock:
Expand All @@ -24,7 +26,7 @@ def stagehand_client_mock() -> MagicMock:


@pytest.fixture
async def plugin(stagehand_binary: None) -> AsyncGenerator[StagehandBrowserPlugin, None]: # noqa: ARG001
async def plugin() -> AsyncGenerator[StagehandBrowserPlugin, None]:
async with StagehandBrowserPlugin() as plugin:
yield plugin

Expand Down
17 changes: 11 additions & 6 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import pytest
from filelock import FileLock
from proxy import Proxy
from stagehand._custom.sea_binary import resolve_binary_path
from stagehand import __version__ as stagehand_version
from stagehand._custom.sea_binary import _cache_dir, resolve_binary_path
from uvicorn.config import Config

from crawlee.proxy_configuration import ProxyInfo
Expand All @@ -29,12 +30,16 @@


@pytest.fixture(scope='session')
def stagehand_binary(tmp_path_factory: pytest.TempPathFactory) -> None:
"""Populate Stagehand's per-user binary cache once, serialized across xdist workers."""
def stagehand_binary() -> None:
"""Populate Stagehand's per-user binary cache once, serialized across xdist workers and pytest sessions."""
# Stagehand copies its server binary into the cache on first use without a lock, so concurrent first uses race on
# the copy (`WinError 32` on Windows, `FileNotFoundError` elsewhere).
with FileLock(tmp_path_factory.getbasetemp().parent / 'stagehand_binary.lock'):
resolve_binary_path()
# the copy (`WinError 32` on Windows, `FileNotFoundError` elsewhere). The lock sits next to the cache, so it also
# covers concurrent pytest sessions of the same user. The version matches the one `AsyncStagehand` resolves, so a
# set `STAGEHAND_VERSION` can't make the fixture fill a different cache directory.
cache_dir = _cache_dir()
cache_dir.mkdir(parents=True, exist_ok=True)
with FileLock(cache_dir / 'populate.lock'):
resolve_binary_path(version=stagehand_version)


def _proxy_info(port: int) -> ProxyInfo:
Expand Down
Loading