test_input.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. """Integration tests for text input and related components."""
  2. from typing import Generator
  3. import pytest
  4. from selenium.webdriver.common.by import By
  5. from selenium.webdriver.common.keys import Keys
  6. from reflex.testing import AppHarness
  7. def FullyControlledInput():
  8. """App using a fully controlled input with implicit debounce wrapper."""
  9. import reflex as rx
  10. class State(rx.State):
  11. text: str = "initial"
  12. app = rx.App()
  13. @app.add_page
  14. def index():
  15. return rx.fragment(
  16. rx.input(
  17. value=State.router.session.client_token, is_read_only=True, id="token"
  18. ),
  19. rx.input(
  20. id="debounce_input_input",
  21. on_change=State.set_text, # pyright: ignore [reportAttributeAccessIssue]
  22. value=State.text,
  23. ),
  24. rx.input(value=State.text, id="value_input", is_read_only=True),
  25. rx.input(on_change=State.set_text, id="on_change_input"), # pyright: ignore [reportAttributeAccessIssue]
  26. rx.el.input(
  27. value=State.text,
  28. id="plain_value_input",
  29. disabled=True,
  30. _disabled={"width": "42px"},
  31. ),
  32. rx.input(default_value="default", id="default_input"),
  33. rx.el.input(
  34. type="text", default_value="default plain", id="plain_default_input"
  35. ),
  36. rx.checkbox(default_checked=True, id="default_checkbox"),
  37. rx.el.input(
  38. type="checkbox", default_checked=True, id="plain_default_checkbox"
  39. ),
  40. rx.button(
  41. "CLEAR", on_click=rx.set_value("on_change_input", ""), id="clear"
  42. ),
  43. )
  44. @pytest.fixture()
  45. def fully_controlled_input(tmp_path) -> Generator[AppHarness, None, None]:
  46. """Start FullyControlledInput app at tmp_path via AppHarness.
  47. Args:
  48. tmp_path: pytest tmp_path fixture
  49. Yields:
  50. running AppHarness instance
  51. """
  52. with AppHarness.create(
  53. root=tmp_path,
  54. app_source=FullyControlledInput,
  55. ) as harness:
  56. yield harness
  57. @pytest.mark.asyncio
  58. async def test_fully_controlled_input(fully_controlled_input: AppHarness):
  59. """Type text after moving cursor. Update text on backend.
  60. Args:
  61. fully_controlled_input: harness for FullyControlledInput app
  62. """
  63. assert fully_controlled_input.app_instance is not None, "app is not running"
  64. driver = fully_controlled_input.frontend()
  65. # get a reference to the connected client
  66. token_input = driver.find_element(By.ID, "token")
  67. assert token_input
  68. # wait for the backend connection to send the token
  69. token = fully_controlled_input.poll_for_value(token_input)
  70. assert token
  71. state_name = fully_controlled_input.get_state_name("_state")
  72. full_state_name = fully_controlled_input.get_full_state_name(["_state"])
  73. async def get_state_text():
  74. state = await fully_controlled_input.get_state(f"{token}_{full_state_name}")
  75. return state.substates[state_name].text
  76. # ensure defaults are set correctly
  77. assert (
  78. fully_controlled_input.poll_for_value(
  79. driver.find_element(By.ID, "default_input")
  80. )
  81. == "default"
  82. )
  83. assert (
  84. fully_controlled_input.poll_for_value(
  85. driver.find_element(By.ID, "plain_default_input")
  86. )
  87. == "default plain"
  88. )
  89. assert (
  90. fully_controlled_input.poll_for_value(
  91. driver.find_element(By.ID, "default_checkbox")
  92. )
  93. == "on"
  94. )
  95. assert (
  96. fully_controlled_input.poll_for_value(
  97. driver.find_element(By.ID, "plain_default_checkbox")
  98. )
  99. == "on"
  100. )
  101. # find the input and wait for it to have the initial state value
  102. debounce_input = driver.find_element(By.ID, "debounce_input_input")
  103. value_input = driver.find_element(By.ID, "value_input")
  104. on_change_input = driver.find_element(By.ID, "on_change_input")
  105. plain_value_input = driver.find_element(By.ID, "plain_value_input")
  106. clear_button = driver.find_element(By.ID, "clear")
  107. assert fully_controlled_input.poll_for_value(debounce_input) == "initial"
  108. assert fully_controlled_input.poll_for_value(value_input) == "initial"
  109. assert fully_controlled_input.poll_for_value(plain_value_input) == "initial"
  110. assert plain_value_input.value_of_css_property("width") == "42px"
  111. # move cursor to home, then to the right and type characters
  112. debounce_input.send_keys(*([Keys.ARROW_LEFT] * len("initial")), Keys.ARROW_RIGHT)
  113. debounce_input.send_keys("foo")
  114. assert AppHarness._poll_for(
  115. lambda: fully_controlled_input.poll_for_value(value_input) == "ifoonitial"
  116. )
  117. assert debounce_input.get_attribute("value") == "ifoonitial"
  118. assert await get_state_text() == "ifoonitial"
  119. assert fully_controlled_input.poll_for_value(plain_value_input) == "ifoonitial"
  120. # clear the input on the backend
  121. async with fully_controlled_input.modify_state(
  122. f"{token}_{full_state_name}"
  123. ) as state:
  124. state.substates[state_name].text = ""
  125. assert await get_state_text() == ""
  126. assert (
  127. fully_controlled_input.poll_for_value(
  128. debounce_input, exp_not_equal="ifoonitial"
  129. )
  130. == ""
  131. )
  132. # type more characters
  133. debounce_input.send_keys("getting testing done")
  134. assert AppHarness._poll_for(
  135. lambda: fully_controlled_input.poll_for_value(value_input)
  136. == "getting testing done"
  137. )
  138. assert debounce_input.get_attribute("value") == "getting testing done"
  139. assert await get_state_text() == "getting testing done"
  140. assert (
  141. fully_controlled_input.poll_for_value(plain_value_input)
  142. == "getting testing done"
  143. )
  144. # type into the on_change input
  145. on_change_input.send_keys("overwrite the state")
  146. assert AppHarness._poll_for(
  147. lambda: fully_controlled_input.poll_for_value(value_input)
  148. == "overwrite the state"
  149. )
  150. assert debounce_input.get_attribute("value") == "overwrite the state"
  151. assert on_change_input.get_attribute("value") == "overwrite the state"
  152. assert await get_state_text() == "overwrite the state"
  153. assert (
  154. fully_controlled_input.poll_for_value(plain_value_input)
  155. == "overwrite the state"
  156. )
  157. clear_button.click()
  158. assert AppHarness._poll_for(lambda: on_change_input.get_attribute("value") == "")
  159. # potential bug: clearing the on_change field doesn't itself trigger on_change
  160. # assert backend_state.text == "" #noqa: ERA001
  161. # assert debounce_input.get_attribute("value") == "" #noqa: ERA001
  162. # assert value_input.get_attribute("value") == "" #noqa: ERA001