test_input.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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(state=rx.State)
  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, # type: ignore
  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"), # type: ignore
  26. rx.el.input(
  27. value=State.text,
  28. id="plain_value_input",
  29. disabled=True,
  30. _disabled={"background_color": "#EEE"},
  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, # type: ignore
  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. async def get_state_text():
  72. state = await fully_controlled_input.get_state(f"{token}_state.state")
  73. return state.substates["state"].text
  74. # ensure defaults are set correctly
  75. assert (
  76. fully_controlled_input.poll_for_value(
  77. driver.find_element(By.ID, "default_input")
  78. )
  79. == "default"
  80. )
  81. assert (
  82. fully_controlled_input.poll_for_value(
  83. driver.find_element(By.ID, "plain_default_input")
  84. )
  85. == "default plain"
  86. )
  87. assert (
  88. fully_controlled_input.poll_for_value(
  89. driver.find_element(By.ID, "default_checkbox")
  90. )
  91. == "on"
  92. )
  93. assert (
  94. fully_controlled_input.poll_for_value(
  95. driver.find_element(By.ID, "plain_default_checkbox")
  96. )
  97. == "on"
  98. )
  99. # find the input and wait for it to have the initial state value
  100. debounce_input = driver.find_element(By.ID, "debounce_input_input")
  101. value_input = driver.find_element(By.ID, "value_input")
  102. on_change_input = driver.find_element(By.ID, "on_change_input")
  103. plain_value_input = driver.find_element(By.ID, "plain_value_input")
  104. clear_button = driver.find_element(By.ID, "clear")
  105. assert fully_controlled_input.poll_for_value(debounce_input) == "initial"
  106. assert fully_controlled_input.poll_for_value(value_input) == "initial"
  107. assert fully_controlled_input.poll_for_value(plain_value_input) == "initial"
  108. assert (
  109. plain_value_input.value_of_css_property("background-color")
  110. == "rgba(238, 238, 238, 1)"
  111. )
  112. # move cursor to home, then to the right and type characters
  113. debounce_input.send_keys(Keys.HOME, Keys.ARROW_RIGHT)
  114. debounce_input.send_keys("foo")
  115. assert AppHarness._poll_for(
  116. lambda: fully_controlled_input.poll_for_value(value_input) == "ifoonitial"
  117. )
  118. assert debounce_input.get_attribute("value") == "ifoonitial"
  119. assert await get_state_text() == "ifoonitial"
  120. assert fully_controlled_input.poll_for_value(plain_value_input) == "ifoonitial"
  121. # clear the input on the backend
  122. async with fully_controlled_input.modify_state(f"{token}_state.state") as state:
  123. state.substates["state"].text = ""
  124. assert await get_state_text() == ""
  125. assert (
  126. fully_controlled_input.poll_for_value(
  127. debounce_input, exp_not_equal="ifoonitial"
  128. )
  129. == ""
  130. )
  131. # type more characters
  132. debounce_input.send_keys("getting testing done")
  133. assert AppHarness._poll_for(
  134. lambda: fully_controlled_input.poll_for_value(value_input)
  135. == "getting testing done"
  136. )
  137. assert debounce_input.get_attribute("value") == "getting testing done"
  138. assert await get_state_text() == "getting testing done"
  139. assert (
  140. fully_controlled_input.poll_for_value(plain_value_input)
  141. == "getting testing done"
  142. )
  143. # type into the on_change input
  144. on_change_input.send_keys("overwrite the state")
  145. assert AppHarness._poll_for(
  146. lambda: fully_controlled_input.poll_for_value(value_input)
  147. == "overwrite the state"
  148. )
  149. assert debounce_input.get_attribute("value") == "overwrite the state"
  150. assert on_change_input.get_attribute("value") == "overwrite the state"
  151. assert await get_state_text() == "overwrite the state"
  152. assert (
  153. fully_controlled_input.poll_for_value(plain_value_input)
  154. == "overwrite the state"
  155. )
  156. clear_button.click()
  157. assert AppHarness._poll_for(lambda: on_change_input.get_attribute("value") == "")
  158. # potential bug: clearing the on_change field doesn't itself trigger on_change
  159. # assert backend_state.text == ""
  160. # assert debounce_input.get_attribute("value") == ""
  161. # assert value_input.get_attribute("value") == ""