diff --git a/tests/integration/browsers/test_stagehand_browser_plugin.py b/tests/integration/browsers/test_stagehand_browser_plugin.py index e63d9f29e0..23592a26b5 100644 --- a/tests/integration/browsers/test_stagehand_browser_plugin.py +++ b/tests/integration/browsers/test_stagehand_browser_plugin.py @@ -14,6 +14,8 @@ _PATCH_MODULE = 'crawlee.browsers._stagehand_browser_plugin' +pytestmark = pytest.mark.usefixtures('stagehand_binary') + @pytest.fixture def stagehand_client_mock() -> MagicMock: @@ -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 diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index ca252f0917..d66ed82eae 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -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 @@ -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: