|
9 | 9 | pure unit/contract tests without requiring Playwright. |
10 | 10 |
|
11 | 11 | IMPORTANT: |
12 | | -- These stubs are only active during pytest runs (via conftest import order). |
| 12 | +- These stubs are ONLY loaded when Playwright is NOT installed. |
| 13 | +- When Playwright IS installed, real Playwright is used for all tests. |
13 | 14 | - Integration/E2E tests that need real Playwright should install Playwright and will |
14 | 15 | typically run in separate environments. |
15 | 16 | """ |
|
20 | 21 | import types |
21 | 22 |
|
22 | 23 |
|
23 | | -def _ensure_module(name: str) -> types.ModuleType: |
24 | | - if name in sys.modules: |
25 | | - return sys.modules[name] |
26 | | - mod = types.ModuleType(name) |
27 | | - sys.modules[name] = mod |
28 | | - return mod |
| 24 | +def _ensure_playwright_stubs() -> None: |
| 25 | + """ |
| 26 | + Provide minimal `playwright.*` stubs so the SDK can be imported in environments |
| 27 | + where Playwright isn't installed (e.g., constrained CI/sandbox). |
29 | 28 |
|
| 29 | + This is only intended to support pure unit/contract tests that don't actually |
| 30 | + launch browsers. |
| 31 | + """ |
30 | 32 |
|
31 | | -# Create top-level playwright module and submodules |
32 | | -playwright_mod = _ensure_module("playwright") |
33 | | -async_api_mod = _ensure_module("playwright.async_api") |
34 | | -sync_api_mod = _ensure_module("playwright.sync_api") |
| 33 | + def _ensure_module(name: str) -> types.ModuleType: |
| 34 | + if name in sys.modules: |
| 35 | + return sys.modules[name] |
| 36 | + mod = types.ModuleType(name) |
| 37 | + sys.modules[name] = mod |
| 38 | + return mod |
35 | 39 |
|
| 40 | + # Create top-level playwright module and submodules |
| 41 | + playwright_mod = _ensure_module("playwright") |
| 42 | + async_api_mod = _ensure_module("playwright.async_api") |
| 43 | + sync_api_mod = _ensure_module("playwright.sync_api") |
36 | 44 |
|
37 | | -class _Dummy: |
38 | | - """Placeholder type used for Playwright classes in unit tests.""" |
| 45 | + class _Dummy: |
| 46 | + """Placeholder type used for Playwright classes in unit tests.""" |
39 | 47 |
|
| 48 | + # Minimal symbols imported by `sentience.browser` |
| 49 | + async_api_mod.BrowserContext = _Dummy |
| 50 | + async_api_mod.Browser = _Dummy |
| 51 | + async_api_mod.Page = _Dummy |
| 52 | + async_api_mod.Playwright = _Dummy |
| 53 | + async_api_mod.PlaywrightContextManager = _Dummy |
40 | 54 |
|
41 | | -# Minimal symbols imported by `sentience.browser` |
42 | | -async_api_mod.BrowserContext = _Dummy |
43 | | -async_api_mod.Browser = _Dummy |
44 | | -async_api_mod.Page = _Dummy |
45 | | -async_api_mod.Playwright = _Dummy |
46 | | -async_api_mod.PlaywrightContextManager = _Dummy |
| 55 | + async def _async_playwright(): |
| 56 | + raise RuntimeError("Playwright is not available in this unit-test environment.") |
47 | 57 |
|
| 58 | + async_api_mod.async_playwright = _async_playwright |
48 | 59 |
|
49 | | -async def _async_playwright(): |
50 | | - raise RuntimeError("Playwright is not available in this unit-test environment.") |
| 60 | + sync_api_mod.BrowserContext = _Dummy |
| 61 | + sync_api_mod.Browser = _Dummy |
| 62 | + sync_api_mod.Page = _Dummy |
| 63 | + sync_api_mod.Playwright = _Dummy |
| 64 | + sync_api_mod.PlaywrightContextManager = _Dummy |
51 | 65 |
|
| 66 | + def _sync_playwright(): |
| 67 | + raise RuntimeError("Playwright is not available in this unit-test environment.") |
52 | 68 |
|
53 | | -async_api_mod.async_playwright = _async_playwright |
| 69 | + sync_api_mod.sync_playwright = _sync_playwright |
54 | 70 |
|
55 | | -sync_api_mod.BrowserContext = _Dummy |
56 | | -sync_api_mod.Browser = _Dummy |
57 | | -sync_api_mod.Page = _Dummy |
58 | | -sync_api_mod.Playwright = _Dummy |
59 | | -sync_api_mod.PlaywrightContextManager = _Dummy |
| 71 | + # Expose submodules on the top-level module for completeness |
| 72 | + playwright_mod.async_api = async_api_mod |
| 73 | + playwright_mod.sync_api = sync_api_mod |
60 | 74 |
|
61 | 75 |
|
62 | | -def _sync_playwright(): |
63 | | - raise RuntimeError("Playwright is not available in this unit-test environment.") |
64 | | - |
65 | | - |
66 | | -sync_api_mod.sync_playwright = _sync_playwright |
67 | | - |
68 | | - |
69 | | -# Expose submodules on the top-level module for completeness |
70 | | -playwright_mod.async_api = async_api_mod |
71 | | -playwright_mod.sync_api = sync_api_mod |
| 76 | +# Only load stubs if Playwright is NOT available |
| 77 | +# This prevents overwriting real Playwright when it IS installed |
| 78 | +try: |
| 79 | + import playwright # noqa: F401 |
| 80 | +except ImportError: |
| 81 | + _ensure_playwright_stubs() |
0 commit comments