test_extra_overlay_function.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. """Test case for adding an overlay component defined in the rxconfig."""
  2. from typing import Generator
  3. import pytest
  4. from selenium.webdriver.common.by import By
  5. from reflex.testing import AppHarness, WebDriver
  6. def ExtraOverlay():
  7. import reflex as rx
  8. rx.config.get_config().extra_overlay_function = "reflex.components.moment.moment"
  9. def index():
  10. return rx.vstack(
  11. rx.el.input(
  12. id="token",
  13. value=rx.State.router.session.client_token,
  14. is_read_only=True,
  15. ),
  16. rx.text(
  17. "Hello World",
  18. ),
  19. )
  20. app = rx.App(_state=rx.State)
  21. app.add_page(index)
  22. @pytest.fixture(scope="module")
  23. def extra_overlay(tmp_path_factory) -> Generator[AppHarness, None, None]:
  24. """Start ExtraOverlay app at tmp_path via AppHarness.
  25. Args:
  26. tmp_path_factory: pytest tmp_path_factory fixture
  27. Yields:
  28. running AppHarness instance
  29. """
  30. with AppHarness.create(
  31. root=tmp_path_factory.mktemp("extra_overlay"),
  32. app_source=ExtraOverlay,
  33. ) as harness:
  34. assert harness.app_instance is not None, "app is not running"
  35. yield harness
  36. @pytest.fixture
  37. def driver(extra_overlay: AppHarness):
  38. """Get an instance of the browser open to the extra overlay app.
  39. Args:
  40. extra_overlay: harness for the ExtraOverlay app.
  41. Yields:
  42. WebDriver instance.
  43. """
  44. driver = extra_overlay.frontend()
  45. try:
  46. token_input = driver.find_element(By.ID, "token")
  47. assert token_input
  48. # wait for the backend connection to send the token
  49. token = extra_overlay.poll_for_value(token_input)
  50. assert token is not None
  51. yield driver
  52. finally:
  53. driver.quit()
  54. def test_extra_overlay(driver: WebDriver, extra_overlay: AppHarness):
  55. """Test the ExtraOverlay app.
  56. Args:
  57. driver: WebDriver instance.
  58. extra_overlay: harness for the ExtraOverlay app.
  59. """
  60. # Check that the text is displayed.
  61. text = driver.find_element(By.XPATH, "//*[contains(text(), 'Hello World')]")
  62. assert text
  63. assert text.text == "Hello World"
  64. time = driver.find_element(By.TAG_NAME, "time")
  65. assert time
  66. assert time.text