test_dict.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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_dict(page: "Page", gui: Gui, helpers):
  20. page_md = """
  21. <|{a_dict[a_key]}|input|id=inp1|>
  22. <|{a_dict.key}|input|id=inp2|>
  23. <|test|button|on_action=on_action_1|id=btn1|>
  24. <|test|button|on_action=on_action_2|id=btn2|>
  25. """
  26. a_key = "key"
  27. a_dict = {a_key: "Taipy"} # noqa: F841
  28. def on_action_1(state):
  29. state.a_dict.key = "Hello"
  30. def on_action_2(state):
  31. state.a_dict[state.a_key] = "World"
  32. gui._set_frame(inspect.currentframe())
  33. gui.add_page(name="test", page=page_md)
  34. helpers.run_e2e(gui)
  35. page.goto("./test")
  36. page.expect_websocket()
  37. page.wait_for_selector("#inp1")
  38. assert_text(page, "Taipy", "Taipy")
  39. page.fill("input#inp1", "Taipy is the best")
  40. function_evaluated = True
  41. try:
  42. page.wait_for_function("document.querySelector('#inp2').value !== 'Taipy'")
  43. except Exception as e:
  44. function_evaluated = False
  45. logging.getLogger().debug(f"Function evaluation timeout.\n{e}")
  46. if function_evaluated:
  47. assert_text(page, "Taipy is the best", "Taipy is the best")
  48. page.fill("#inp2", "Taipy-Gui")
  49. function_evaluated = True
  50. try:
  51. page.wait_for_function("document.querySelector('#inp1').value !== 'Taipy is the best'")
  52. except Exception as e:
  53. function_evaluated = False
  54. logging.getLogger().debug(f"Function evaluation timeout.\n{e}")
  55. if function_evaluated:
  56. assert_text(page, "Taipy-Gui", "Taipy-Gui")
  57. page.click("#btn1")
  58. function_evaluated = True
  59. try:
  60. page.wait_for_function("document.querySelector('#inp1').value !== 'Taipy-Gui'")
  61. except Exception as e:
  62. function_evaluated = False
  63. logging.getLogger().debug(f"Function evaluation timeout.\n{e}")
  64. if function_evaluated:
  65. assert_text(page, "Hello", "Hello")
  66. page.click("#btn2")
  67. function_evaluated = True
  68. try:
  69. page.wait_for_function("document.querySelector('#inp1').value !== 'Hello'")
  70. except Exception as e:
  71. function_evaluated = False
  72. logging.getLogger().debug(f"Function evaluation timeout.\n{e}")
  73. if function_evaluated:
  74. assert_text(page, "World", "World")
  75. def assert_text(page, inp1, inp2):
  76. assert page.input_value("input#inp1") == inp1
  77. assert page.input_value("input#inp2") == inp2