test_notebook_simple_gui.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Copyright 2021-2025 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 contextlib
  12. import time
  13. from urllib.request import urlopen
  14. import pytest
  15. from testbook import testbook
  16. @pytest.mark.filterwarnings("ignore::RuntimeWarning")
  17. @pytest.mark.teste2e
  18. @testbook("tests/gui/notebook/simple_gui.ipynb")
  19. def test_notebook_simple_gui(tb, helpers):
  20. tb.execute_cell("import")
  21. tb.execute_cell("page_declaration")
  22. tb.execute_cell("gui_init")
  23. tb.execute_cell("gui_run")
  24. while not helpers.port_check():
  25. time.sleep(0.1)
  26. assert ">Hello</h1>" in urlopen("http://127.0.0.1:5000/taipy-jsx/page1").read().decode("utf-8")
  27. assert 'defaultValue=\\"10\\"' in urlopen("http://127.0.0.1:5000/taipy-jsx/page1").read().decode("utf-8")
  28. # Test state manipulation within notebook
  29. tb.execute_cell("get_variable")
  30. assert "10" in tb.cell_output_text("get_variable")
  31. assert 'defaultValue=\\"10\\"' in urlopen("http://127.0.0.1:5000/taipy-jsx/page1").read().decode("utf-8")
  32. tb.execute_cell("set_variable")
  33. assert 'defaultValue=\\"20\\"' in urlopen("http://127.0.0.1:5000/taipy-jsx/page1").read().decode("utf-8")
  34. tb.execute_cell("re_get_variable")
  35. assert "20" in tb.cell_output_text("re_get_variable")
  36. # Test page reload
  37. tb.execute_cell("gui_stop")
  38. with pytest.raises(Exception) as exc_info:
  39. urlopen("http://127.0.0.1:5000/taipy-jsx/page1")
  40. assert "501: Gateway error" in str(exc_info.value)
  41. tb.execute_cell("gui_re_run")
  42. while True:
  43. with contextlib.suppress(Exception):
  44. urlopen("http://127.0.0.1:5000/taipy-jsx/page1")
  45. break
  46. assert ">Hello</h1>" in urlopen("http://127.0.0.1:5000/taipy-jsx/page1").read().decode("utf-8")
  47. tb.execute_cell("gui_reload")
  48. while True:
  49. with contextlib.suppress(Exception):
  50. urlopen("http://127.0.0.1:5000/taipy-jsx/page1")
  51. break
  52. assert ">Hello</h1>" in urlopen("http://127.0.0.1:5000/taipy-jsx/page1").read().decode("utf-8")
  53. tb.execute_cell("gui_re_stop")
  54. with pytest.raises(Exception) as exc_info:
  55. urlopen("http://127.0.0.1:5000/taipy-jsx/page1")
  56. assert "501: Gateway error" in str(exc_info.value)