test_deploy_url.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. """Integration tests for deploy_url."""
  2. from __future__ import annotations
  3. from typing import Generator
  4. import pytest
  5. from selenium.webdriver.common.by import By
  6. from selenium.webdriver.remote.webdriver import WebDriver
  7. from selenium.webdriver.support.ui import WebDriverWait
  8. from reflex.testing import AppHarness
  9. def DeployUrlSample() -> None:
  10. """Sample app for testing config deploy_url is correct (in tests)."""
  11. import reflex as rx
  12. class State(rx.State):
  13. @rx.event
  14. def goto_self(self):
  15. return rx.redirect(rx.config.get_config().deploy_url) # pyright: ignore [reportArgumentType]
  16. def index():
  17. return rx.fragment(
  18. rx.button("GOTO SELF", on_click=State.goto_self, id="goto_self")
  19. )
  20. app = rx.App(_state=rx.State)
  21. app.add_page(index)
  22. @pytest.fixture(scope="module")
  23. def deploy_url_sample(
  24. tmp_path_factory: pytest.TempPathFactory,
  25. ) -> Generator[AppHarness, None, None]:
  26. """AppHarness fixture for testing deploy_url.
  27. Args:
  28. tmp_path_factory: pytest fixture for creating temporary directories.
  29. Yields:
  30. AppHarness: An AppHarness instance.
  31. """
  32. with AppHarness.create(
  33. root=tmp_path_factory.mktemp("deploy_url_sample"),
  34. app_source=DeployUrlSample,
  35. ) as harness:
  36. yield harness
  37. @pytest.fixture()
  38. def driver(deploy_url_sample: AppHarness) -> Generator[WebDriver, None, None]:
  39. """WebDriver fixture for testing deploy_url.
  40. Args:
  41. deploy_url_sample: AppHarness fixture for testing deploy_url.
  42. Yields:
  43. WebDriver: A WebDriver instance.
  44. """
  45. assert deploy_url_sample.app_instance is not None, "app is not running"
  46. driver = deploy_url_sample.frontend()
  47. try:
  48. yield driver
  49. finally:
  50. driver.quit()
  51. def test_deploy_url(deploy_url_sample: AppHarness, driver: WebDriver) -> None:
  52. """Test deploy_url is correct.
  53. Args:
  54. deploy_url_sample: AppHarness fixture for testing deploy_url.
  55. driver: WebDriver fixture for testing deploy_url.
  56. """
  57. import reflex as rx
  58. deploy_url = rx.config.get_config().deploy_url
  59. assert deploy_url is not None
  60. assert deploy_url != "http://localhost:3000"
  61. assert deploy_url == deploy_url_sample.frontend_url
  62. driver.get(deploy_url)
  63. assert driver.current_url == deploy_url + "/"
  64. def test_deploy_url_in_app(deploy_url_sample: AppHarness, driver: WebDriver) -> None:
  65. """Test deploy_url is correct in app.
  66. Args:
  67. deploy_url_sample: AppHarness fixture for testing deploy_url.
  68. driver: WebDriver fixture for testing deploy_url.
  69. """
  70. driver.implicitly_wait(10)
  71. driver.find_element(By.ID, "goto_self").click()
  72. WebDriverWait(driver, 10).until(
  73. lambda driver: driver.current_url == f"{deploy_url_sample.frontend_url}/"
  74. )