test_table.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. """Integration tests for table and related components."""
  2. from typing import Generator
  3. import pytest
  4. from selenium.webdriver.common.by import By
  5. from reflex.testing import AppHarness
  6. def Table():
  7. """App using table component."""
  8. import reflex as rx
  9. app = rx.App(state=rx.State)
  10. @app.add_page
  11. def index():
  12. return rx.center(
  13. rx.input(
  14. id="token",
  15. value=rx.State.router.session.client_token,
  16. is_read_only=True,
  17. ),
  18. rx.table.root(
  19. rx.table.header(
  20. rx.table.row(
  21. rx.table.column_header_cell("Name"),
  22. rx.table.column_header_cell("Age"),
  23. rx.table.column_header_cell("Location"),
  24. ),
  25. ),
  26. rx.table.body(
  27. rx.table.row(
  28. rx.table.row_header_cell("John"),
  29. rx.table.cell(30),
  30. rx.table.cell("New York"),
  31. ),
  32. rx.table.row(
  33. rx.table.row_header_cell("Jane"),
  34. rx.table.cell(31),
  35. rx.table.cell("San Fransisco"),
  36. ),
  37. rx.table.row(
  38. rx.table.row_header_cell("Joe"),
  39. rx.table.cell(32),
  40. rx.table.cell("Los Angeles"),
  41. ),
  42. ),
  43. width="100%",
  44. ),
  45. )
  46. @pytest.fixture()
  47. def table(tmp_path_factory) -> Generator[AppHarness, None, None]:
  48. """Start Table app at tmp_path via AppHarness.
  49. Args:
  50. tmp_path_factory: pytest tmp_path_factory fixture
  51. Yields:
  52. running AppHarness instance
  53. """
  54. with AppHarness.create(
  55. root=tmp_path_factory.mktemp("table"),
  56. app_source=Table, # type: ignore
  57. ) as harness:
  58. assert harness.app_instance is not None, "app is not running"
  59. yield harness
  60. @pytest.fixture
  61. def driver(table: AppHarness):
  62. """GEt an instance of the browser open to the table app.
  63. Args:
  64. table: harness for Table app
  65. Yields:
  66. WebDriver instance.
  67. """
  68. driver = table.frontend()
  69. try:
  70. token_input = driver.find_element(By.ID, "token")
  71. assert token_input
  72. # wait for the backend connection to send the token
  73. token = table.poll_for_value(token_input)
  74. assert token is not None
  75. yield driver
  76. finally:
  77. driver.quit()
  78. def test_table(driver, table: AppHarness):
  79. """Test that a table component is rendered properly.
  80. Args:
  81. driver: Selenium WebDriver open to the app
  82. table: Harness for Table app
  83. """
  84. assert table.app_instance is not None, "app is not running"
  85. thead = driver.find_element(By.TAG_NAME, "thead")
  86. # poll till page is fully loaded.
  87. table.poll_for_content(element=thead)
  88. # check headers
  89. assert thead.find_element(By.TAG_NAME, "tr").text == "Name Age Location"
  90. # check first row value
  91. assert (
  92. driver.find_element(By.TAG_NAME, "tbody")
  93. .find_elements(By.TAG_NAME, "tr")[0]
  94. .text
  95. == "John 30 New York"
  96. )