testing.py 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  1. """reflex.testing - tools for testing reflex apps."""
  2. from __future__ import annotations
  3. import asyncio
  4. import contextlib
  5. import dataclasses
  6. import functools
  7. import inspect
  8. import os
  9. import platform
  10. import re
  11. import signal
  12. import socket
  13. import socketserver
  14. import subprocess
  15. import textwrap
  16. import threading
  17. import time
  18. import types
  19. from http.server import SimpleHTTPRequestHandler
  20. from pathlib import Path
  21. from typing import (
  22. TYPE_CHECKING,
  23. Any,
  24. AsyncIterator,
  25. Callable,
  26. Coroutine,
  27. Optional,
  28. Sequence,
  29. Type,
  30. TypeVar,
  31. )
  32. import psutil
  33. import uvicorn
  34. import reflex
  35. import reflex.reflex
  36. import reflex.utils.build
  37. import reflex.utils.exec
  38. import reflex.utils.format
  39. import reflex.utils.prerequisites
  40. import reflex.utils.processes
  41. from reflex.components.component import CustomComponent
  42. from reflex.config import environment
  43. from reflex.state import (
  44. BaseState,
  45. StateManager,
  46. StateManagerDisk,
  47. StateManagerMemory,
  48. StateManagerRedis,
  49. reload_state_module,
  50. )
  51. from reflex.utils import console
  52. try:
  53. from selenium import webdriver
  54. from selenium.webdriver.remote.webdriver import WebDriver
  55. if TYPE_CHECKING:
  56. from selenium.webdriver.common.options import ArgOptions
  57. from selenium.webdriver.remote.webelement import WebElement
  58. has_selenium = True
  59. except ImportError:
  60. has_selenium = False
  61. # The timeout (minutes) to check for the port.
  62. DEFAULT_TIMEOUT = 15
  63. POLL_INTERVAL = 0.25
  64. FRONTEND_POPEN_ARGS = {}
  65. T = TypeVar("T")
  66. TimeoutType = int | float | None
  67. if platform.system() == "Windows":
  68. FRONTEND_POPEN_ARGS["creationflags"] = subprocess.CREATE_NEW_PROCESS_GROUP # pyright: ignore [reportAttributeAccessIssue]
  69. FRONTEND_POPEN_ARGS["shell"] = True
  70. else:
  71. FRONTEND_POPEN_ARGS["start_new_session"] = True
  72. # borrowed from py3.11
  73. class chdir(contextlib.AbstractContextManager): # noqa: N801
  74. """Non thread-safe context manager to change the current working directory."""
  75. def __init__(self, path: str | Path):
  76. """Prepare contextmanager.
  77. Args:
  78. path: the path to change to
  79. """
  80. self.path = path
  81. self._old_cwd = []
  82. def __enter__(self):
  83. """Save current directory and perform chdir."""
  84. self._old_cwd.append(Path.cwd())
  85. os.chdir(self.path)
  86. def __exit__(self, *excinfo):
  87. """Change back to previous directory on stack.
  88. Args:
  89. excinfo: sys.exc_info captured in the context block
  90. """
  91. os.chdir(self._old_cwd.pop())
  92. @dataclasses.dataclass
  93. class AppHarness:
  94. """AppHarness executes a reflex app in-process for testing."""
  95. app_name: str
  96. app_source: (
  97. Callable[[], None] | types.ModuleType | str | functools.partial[Any] | None
  98. )
  99. app_path: Path
  100. app_module_path: Path
  101. app_module: types.ModuleType | None = None
  102. app_instance: reflex.App | None = None
  103. frontend_process: subprocess.Popen | None = None
  104. frontend_url: str | None = None
  105. frontend_output_thread: threading.Thread | None = None
  106. backend_thread: threading.Thread | None = None
  107. backend: uvicorn.Server | None = None
  108. state_manager: StateManager | None = None
  109. _frontends: list["WebDriver"] = dataclasses.field(default_factory=list)
  110. _decorated_pages: list = dataclasses.field(default_factory=list)
  111. @classmethod
  112. def create(
  113. cls,
  114. root: Path,
  115. app_source: (
  116. Callable[[], None] | types.ModuleType | str | functools.partial[Any] | None
  117. ) = None,
  118. app_name: str | None = None,
  119. ) -> "AppHarness":
  120. """Create an AppHarness instance at root.
  121. Args:
  122. root: the directory that will contain the app under test.
  123. app_source: if specified, the source code from this function or module is used
  124. as the main module for the app. It may also be the raw source code text, as a str.
  125. If unspecified, then root must already contain a working reflex app and will be used directly.
  126. app_name: provide the name of the app, otherwise will be derived from app_source or root.
  127. Raises:
  128. ValueError: when app_source is a string and app_name is not provided.
  129. Returns:
  130. AppHarness instance
  131. """
  132. if app_name is None:
  133. if app_source is None:
  134. app_name = root.name
  135. elif isinstance(app_source, functools.partial):
  136. keywords = app_source.keywords
  137. slug_suffix = "_".join([str(v) for v in keywords.values()])
  138. func_name = app_source.func.__name__
  139. app_name = f"{func_name}_{slug_suffix}"
  140. app_name = re.sub(r"[^a-zA-Z0-9_]", "_", app_name)
  141. elif isinstance(app_source, str):
  142. raise ValueError(
  143. "app_name must be provided when app_source is a string."
  144. )
  145. else:
  146. app_name = app_source.__name__
  147. app_name = app_name.lower()
  148. while "__" in app_name:
  149. app_name = app_name.replace("__", "_")
  150. return cls(
  151. app_name=app_name,
  152. app_source=app_source,
  153. app_path=root,
  154. app_module_path=root / app_name / f"{app_name}.py",
  155. )
  156. def get_state_name(self, state_cls_name: str) -> str:
  157. """Get the state name for the given state class name.
  158. Args:
  159. state_cls_name: The state class name
  160. Returns:
  161. The state name
  162. """
  163. return reflex.utils.format.to_snake_case(
  164. f"{self.app_name}___{self.app_name}___" + state_cls_name
  165. )
  166. def get_full_state_name(self, path: list[str]) -> str:
  167. """Get the full state name for the given state class name.
  168. Args:
  169. path: A list of state class names
  170. Returns:
  171. The full state name
  172. """
  173. # NOTE: using State.get_name() somehow causes trouble here
  174. # path = [State.get_name()] + [self.get_state_name(p) for p in path] # noqa: ERA001
  175. path = ["reflex___state____state"] + [self.get_state_name(p) for p in path]
  176. return ".".join(path)
  177. def _get_globals_from_signature(self, func: Any) -> dict[str, Any]:
  178. """Get the globals from a function or module object.
  179. Args:
  180. func: function or module object
  181. Returns:
  182. dict of globals
  183. """
  184. overrides = {}
  185. glbs = {}
  186. if not callable(func):
  187. return glbs
  188. if isinstance(func, functools.partial):
  189. overrides = func.keywords
  190. func = func.func
  191. for param in inspect.signature(func).parameters.values():
  192. if param.default is not inspect.Parameter.empty:
  193. glbs[param.name] = param.default
  194. glbs.update(overrides)
  195. return glbs
  196. def _get_source_from_app_source(self, app_source: Any) -> str:
  197. """Get the source from app_source.
  198. Args:
  199. app_source: function or module or str
  200. Returns:
  201. source code
  202. """
  203. if isinstance(app_source, str):
  204. return app_source
  205. source = inspect.getsource(app_source)
  206. source = re.sub(
  207. r"^\s*def\s+\w+\s*\(.*?\)(\s+->\s+\w+)?:", "", source, flags=re.DOTALL
  208. )
  209. return textwrap.dedent(source)
  210. def _initialize_app(self):
  211. # disable telemetry reporting for tests
  212. os.environ["TELEMETRY_ENABLED"] = "false"
  213. CustomComponent.create().get_component.cache_clear()
  214. self.app_path.mkdir(parents=True, exist_ok=True)
  215. if self.app_source is not None:
  216. app_globals = self._get_globals_from_signature(self.app_source)
  217. if isinstance(self.app_source, functools.partial):
  218. self.app_source = self.app_source.func
  219. # get the source from a function or module object
  220. source_code = "\n".join(
  221. [
  222. "\n".join(
  223. self.get_app_global_source(k, v) for k, v in app_globals.items()
  224. ),
  225. self._get_source_from_app_source(self.app_source),
  226. ]
  227. )
  228. with chdir(self.app_path):
  229. reflex.reflex._init(
  230. name=self.app_name,
  231. template=reflex.constants.Templates.DEFAULT,
  232. loglevel=reflex.constants.LogLevel.INFO,
  233. )
  234. self.app_module_path.write_text(source_code)
  235. else:
  236. # Just initialize the web folder.
  237. with chdir(self.app_path):
  238. reflex.utils.prerequisites.initialize_frontend_dependencies()
  239. with chdir(self.app_path):
  240. # ensure config and app are reloaded when testing different app
  241. reflex.config.get_config(reload=True)
  242. # Save decorated pages before importing the test app module
  243. before_decorated_pages = reflex.app.DECORATED_PAGES[self.app_name].copy()
  244. # Ensure the AppHarness test does not skip State assignment due to running via pytest
  245. os.environ.pop(reflex.constants.PYTEST_CURRENT_TEST, None)
  246. os.environ[reflex.constants.APP_HARNESS_FLAG] = "true"
  247. self.app_module = reflex.utils.prerequisites.get_compiled_app(
  248. # Do not reload the module for pre-existing apps (only apps generated from source)
  249. reload=self.app_source is not None
  250. )
  251. # Save the pages that were added during testing
  252. self._decorated_pages = [
  253. p
  254. for p in reflex.app.DECORATED_PAGES[self.app_name]
  255. if p not in before_decorated_pages
  256. ]
  257. self.app_instance = self.app_module.app
  258. if self.app_instance and isinstance(
  259. self.app_instance._state_manager, StateManagerRedis
  260. ):
  261. if self.app_instance._state is None:
  262. raise RuntimeError("State is not set.")
  263. # Create our own redis connection for testing.
  264. self.state_manager = StateManagerRedis.create(self.app_instance._state)
  265. else:
  266. self.state_manager = (
  267. self.app_instance._state_manager if self.app_instance else None
  268. )
  269. def _reload_state_module(self):
  270. """Reload the rx.State module to avoid conflict when reloading."""
  271. reload_state_module(module=f"{self.app_name}.{self.app_name}")
  272. def _get_backend_shutdown_handler(self):
  273. if self.backend is None:
  274. raise RuntimeError("Backend was not initialized.")
  275. original_shutdown = self.backend.shutdown
  276. async def _shutdown_redis(*args, **kwargs) -> None:
  277. # ensure redis is closed before event loop
  278. try:
  279. if self.app_instance is not None and isinstance(
  280. self.app_instance.state_manager, StateManagerRedis
  281. ):
  282. await self.app_instance.state_manager.close()
  283. except ValueError:
  284. pass
  285. await original_shutdown(*args, **kwargs)
  286. return _shutdown_redis
  287. def _start_backend(self, port: int = 0):
  288. if self.app_instance is None or self.app_instance.api is None:
  289. raise RuntimeError("App was not initialized.")
  290. self.backend = uvicorn.Server(
  291. uvicorn.Config(
  292. app=self.app_instance.api,
  293. host="127.0.0.1",
  294. port=port,
  295. )
  296. )
  297. self.backend.shutdown = self._get_backend_shutdown_handler()
  298. with chdir(self.app_path):
  299. self.backend_thread = threading.Thread(target=self.backend.run)
  300. self.backend_thread.start()
  301. async def _reset_backend_state_manager(self):
  302. """Reset the StateManagerRedis event loop affinity.
  303. This is necessary when the backend is restarted and the state manager is a
  304. StateManagerRedis instance.
  305. Raises:
  306. RuntimeError: when the state manager cannot be reset
  307. """
  308. if (
  309. self.app_instance is not None
  310. and isinstance(
  311. self.app_instance.state_manager,
  312. StateManagerRedis,
  313. )
  314. and self.app_instance._state is not None
  315. ):
  316. with contextlib.suppress(RuntimeError):
  317. await self.app_instance.state_manager.close()
  318. self.app_instance._state_manager = StateManagerRedis.create(
  319. state=self.app_instance._state,
  320. )
  321. if not isinstance(self.app_instance.state_manager, StateManagerRedis):
  322. raise RuntimeError("Failed to reset state manager.")
  323. def _start_frontend(self):
  324. # Set up the frontend.
  325. with chdir(self.app_path):
  326. config = reflex.config.get_config()
  327. config.api_url = "http://{0}:{1}".format(
  328. *self._poll_for_servers().getsockname(),
  329. )
  330. reflex.utils.build.setup_frontend(self.app_path)
  331. # Start the frontend.
  332. self.frontend_process = reflex.utils.processes.new_process(
  333. [
  334. *reflex.utils.prerequisites.get_js_package_executor(raise_on_none=True)[
  335. 0
  336. ],
  337. "run",
  338. "dev",
  339. ],
  340. cwd=self.app_path / reflex.utils.prerequisites.get_web_dir(),
  341. env={"PORT": "0"},
  342. **FRONTEND_POPEN_ARGS,
  343. )
  344. def _wait_frontend(self):
  345. while self.frontend_url is None:
  346. line = (
  347. self.frontend_process.stdout.readline() # pyright: ignore [reportOptionalMemberAccess]
  348. )
  349. if not line:
  350. break
  351. print(line) # for pytest diagnosis #noqa: T201
  352. m = re.search(reflex.constants.Next.FRONTEND_LISTENING_REGEX, line)
  353. if m is not None:
  354. self.frontend_url = m.group(1)
  355. config = reflex.config.get_config()
  356. config.deploy_url = self.frontend_url
  357. break
  358. if self.frontend_url is None:
  359. raise RuntimeError("Frontend did not start")
  360. def consume_frontend_output():
  361. while True:
  362. try:
  363. line = (
  364. self.frontend_process.stdout.readline() # pyright: ignore [reportOptionalMemberAccess]
  365. )
  366. # catch I/O operation on closed file.
  367. except ValueError as e:
  368. console.error(str(e))
  369. break
  370. if not line:
  371. break
  372. self.frontend_output_thread = threading.Thread(target=consume_frontend_output)
  373. self.frontend_output_thread.start()
  374. def start(self) -> "AppHarness":
  375. """Start the backend in a new thread and dev frontend as a separate process.
  376. Returns:
  377. self
  378. """
  379. self._initialize_app()
  380. self._start_backend()
  381. self._start_frontend()
  382. self._wait_frontend()
  383. return self
  384. @staticmethod
  385. def get_app_global_source(key: str, value: Any):
  386. """Get the source code of a global object.
  387. If value is a function or class we render the actual
  388. source of value otherwise we assign value to key.
  389. Args:
  390. key: variable name to assign value to.
  391. value: value of the global variable.
  392. Returns:
  393. The rendered app global code.
  394. """
  395. if not inspect.isclass(value) and not inspect.isfunction(value):
  396. return f"{key} = {value!r}"
  397. return inspect.getsource(value)
  398. def __enter__(self) -> "AppHarness":
  399. """Contextmanager protocol for `start()`.
  400. Returns:
  401. Instance of AppHarness after calling start()
  402. """
  403. return self.start()
  404. def stop(self) -> None:
  405. """Stop the frontend and backend servers."""
  406. # Quit browsers first to avoid any lingering events being sent during shutdown.
  407. for driver in self._frontends:
  408. driver.quit()
  409. self._reload_state_module()
  410. if self.backend is not None:
  411. self.backend.should_exit = True
  412. if self.frontend_process is not None:
  413. # https://stackoverflow.com/a/70565806
  414. frontend_children = psutil.Process(self.frontend_process.pid).children(
  415. recursive=True,
  416. )
  417. if platform.system() == "Windows":
  418. self.frontend_process.terminate()
  419. else:
  420. pgrp = os.getpgid(self.frontend_process.pid)
  421. os.killpg(pgrp, signal.SIGTERM)
  422. # kill any remaining child processes
  423. for child in frontend_children:
  424. # It's okay if the process is already gone.
  425. with contextlib.suppress(psutil.NoSuchProcess):
  426. child.terminate()
  427. _, still_alive = psutil.wait_procs(frontend_children, timeout=3)
  428. for child in still_alive:
  429. # It's okay if the process is already gone.
  430. with contextlib.suppress(psutil.NoSuchProcess):
  431. child.kill()
  432. # wait for main process to exit
  433. self.frontend_process.communicate()
  434. if self.backend_thread is not None:
  435. self.backend_thread.join()
  436. if self.frontend_output_thread is not None:
  437. self.frontend_output_thread.join()
  438. # Cleanup decorated pages added during testing
  439. for page in self._decorated_pages:
  440. reflex.app.DECORATED_PAGES[self.app_name].remove(page)
  441. def __exit__(self, *excinfo) -> None:
  442. """Contextmanager protocol for `stop()`.
  443. Args:
  444. excinfo: sys.exc_info captured in the context block
  445. """
  446. self.stop()
  447. @staticmethod
  448. def _poll_for(
  449. target: Callable[[], T],
  450. timeout: TimeoutType = None,
  451. step: TimeoutType = None,
  452. ) -> T | bool:
  453. """Generic polling logic.
  454. Args:
  455. target: callable that returns truthy if polling condition is met.
  456. timeout: max polling time
  457. step: interval between checking target()
  458. Returns:
  459. return value of target() if truthy within timeout
  460. False if timeout elapses
  461. """
  462. if timeout is None:
  463. timeout = DEFAULT_TIMEOUT
  464. if step is None:
  465. step = POLL_INTERVAL
  466. deadline = time.time() + timeout
  467. while time.time() < deadline:
  468. success = target()
  469. if success:
  470. return success
  471. time.sleep(step)
  472. return False
  473. @staticmethod
  474. async def _poll_for_async(
  475. target: Callable[[], Coroutine[None, None, T]],
  476. timeout: TimeoutType = None,
  477. step: TimeoutType = None,
  478. ) -> T | bool:
  479. """Generic polling logic for async functions.
  480. Args:
  481. target: callable that returns truthy if polling condition is met.
  482. timeout: max polling time
  483. step: interval between checking target()
  484. Returns:
  485. return value of target() if truthy within timeout
  486. False if timeout elapses
  487. """
  488. if timeout is None:
  489. timeout = DEFAULT_TIMEOUT
  490. if step is None:
  491. step = POLL_INTERVAL
  492. deadline = time.time() + timeout
  493. while time.time() < deadline:
  494. success = await target()
  495. if success:
  496. return success
  497. await asyncio.sleep(step)
  498. return False
  499. def _poll_for_servers(self, timeout: TimeoutType = None) -> socket.socket:
  500. """Poll backend server for listening sockets.
  501. Args:
  502. timeout: how long to wait for listening socket.
  503. Returns:
  504. first active listening socket on the backend
  505. Raises:
  506. RuntimeError: when the backend hasn't started running
  507. TimeoutError: when server or sockets are not ready
  508. """
  509. if self.backend is None:
  510. raise RuntimeError("Backend is not running.")
  511. backend = self.backend
  512. # check for servers to be initialized
  513. if not self._poll_for(
  514. target=lambda: getattr(backend, "servers", False),
  515. timeout=timeout,
  516. ):
  517. raise TimeoutError("Backend servers are not initialized.")
  518. # check for sockets to be listening
  519. if not self._poll_for(
  520. target=lambda: getattr(backend.servers[0], "sockets", False),
  521. timeout=timeout,
  522. ):
  523. raise TimeoutError("Backend is not listening.")
  524. return backend.servers[0].sockets[0]
  525. def frontend(
  526. self,
  527. driver_clz: Optional[Type["WebDriver"]] = None,
  528. driver_kwargs: dict[str, Any] | None = None,
  529. driver_options: ArgOptions | None = None,
  530. driver_option_args: list[str] | None = None,
  531. driver_option_capabilities: dict[str, Any] | None = None,
  532. ) -> "WebDriver":
  533. """Get a selenium webdriver instance pointed at the app.
  534. Args:
  535. driver_clz: webdriver.Chrome (default), webdriver.Firefox, webdriver.Safari,
  536. webdriver.Edge, etc
  537. driver_kwargs: additional keyword arguments to pass to the webdriver constructor
  538. driver_options: selenium ArgOptions instance to pass to the webdriver constructor
  539. driver_option_args: additional arguments for the webdriver options
  540. driver_option_capabilities: additional capabilities for the webdriver options
  541. Returns:
  542. Instance of the given webdriver navigated to the frontend url of the app.
  543. Raises:
  544. RuntimeError: when selenium is not importable or frontend is not running
  545. """
  546. if not has_selenium:
  547. raise RuntimeError(
  548. "Frontend functionality requires `selenium` to be installed, "
  549. "and it could not be imported."
  550. )
  551. if self.frontend_url is None:
  552. raise RuntimeError("Frontend is not running.")
  553. want_headless = False
  554. if environment.APP_HARNESS_HEADLESS.get():
  555. want_headless = True
  556. if driver_clz is None:
  557. requested_driver = environment.APP_HARNESS_DRIVER.get()
  558. driver_clz = getattr(webdriver, requested_driver) # pyright: ignore [reportPossiblyUnboundVariable]
  559. if driver_options is None:
  560. driver_options = getattr(webdriver, f"{requested_driver}Options")() # pyright: ignore [reportPossiblyUnboundVariable]
  561. if driver_clz is webdriver.Chrome: # pyright: ignore [reportPossiblyUnboundVariable]
  562. if driver_options is None:
  563. driver_options = webdriver.ChromeOptions() # pyright: ignore [reportPossiblyUnboundVariable]
  564. driver_options.add_argument("--class=AppHarness")
  565. if want_headless:
  566. driver_options.add_argument("--headless=new")
  567. elif driver_clz is webdriver.Firefox: # pyright: ignore [reportPossiblyUnboundVariable]
  568. if driver_options is None:
  569. driver_options = webdriver.FirefoxOptions() # pyright: ignore [reportPossiblyUnboundVariable]
  570. if want_headless:
  571. driver_options.add_argument("-headless")
  572. elif driver_clz is webdriver.Edge: # pyright: ignore [reportPossiblyUnboundVariable]
  573. if driver_options is None:
  574. driver_options = webdriver.EdgeOptions() # pyright: ignore [reportPossiblyUnboundVariable]
  575. if want_headless:
  576. driver_options.add_argument("headless")
  577. if driver_options is None:
  578. raise RuntimeError(f"Could not determine options for {driver_clz}")
  579. if args := environment.APP_HARNESS_DRIVER_ARGS.get():
  580. for arg in args.split(","):
  581. driver_options.add_argument(arg)
  582. if driver_option_args is not None:
  583. for arg in driver_option_args:
  584. driver_options.add_argument(arg)
  585. if driver_option_capabilities is not None:
  586. for key, value in driver_option_capabilities.items():
  587. driver_options.set_capability(key, value)
  588. if driver_kwargs is None:
  589. driver_kwargs = {}
  590. driver = driver_clz(options=driver_options, **driver_kwargs) # pyright: ignore [reportOptionalCall, reportArgumentType]
  591. driver.get(self.frontend_url)
  592. self._frontends.append(driver)
  593. return driver
  594. async def get_state(self, token: str) -> BaseState:
  595. """Get the state associated with the given token.
  596. Args:
  597. token: The state token to look up.
  598. Returns:
  599. The state instance associated with the given token
  600. Raises:
  601. RuntimeError: when the app hasn't started running
  602. """
  603. if self.state_manager is None:
  604. raise RuntimeError("state_manager is not set.")
  605. try:
  606. return await self.state_manager.get_state(token)
  607. finally:
  608. if isinstance(self.state_manager, StateManagerRedis):
  609. await self.state_manager.close()
  610. async def set_state(self, token: str, **kwargs) -> None:
  611. """Set the state associated with the given token.
  612. Args:
  613. token: The state token to set.
  614. kwargs: Attributes to set on the state.
  615. Raises:
  616. RuntimeError: when the app hasn't started running
  617. """
  618. if self.state_manager is None:
  619. raise RuntimeError("state_manager is not set.")
  620. state = await self.get_state(token)
  621. for key, value in kwargs.items():
  622. setattr(state, key, value)
  623. try:
  624. await self.state_manager.set_state(token, state)
  625. finally:
  626. if isinstance(self.state_manager, StateManagerRedis):
  627. await self.state_manager.close()
  628. @contextlib.asynccontextmanager
  629. async def modify_state(self, token: str) -> AsyncIterator[BaseState]:
  630. """Modify the state associated with the given token and send update to frontend.
  631. Args:
  632. token: The state token to modify
  633. Yields:
  634. The state instance associated with the given token
  635. Raises:
  636. RuntimeError: when the app hasn't started running
  637. """
  638. if self.state_manager is None:
  639. raise RuntimeError("state_manager is not set.")
  640. if self.app_instance is None:
  641. raise RuntimeError("App is not running.")
  642. app_state_manager = self.app_instance.state_manager
  643. if isinstance(self.state_manager, StateManagerRedis):
  644. # Temporarily replace the app's state manager with our own, since
  645. # the redis connection is on the backend_thread event loop
  646. self.app_instance._state_manager = self.state_manager
  647. try:
  648. async with self.app_instance.modify_state(token) as state:
  649. yield state
  650. finally:
  651. if isinstance(self.state_manager, StateManagerRedis):
  652. self.app_instance._state_manager = app_state_manager
  653. await self.state_manager.close()
  654. def poll_for_content(
  655. self,
  656. element: "WebElement",
  657. timeout: TimeoutType = None,
  658. exp_not_equal: str = "",
  659. ) -> str:
  660. """Poll element.text for change.
  661. Args:
  662. element: selenium webdriver element to check
  663. timeout: how long to poll element.text
  664. exp_not_equal: exit the polling loop when the element text does not match
  665. Returns:
  666. The element text when the polling loop exited
  667. Raises:
  668. TimeoutError: when the timeout expires before text changes
  669. """
  670. if not self._poll_for(
  671. target=lambda: element.text != exp_not_equal,
  672. timeout=timeout,
  673. ):
  674. raise TimeoutError(
  675. f"{element} content remains {exp_not_equal!r} while polling.",
  676. )
  677. return element.text
  678. def poll_for_value(
  679. self,
  680. element: "WebElement",
  681. timeout: TimeoutType = None,
  682. exp_not_equal: str | Sequence[str] = "",
  683. ) -> str | None:
  684. """Poll element.get_attribute("value") for change.
  685. Args:
  686. element: selenium webdriver element to check
  687. timeout: how long to poll element value attribute
  688. exp_not_equal: exit the polling loop when the value does not match
  689. Returns:
  690. The element value when the polling loop exited
  691. Raises:
  692. TimeoutError: when the timeout expires before value changes
  693. """
  694. exp_not_equal = (
  695. (exp_not_equal,) if isinstance(exp_not_equal, str) else exp_not_equal
  696. )
  697. if not self._poll_for(
  698. target=lambda: element.get_attribute("value") not in exp_not_equal,
  699. timeout=timeout,
  700. ):
  701. raise TimeoutError(
  702. f"{element} content remains {exp_not_equal!r} while polling.",
  703. )
  704. return element.get_attribute("value")
  705. def poll_for_clients(self, timeout: TimeoutType = None) -> dict[str, BaseState]:
  706. """Poll app state_manager for any connected clients.
  707. Args:
  708. timeout: how long to wait for client states
  709. Returns:
  710. active state instances when the polling loop exited
  711. Raises:
  712. RuntimeError: when the app hasn't started running
  713. TimeoutError: when the timeout expires before any states are seen
  714. ValueError: when the state_manager is not a memory state manager
  715. """
  716. if self.app_instance is None:
  717. raise RuntimeError("App is not running.")
  718. state_manager = self.app_instance.state_manager
  719. if not isinstance(state_manager, (StateManagerMemory, StateManagerDisk)):
  720. raise ValueError("Only works with memory or disk state manager")
  721. if not self._poll_for(
  722. target=lambda: state_manager.states,
  723. timeout=timeout,
  724. ):
  725. raise TimeoutError("No states were observed while polling.")
  726. return state_manager.states
  727. class SimpleHTTPRequestHandlerCustomErrors(SimpleHTTPRequestHandler):
  728. """SimpleHTTPRequestHandler with custom error page handling."""
  729. def __init__(self, *args, error_page_map: dict[int, Path], **kwargs):
  730. """Initialize the handler.
  731. Args:
  732. error_page_map: map of error code to error page path
  733. *args: passed through to superclass
  734. **kwargs: passed through to superclass
  735. """
  736. self.error_page_map = error_page_map
  737. super().__init__(*args, **kwargs)
  738. def send_error(
  739. self, code: int, message: str | None = None, explain: str | None = None
  740. ) -> None:
  741. """Send the error page for the given error code.
  742. If the code matches a custom error page, then message and explain are
  743. ignored.
  744. Args:
  745. code: the error code
  746. message: the error message
  747. explain: the error explanation
  748. """
  749. error_page = self.error_page_map.get(code)
  750. if error_page:
  751. self.send_response(code, message)
  752. self.send_header("Connection", "close")
  753. body = error_page.read_bytes()
  754. self.send_header("Content-Type", self.error_content_type)
  755. self.send_header("Content-Length", str(len(body)))
  756. self.end_headers()
  757. self.wfile.write(body)
  758. else:
  759. super().send_error(code, message, explain)
  760. class Subdir404TCPServer(socketserver.TCPServer):
  761. """TCPServer for SimpleHTTPRequestHandlerCustomErrors that serves from a subdir."""
  762. def __init__(
  763. self,
  764. *args,
  765. root: Path,
  766. error_page_map: dict[int, Path] | None,
  767. **kwargs,
  768. ):
  769. """Initialize the server.
  770. Args:
  771. root: the root directory to serve from
  772. error_page_map: map of error code to error page path
  773. *args: passed through to superclass
  774. **kwargs: passed through to superclass
  775. """
  776. self.root = root
  777. self.error_page_map = error_page_map or {}
  778. super().__init__(*args, **kwargs)
  779. def finish_request(self, request: socket.socket, client_address: tuple[str, int]):
  780. """Finish one request by instantiating RequestHandlerClass.
  781. Args:
  782. request: the requesting socket
  783. client_address: (host, port) referring to the client's address.
  784. """
  785. self.RequestHandlerClass(
  786. request,
  787. client_address,
  788. self,
  789. directory=str(self.root), # pyright: ignore [reportCallIssue]
  790. error_page_map=self.error_page_map, # pyright: ignore [reportCallIssue]
  791. )
  792. class AppHarnessProd(AppHarness):
  793. """AppHarnessProd executes a reflex app in-process for testing.
  794. In prod mode, instead of running `next dev` the app is exported as static
  795. files and served via the builtin python http.server with custom 404 redirect
  796. handling. Additionally, the backend runs in multi-worker mode.
  797. """
  798. frontend_thread: threading.Thread | None = None
  799. frontend_server: Subdir404TCPServer | None = None
  800. def _run_frontend(self):
  801. web_root = (
  802. self.app_path
  803. / reflex.utils.prerequisites.get_web_dir()
  804. / reflex.constants.Dirs.STATIC
  805. )
  806. error_page_map = {
  807. 404: web_root / "404.html",
  808. }
  809. with Subdir404TCPServer(
  810. ("", 0),
  811. SimpleHTTPRequestHandlerCustomErrors,
  812. root=web_root,
  813. error_page_map=error_page_map,
  814. ) as self.frontend_server:
  815. self.frontend_url = "http://localhost:{1}".format(
  816. *self.frontend_server.socket.getsockname()
  817. )
  818. self.frontend_server.serve_forever()
  819. def _start_frontend(self):
  820. # Set up the frontend.
  821. with chdir(self.app_path):
  822. config = reflex.config.get_config()
  823. config.api_url = "http://{0}:{1}".format(
  824. *self._poll_for_servers().getsockname(),
  825. )
  826. reflex.reflex.export(
  827. zipping=False,
  828. frontend=True,
  829. backend=False,
  830. loglevel=reflex.constants.LogLevel.INFO,
  831. env=reflex.constants.Env.PROD,
  832. )
  833. self.frontend_thread = threading.Thread(target=self._run_frontend)
  834. self.frontend_thread.start()
  835. def _wait_frontend(self):
  836. self._poll_for(lambda: self.frontend_server is not None)
  837. if self.frontend_server is None or not self.frontend_server.socket.fileno():
  838. raise RuntimeError("Frontend did not start")
  839. def _start_backend(self):
  840. if self.app_instance is None:
  841. raise RuntimeError("App was not initialized.")
  842. environment.REFLEX_SKIP_COMPILE.set(True)
  843. self.backend = uvicorn.Server(
  844. uvicorn.Config(
  845. app=self.app_instance,
  846. host="127.0.0.1",
  847. port=0,
  848. workers=reflex.utils.processes.get_num_workers(),
  849. ),
  850. )
  851. self.backend.shutdown = self._get_backend_shutdown_handler()
  852. self.backend_thread = threading.Thread(target=self.backend.run)
  853. self.backend_thread.start()
  854. def _poll_for_servers(self, timeout: TimeoutType = None) -> socket.socket:
  855. try:
  856. return super()._poll_for_servers(timeout)
  857. finally:
  858. environment.REFLEX_SKIP_COMPILE.set(None)
  859. def stop(self):
  860. """Stop the frontend python webserver."""
  861. super().stop()
  862. if self.frontend_server is not None:
  863. self.frontend_server.shutdown()
  864. if self.frontend_thread is not None:
  865. self.frontend_thread.join()