test_input.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. """Integration tests for text input and related components."""
  2. import time
  3. from typing import Generator
  4. import pytest
  5. from selenium.webdriver.common.by import By
  6. from selenium.webdriver.common.keys import Keys
  7. from reflex.testing import AppHarness
  8. def FullyControlledInput():
  9. """App using a fully controlled input with implicit debounce wrapper."""
  10. import reflex as rx
  11. class State(rx.State):
  12. text: str = "initial"
  13. app = rx.App(state=rx.State)
  14. @app.add_page
  15. def index():
  16. return rx.fragment(
  17. rx.input(
  18. value=State.router.session.client_token, is_read_only=True, id="token"
  19. ),
  20. rx.input(
  21. id="debounce_input_input",
  22. on_change=State.set_text, # type: ignore
  23. value=State.text,
  24. ),
  25. rx.input(value=State.text, id="value_input", is_read_only=True),
  26. rx.input(on_change=State.set_text, id="on_change_input"), # type: ignore
  27. rx.el.input(
  28. value=State.text,
  29. id="plain_value_input",
  30. disabled=True,
  31. _disabled={"background_color": "#EEE"},
  32. ),
  33. rx.button("CLEAR", on_click=rx.set_value("on_change_input", "")),
  34. )
  35. app.compile()
  36. @pytest.fixture()
  37. def fully_controlled_input(tmp_path) -> Generator[AppHarness, None, None]:
  38. """Start FullyControlledInput app at tmp_path via AppHarness.
  39. Args:
  40. tmp_path: pytest tmp_path fixture
  41. Yields:
  42. running AppHarness instance
  43. """
  44. with AppHarness.create(
  45. root=tmp_path,
  46. app_source=FullyControlledInput, # type: ignore
  47. ) as harness:
  48. yield harness
  49. @pytest.mark.asyncio
  50. async def test_fully_controlled_input(fully_controlled_input: AppHarness):
  51. """Type text after moving cursor. Update text on backend.
  52. Args:
  53. fully_controlled_input: harness for FullyControlledInput app
  54. """
  55. assert fully_controlled_input.app_instance is not None, "app is not running"
  56. driver = fully_controlled_input.frontend()
  57. # get a reference to the connected client
  58. token_input = driver.find_element(By.ID, "token")
  59. assert token_input
  60. # wait for the backend connection to send the token
  61. token = fully_controlled_input.poll_for_value(token_input)
  62. assert token
  63. # find the input and wait for it to have the initial state value
  64. debounce_input = driver.find_element(By.ID, "debounce_input_input")
  65. value_input = driver.find_element(By.ID, "value_input")
  66. on_change_input = driver.find_element(By.ID, "on_change_input")
  67. plain_value_input = driver.find_element(By.ID, "plain_value_input")
  68. clear_button = driver.find_element(By.TAG_NAME, "button")
  69. assert fully_controlled_input.poll_for_value(debounce_input) == "initial"
  70. assert fully_controlled_input.poll_for_value(value_input) == "initial"
  71. assert fully_controlled_input.poll_for_value(plain_value_input) == "initial"
  72. assert (
  73. plain_value_input.value_of_css_property("background-color")
  74. == "rgba(238, 238, 238, 1)"
  75. )
  76. # move cursor to home, then to the right and type characters
  77. debounce_input.send_keys(Keys.HOME, Keys.ARROW_RIGHT)
  78. debounce_input.send_keys("foo")
  79. time.sleep(0.5)
  80. assert debounce_input.get_attribute("value") == "ifoonitial"
  81. assert (await fully_controlled_input.get_state(token)).substates[
  82. "state"
  83. ].text == "ifoonitial"
  84. assert fully_controlled_input.poll_for_value(value_input) == "ifoonitial"
  85. assert fully_controlled_input.poll_for_value(plain_value_input) == "ifoonitial"
  86. # clear the input on the backend
  87. async with fully_controlled_input.modify_state(token) as state:
  88. state.substates["state"].text = ""
  89. assert (await fully_controlled_input.get_state(token)).substates["state"].text == ""
  90. assert (
  91. fully_controlled_input.poll_for_value(
  92. debounce_input, exp_not_equal="ifoonitial"
  93. )
  94. == ""
  95. )
  96. # type more characters
  97. debounce_input.send_keys("getting testing done")
  98. time.sleep(0.5)
  99. assert debounce_input.get_attribute("value") == "getting testing done"
  100. assert (await fully_controlled_input.get_state(token)).substates[
  101. "state"
  102. ].text == "getting testing done"
  103. assert fully_controlled_input.poll_for_value(value_input) == "getting testing done"
  104. assert (
  105. fully_controlled_input.poll_for_value(plain_value_input)
  106. == "getting testing done"
  107. )
  108. # type into the on_change input
  109. on_change_input.send_keys("overwrite the state")
  110. time.sleep(0.5)
  111. assert debounce_input.get_attribute("value") == "overwrite the state"
  112. assert on_change_input.get_attribute("value") == "overwrite the state"
  113. assert (await fully_controlled_input.get_state(token)).substates[
  114. "state"
  115. ].text == "overwrite the state"
  116. assert fully_controlled_input.poll_for_value(value_input) == "overwrite the state"
  117. assert (
  118. fully_controlled_input.poll_for_value(plain_value_input)
  119. == "overwrite the state"
  120. )
  121. clear_button.click()
  122. time.sleep(0.5)
  123. assert on_change_input.get_attribute("value") == ""
  124. # potential bug: clearing the on_change field doesn't itself trigger on_change
  125. # assert backend_state.text == ""
  126. # assert debounce_input.get_attribute("value") == ""
  127. # assert value_input.get_attribute("value") == ""