test_input.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. @pytest.fixture()
  36. def fully_controlled_input(tmp_path) -> Generator[AppHarness, None, None]:
  37. """Start FullyControlledInput app at tmp_path via AppHarness.
  38. Args:
  39. tmp_path: pytest tmp_path fixture
  40. Yields:
  41. running AppHarness instance
  42. """
  43. with AppHarness.create(
  44. root=tmp_path,
  45. app_source=FullyControlledInput, # type: ignore
  46. ) as harness:
  47. yield harness
  48. @pytest.mark.asyncio
  49. async def test_fully_controlled_input(fully_controlled_input: AppHarness):
  50. """Type text after moving cursor. Update text on backend.
  51. Args:
  52. fully_controlled_input: harness for FullyControlledInput app
  53. """
  54. assert fully_controlled_input.app_instance is not None, "app is not running"
  55. driver = fully_controlled_input.frontend()
  56. # get a reference to the connected client
  57. token_input = driver.find_element(By.ID, "token")
  58. assert token_input
  59. # wait for the backend connection to send the token
  60. token = fully_controlled_input.poll_for_value(token_input)
  61. assert token
  62. # find the input and wait for it to have the initial state value
  63. debounce_input = driver.find_element(By.ID, "debounce_input_input")
  64. value_input = driver.find_element(By.ID, "value_input")
  65. on_change_input = driver.find_element(By.ID, "on_change_input")
  66. plain_value_input = driver.find_element(By.ID, "plain_value_input")
  67. clear_button = driver.find_element(By.TAG_NAME, "button")
  68. assert fully_controlled_input.poll_for_value(debounce_input) == "initial"
  69. assert fully_controlled_input.poll_for_value(value_input) == "initial"
  70. assert fully_controlled_input.poll_for_value(plain_value_input) == "initial"
  71. assert (
  72. plain_value_input.value_of_css_property("background-color")
  73. == "rgba(238, 238, 238, 1)"
  74. )
  75. # move cursor to home, then to the right and type characters
  76. debounce_input.send_keys(Keys.HOME, Keys.ARROW_RIGHT)
  77. debounce_input.send_keys("foo")
  78. time.sleep(0.5)
  79. assert debounce_input.get_attribute("value") == "ifoonitial"
  80. assert (await fully_controlled_input.get_state(token)).substates[
  81. "state"
  82. ].text == "ifoonitial"
  83. assert fully_controlled_input.poll_for_value(value_input) == "ifoonitial"
  84. assert fully_controlled_input.poll_for_value(plain_value_input) == "ifoonitial"
  85. # clear the input on the backend
  86. async with fully_controlled_input.modify_state(token) as state:
  87. state.substates["state"].text = ""
  88. assert (await fully_controlled_input.get_state(token)).substates["state"].text == ""
  89. assert (
  90. fully_controlled_input.poll_for_value(
  91. debounce_input, exp_not_equal="ifoonitial"
  92. )
  93. == ""
  94. )
  95. # type more characters
  96. debounce_input.send_keys("getting testing done")
  97. time.sleep(0.5)
  98. assert debounce_input.get_attribute("value") == "getting testing done"
  99. assert (await fully_controlled_input.get_state(token)).substates[
  100. "state"
  101. ].text == "getting testing done"
  102. assert fully_controlled_input.poll_for_value(value_input) == "getting testing done"
  103. assert (
  104. fully_controlled_input.poll_for_value(plain_value_input)
  105. == "getting testing done"
  106. )
  107. # type into the on_change input
  108. on_change_input.send_keys("overwrite the state")
  109. time.sleep(0.5)
  110. assert debounce_input.get_attribute("value") == "overwrite the state"
  111. assert on_change_input.get_attribute("value") == "overwrite the state"
  112. assert (await fully_controlled_input.get_state(token)).substates[
  113. "state"
  114. ].text == "overwrite the state"
  115. assert fully_controlled_input.poll_for_value(value_input) == "overwrite the state"
  116. assert (
  117. fully_controlled_input.poll_for_value(plain_value_input)
  118. == "overwrite the state"
  119. )
  120. clear_button.click()
  121. time.sleep(0.5)
  122. assert on_change_input.get_attribute("value") == ""
  123. # potential bug: clearing the on_change field doesn't itself trigger on_change
  124. # assert backend_state.text == ""
  125. # assert debounce_input.get_attribute("value") == ""
  126. # assert value_input.get_attribute("value") == ""