test_page_scopes_state.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # Copyright 2023 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. from taipy.gui import Gui
  16. if util.find_spec("playwright"):
  17. from playwright._impl._page import Page
  18. from .assets4.page1 import page as page1
  19. from .assets4.page1 import reset_d
  20. @pytest.mark.timeout(300)
  21. @pytest.mark.teste2e
  22. @pytest.mark.filterwarnings("ignore::Warning")
  23. def test_page_scopes_state_runtime(page: "Page", gui: Gui, helpers):
  24. gui._set_frame(inspect.currentframe())
  25. def test(state):
  26. reset_d(state)
  27. def test2(state):
  28. state["page1"].d = 30
  29. page_md = """
  30. <|button|on_action=test|id=btn1|>
  31. <|button|on_action=test2|id=btn2|>
  32. """
  33. gui.add_page("page1", page1)
  34. gui.add_page(name=Gui._get_root_page_name(), page=page_md)
  35. helpers.run_e2e(gui)
  36. page.goto("./page1")
  37. page.expect_websocket()
  38. page.wait_for_selector("#n1")
  39. text1 = page.query_selector("#t1")
  40. assert text1.inner_text() == "20"
  41. page.fill("#n1", "21")
  42. function_evaluated = True
  43. try:
  44. page.wait_for_function("document.querySelector('#t1').innerText !== '20'")
  45. function_evaluated = True
  46. except Exception as e:
  47. function_evaluated = False
  48. logging.getLogger().debug(f"Function evaluation timeout.\n{e}")
  49. if not function_evaluated:
  50. return
  51. text1 = page.query_selector("#t1")
  52. assert text1.inner_text() == "21"
  53. page.click("#btn1")
  54. try:
  55. page.wait_for_function("document.querySelector('#t1').innerText !== '21'")
  56. function_evaluated = True
  57. except Exception as e:
  58. function_evaluated = False
  59. logging.getLogger().debug(f"Function evaluation timeout.\n{e}")
  60. if not function_evaluated:
  61. return
  62. text1 = page.query_selector("#t1")
  63. assert text1.inner_text() == "20"
  64. page.click("#btn2")
  65. try:
  66. page.wait_for_function("document.querySelector('#t1').innerText !== '20'")
  67. function_evaluated = True
  68. except Exception as e:
  69. function_evaluated = False
  70. logging.getLogger().debug(f"Function evaluation timeout.\n{e}")
  71. if not function_evaluated:
  72. return
  73. text1 = page.query_selector("#t1")
  74. assert text1.inner_text() == "30"