test_slider_action.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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_slider_action(page: "Page", gui: Gui, helpers):
  20. page_md = """
  21. <|{x}|id=text1|>
  22. <|{x}|slider|id=slider1|>
  23. """
  24. x = 10 # noqa: F841
  25. gui._set_frame(inspect.currentframe())
  26. gui.add_page(name="test", page=page_md)
  27. helpers.run_e2e(gui)
  28. page.goto("./test")
  29. page.expect_websocket()
  30. page.wait_for_selector("#text1")
  31. text1 = page.query_selector("#text1")
  32. assert text1.inner_text() == "10"
  33. page.wait_for_selector("#slider1")
  34. page.fill("#slider1 input", "20")
  35. function_evaluated = True
  36. try:
  37. page.wait_for_function("document.querySelector('#text1').innerText !== '10'")
  38. except Exception as e:
  39. function_evaluated = False
  40. logging.getLogger().debug(f"Function evaluation timeout.\n{e}")
  41. if function_evaluated:
  42. text1_2 = page.query_selector("#text1")
  43. assert text1_2.inner_text() == "20"
  44. @pytest.mark.teste2e
  45. def test_slider_action_on_change(page: "Page", gui: Gui, helpers):
  46. d = {"v1": 10, "v2": 10} # noqa: F841
  47. def on_change(state, var, val):
  48. if var == "d.v2":
  49. d = {"v1": 2 * val}
  50. state.d.update(d)
  51. page_md = """
  52. Value: <|{d.v1}|id=text1|>
  53. Slider: <|{d.v2}|slider|id=slider1|>
  54. """
  55. gui._set_frame(inspect.currentframe())
  56. gui.add_page(name="test", page=page_md)
  57. helpers.run_e2e(gui)
  58. page.goto("./test")
  59. page.expect_websocket()
  60. page.wait_for_selector("#text1")
  61. text1 = page.query_selector("#text1")
  62. assert text1.inner_text() == "10"
  63. page.wait_for_selector("#slider1")
  64. page.fill("#slider1 input", "20")
  65. function_evaluated = True
  66. try:
  67. page.wait_for_function("document.querySelector('#text1').innerText !== '10'")
  68. except Exception as e:
  69. function_evaluated = False
  70. logging.getLogger().debug(f"Function evaluation timeout.\n{e}")
  71. if function_evaluated:
  72. text1_2 = page.query_selector("#text1")
  73. assert text1_2.inner_text() == "40"