test_page_scopes_state.py 2.7 KB

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