test_deploy_url.py 2.8 KB

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