|
4 | 4 |
|
5 | 5 | from pathlib import PurePath
|
6 | 6 |
|
| 7 | +import pytest |
| 8 | +from playwright.sync_api import BrowserContext, Page |
| 9 | + |
7 | 10 | from shiny.pytest import ScopeName as ScopeName
|
8 | 11 | from shiny.pytest import create_app_fixture
|
9 | 12 |
|
|
19 | 22 | here_root = here.parent.parent
|
20 | 23 |
|
21 | 24 |
|
| 25 | +# Make a single page fixture that can be used by all tests |
| 26 | +@pytest.fixture(scope="session") |
| 27 | +# By using a single page, the browser is only launched once and all tests run in the same tab / page. |
| 28 | +def session_page(browser: BrowserContext) -> Page: |
| 29 | + """ |
| 30 | + Create a new page within the given browser context. |
| 31 | + Parameters: |
| 32 | + browser (BrowserContext): The browser context in which to create the new page. |
| 33 | + Returns: |
| 34 | + Page: The newly created page. |
| 35 | + """ |
| 36 | + return browser.new_page() |
| 37 | + |
| 38 | + |
| 39 | +@pytest.fixture(scope="function") |
| 40 | +# By going to `about:blank`, we _reset_ the page to a known state before each test. |
| 41 | +# It is not perfect, but it is faster than making a new page for each test. |
| 42 | +# This must be done before each test |
| 43 | +def page(session_page: Page) -> Page: |
| 44 | + """ |
| 45 | + Reset the given page to a known state before each test. |
| 46 | + The page is built on the session_page, which is maintained over the full session. |
| 47 | + The page will visit "about:blank" to reset between apps. |
| 48 | + The default viewport size is set to 1920 x 1080 (1080p) for each test function. |
| 49 | + Parameters: |
| 50 | + session_page (Page): The page to reset. |
| 51 | + """ |
| 52 | + session_page.goto("about:blank") |
| 53 | + # Reset screen size to 1080p |
| 54 | + session_page.set_viewport_size({"width": 1920, "height": 1080}) |
| 55 | + return session_page |
| 56 | + |
| 57 | + |
22 | 58 | def create_example_fixture(
|
23 | 59 | example_name: str,
|
24 | 60 | example_file: str = "app.py",
|
|
0 commit comments