test_input.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # Copyright 2021-2024 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. import inspect
  12. import logging
  13. from importlib import util
  14. import pytest
  15. if util.find_spec("playwright"):
  16. from playwright._impl._page import Page
  17. from taipy.gui import Gui
  18. @pytest.mark.teste2e
  19. def test_input_action(page: "Page", gui: Gui, helpers):
  20. page_md = """
  21. <|{input1_value}|input|on_action=input_action|id=input1|>
  22. <|{input2_value}|input|on_action=input_action|id=input2|action_on_blur|>
  23. <|X|button|id=button1|on_action=button_action|>
  24. <|{input1_action_tracker}|id=input1_tracker|>
  25. <|{input2_action_tracker}|id=input2_tracker|>
  26. <|{button_action_tracker}|id=button_tracker|>
  27. """
  28. input1_value = "init" # noqa: F841
  29. input2_value = "init" # noqa: F841
  30. input1_action_tracker = 0 # noqa: F841
  31. input2_action_tracker = 0 # noqa: F841
  32. button_action_tracker = 0 # noqa: F841
  33. def input_action(state, id):
  34. if id == "input1":
  35. state.input1_action_tracker = state.input1_action_tracker + 1
  36. elif id == "input2":
  37. state.input2_action_tracker = state.input2_action_tracker + 1
  38. def button_action(state, id):
  39. state.button_action_tracker = state.button_action_tracker + 1
  40. gui._set_frame(inspect.currentframe())
  41. gui.add_page(name="test", page=page_md)
  42. helpers.run_e2e(gui)
  43. page.goto("./test")
  44. page.expect_websocket()
  45. page.wait_for_selector("#input1_tracker")
  46. assert page.query_selector("#input1").input_value() == "init", "Wrong initial value"
  47. page.click("#button1")
  48. try:
  49. page.wait_for_function("document.querySelector('#button_tracker').innerText !== '0'")
  50. except Exception as e:
  51. logging.getLogger().debug(f"Function evaluation timeout.\n{e}")
  52. assert page.query_selector("#button_tracker").inner_text() == "1"
  53. page.click("#input1")
  54. page.fill("#input1", "step2")
  55. page.click("#button1")
  56. try:
  57. page.wait_for_function("document.querySelector('#button_tracker').innerText !== '1'")
  58. except Exception as e:
  59. logging.getLogger().debug(f"Function evaluation timeout.\n{e}")
  60. assert page.query_selector("#button_tracker").inner_text() == "2", "Button action should have been invoked"
  61. assert (
  62. page.query_selector("#input1_tracker").inner_text() == "0"
  63. ), "Action should not have been invoked (no action_on_blur)"
  64. page.click("#input2")
  65. page.fill("#input2", "step2")
  66. page.click("#button1")
  67. try:
  68. page.wait_for_function("document.querySelector('#button_tracker').innerText !== '2'")
  69. except Exception as e:
  70. logging.getLogger().debug(f"Function evaluation timeout.\n{e}")
  71. assert page.query_selector("#button_tracker").inner_text() == "3", "Button action should have been invoked"
  72. assert (
  73. page.query_selector("#input2_tracker").inner_text() == "1"
  74. ), "Action should have been invoked (action_on_blur)"