|
| 1 | +import os |
1 | 2 | import subprocess |
2 | 3 |
|
3 | 4 | import pytest |
4 | | -from playwright.async_api import Browser, Page, async_playwright |
5 | | -from reactpy.config import REACTPY_TESTING_DEFAULT_TIMEOUT |
| 5 | +from playwright.async_api import async_playwright |
6 | 6 | from reactpy.testing import BackendFixture, DisplayFixture |
7 | 7 |
|
8 | 8 | from .tooling import update_vscode_env |
9 | 9 |
|
| 10 | +GITHUB_ACTIONS = os.getenv("GITHUB_ACTIONS", "").lower() == "true" |
10 | 11 |
|
11 | | -@pytest.fixture(scope="session") |
12 | | -def anyio_backend(): |
13 | | - return "asyncio" |
14 | 12 |
|
15 | | - |
16 | | -def pytest_addoption(parser: pytest.Parser) -> None: |
| 13 | +def pytest_addoption(parser) -> None: |
17 | 14 | parser.addoption( |
18 | | - "--headed", |
19 | | - dest="headed", |
| 15 | + "--headless", |
| 16 | + dest="headless", |
20 | 17 | action="store_true", |
21 | | - help="Open a browser window when running web-based tests", |
| 18 | + help="Hide the browser window when running web-based tests", |
22 | 19 | ) |
23 | 20 |
|
24 | 21 |
|
25 | 22 | def pytest_sessionstart(session): |
26 | 23 | """Rebuild the project before running the tests to get the latest JavaScript""" |
27 | | - |
28 | 24 | # subprocess.run(["hatch", "build", "--clean"], check=True) |
29 | | - |
30 | 25 | update_vscode_env(".env") |
31 | 26 | subprocess.run(["playwright", "install", "chromium"], check=True) |
32 | 27 |
|
33 | 28 |
|
34 | 29 | @pytest.fixture(scope="session") |
35 | | -def display(server: BackendFixture, page: Page): |
36 | | - # async with DisplayFixture(server, page) as display: |
37 | | - # yield display |
38 | | - |
39 | | - return DisplayFixture(server, page) |
| 30 | +async def display(backend, browser): |
| 31 | + async with DisplayFixture(backend, browser) as display_fixture: |
| 32 | + display_fixture.page.set_default_timeout(10000) |
| 33 | + yield display_fixture |
40 | 34 |
|
41 | 35 |
|
42 | 36 | @pytest.fixture(scope="session") |
43 | | -async def server(): |
44 | | - async with BackendFixture() as server: |
45 | | - yield server |
| 37 | +async def backend(): |
| 38 | + async with BackendFixture() as backend_fixture: |
| 39 | + yield backend_fixture |
46 | 40 |
|
47 | 41 |
|
48 | 42 | @pytest.fixture(scope="session") |
49 | | -async def page(browser: Browser): |
50 | | - context = await browser.new_context(permissions=["clipboard-read", "clipboard-write"]) |
51 | | - pg = await context.new_page() |
52 | | - pg.set_default_timeout(REACTPY_TESTING_DEFAULT_TIMEOUT.current * 1000) |
53 | | - try: |
54 | | - yield pg |
55 | | - finally: |
56 | | - await pg.close() |
| 43 | +async def browser(pytestconfig): |
| 44 | + async with async_playwright() as pw: |
| 45 | + browser = await pw.chromium.launch(headless=True if GITHUB_ACTIONS else pytestconfig.getoption("headless")) |
| 46 | + context = await browser.new_context(permissions=["clipboard-read", "clipboard-write"]) |
| 47 | + yield context |
| 48 | + await context.close() |
| 49 | + await browser.close() |
57 | 50 |
|
58 | 51 |
|
59 | 52 | @pytest.fixture(scope="session") |
60 | | -async def browser(pytestconfig: pytest.Config): |
61 | | - async with async_playwright() as pw: |
62 | | - yield await pw.chromium.launch(headless=not bool(pytestconfig.option.headed)) |
| 53 | +def anyio_backend(): |
| 54 | + return "asyncio" |
0 commit comments