test_button_action.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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_button_action(page: "Page", gui: Gui, helpers):
  20. page_md = """
  21. <|{x}|id=text1|>
  22. <|Action|button|on_action=do_something_fn|id=button1|>
  23. """
  24. x = 10 # noqa: F841
  25. def do_something_fn(state):
  26. state.x = state.x * 2
  27. gui._set_frame(inspect.currentframe())
  28. gui.add_page(name="test", page=page_md)
  29. helpers.run_e2e(gui)
  30. page.goto("./test")
  31. page.expect_websocket()
  32. page.wait_for_selector("#text1")
  33. text1 = page.query_selector("#text1")
  34. assert text1.inner_text() == "10"
  35. page.click("#button1")
  36. function_evaluated = True
  37. try:
  38. page.wait_for_function("document.querySelector('#text1').innerText !== '10'")
  39. except Exception as e:
  40. function_evaluated = False
  41. logging.getLogger().debug(f"Function evaluation timeout.\n{e}")
  42. if function_evaluated:
  43. text1_2 = page.query_selector("#text1")
  44. assert text1_2.inner_text() == "20"