MCPcopy Index your code
hub / github.com/reactive-python/reactpy / DisplayFixture

Class DisplayFixture

src/py/reactpy/reactpy/testing/display.py:20–91  ·  view source on GitHub ↗

A fixture for running web-based tests using ``playwright``

Source from the content-addressed store, hash-verified

18
19
20class DisplayFixture:
21 """A fixture for running web-based tests using ``playwright``"""
22
23 _exit_stack: AsyncExitStack
24
25 def __init__(
26 self,
27 backend: BackendFixture | None = None,
28 driver: Browser | BrowserContext | Page | None = None,
29 url_prefix: str = "",
30 ) -> None:
31 if backend is not None:
32 self.backend = backend
33 if driver is not None:
34 if isinstance(driver, Page):
35 self.page = driver
36 else:
37 self._browser = driver
38 self.url_prefix = url_prefix
39
40 async def show(
41 self,
42 component: RootComponentConstructor,
43 ) -> None:
44 self.backend.mount(component)
45 await self.goto("/")
46 await self.root_element() # check that root element is attached
47
48 async def goto(
49 self, path: str, query: Any | None = None, add_url_prefix: bool = True
50 ) -> None:
51 await self.page.goto(
52 self.backend.url(
53 f"{self.url_prefix}{path}" if add_url_prefix else path, query
54 )
55 )
56
57 async def root_element(self) -> ElementHandle:
58 element = await self.page.wait_for_selector("#app", state="attached")
59 if element is None: # nocov
60 msg = "Root element not attached"
61 raise RuntimeError(msg)
62 return element
63
64 async def __aenter__(self) -> DisplayFixture:
65 es = self._exit_stack = AsyncExitStack()
66
67 browser: Browser | BrowserContext
68 if not hasattr(self, "page"):
69 if not hasattr(self, "_browser"):
70 pw = await es.enter_async_context(async_playwright())
71 browser = await pw.chromium.launch()
72 else:
73 browser = self._browser
74 self.page = await browser.new_page()
75
76 self.page.set_default_timeout(REACTPY_TESTING_DEFAULT_TIMEOUT.current * 1000)
77

Callers 5

test_automatic_reconnectFunction · 0.90
displayFunction · 0.90
displayFunction · 0.90
test_customized_headFunction · 0.90
test_module_from_urlFunction · 0.90

Calls

no outgoing calls

Tested by 5

test_automatic_reconnectFunction · 0.72
displayFunction · 0.72
displayFunction · 0.72
test_customized_headFunction · 0.72
test_module_from_urlFunction · 0.72