test_notebook_simple_gui.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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.skip_if_not_server("flask")
  17. @pytest.mark.filterwarnings("ignore::RuntimeWarning")
  18. @pytest.mark.teste2e
  19. @testbook("tests/gui/notebook/simple_gui.ipynb")
  20. def test_notebook_simple_gui(tb, helpers):
  21. tb.execute_cell("import")
  22. tb.execute_cell("page_declaration")
  23. tb.execute_cell("gui_init")
  24. tb.execute_cell("gui_run")
  25. while not helpers.port_check():
  26. time.sleep(1)
  27. assert ">Hello</h1>" in urlopen("http://127.0.0.1:5000/taipy-jsx/page1").read().decode("utf-8")
  28. assert 'defaultValue=\\"10\\"' in urlopen("http://127.0.0.1:5000/taipy-jsx/page1").read().decode("utf-8")
  29. # Test state manipulation within notebook
  30. tb.execute_cell("get_variable")
  31. assert "10" in tb.cell_output_text("get_variable")
  32. assert 'defaultValue=\\"10\\"' in urlopen("http://127.0.0.1:5000/taipy-jsx/page1").read().decode("utf-8")
  33. tb.execute_cell("set_variable")
  34. assert 'defaultValue=\\"20\\"' in urlopen("http://127.0.0.1:5000/taipy-jsx/page1").read().decode("utf-8")
  35. tb.execute_cell("re_get_variable")
  36. assert "20" in tb.cell_output_text("re_get_variable")
  37. # Test page reload
  38. tb.execute_cell("gui_stop")
  39. with pytest.raises(Exception) as exc_info:
  40. urlopen("http://127.0.0.1:5000/taipy-jsx/page1")
  41. assert "501: Gateway error" in str(exc_info.value)
  42. tb.execute_cell("gui_re_run")
  43. while True:
  44. with contextlib.suppress(Exception):
  45. urlopen("http://127.0.0.1:5000/taipy-jsx/page1")
  46. break
  47. assert ">Hello</h1>" in urlopen("http://127.0.0.1:5000/taipy-jsx/page1").read().decode("utf-8")
  48. tb.execute_cell("gui_reload")
  49. while True:
  50. with contextlib.suppress(Exception):
  51. urlopen("http://127.0.0.1:5000/taipy-jsx/page1")
  52. break
  53. assert ">Hello</h1>" in urlopen("http://127.0.0.1:5000/taipy-jsx/page1").read().decode("utf-8")
  54. tb.execute_cell("gui_re_stop")
  55. with pytest.raises(Exception) as exc_info:
  56. urlopen("http://127.0.0.1:5000/taipy-jsx/page1")
  57. assert "501: Gateway error" in str(exc_info.value)
  58. @pytest.mark.skip_if_not_server("fastapi")
  59. @pytest.mark.filterwarnings("ignore::RuntimeWarning")
  60. @pytest.mark.teste2e
  61. @testbook("tests/gui/notebook/simple_gui_fastapi.ipynb")
  62. def test_notebook_simple_gui_fastapi(tb, helpers):
  63. tb.execute_cell("import")
  64. tb.execute_cell("page_declaration")
  65. tb.execute_cell("gui_init")
  66. tb.execute_cell("gui_run")
  67. while not helpers.port_check():
  68. time.sleep(1)
  69. assert ">Hello</h1>" in urlopen("http://127.0.0.1:5000/taipy-jsx/page1").read().decode("utf-8")
  70. assert 'defaultValue=\\"10\\"' in urlopen("http://127.0.0.1:5000/taipy-jsx/page1").read().decode("utf-8")
  71. # Test state manipulation within notebook
  72. tb.execute_cell("get_variable")
  73. assert "10" in tb.cell_output_text("get_variable")
  74. assert 'defaultValue=\\"10\\"' in urlopen("http://127.0.0.1:5000/taipy-jsx/page1").read().decode("utf-8")
  75. tb.execute_cell("set_variable")
  76. assert 'defaultValue=\\"20\\"' in urlopen("http://127.0.0.1:5000/taipy-jsx/page1").read().decode("utf-8")
  77. tb.execute_cell("re_get_variable")
  78. assert "20" in tb.cell_output_text("re_get_variable")
  79. # Test page reload
  80. tb.execute_cell("gui_stop")
  81. with pytest.raises(Exception) as exc_info:
  82. urlopen("http://127.0.0.1:5000/taipy-jsx/page1")
  83. assert "refused" in str(exc_info.value)
  84. tb.execute_cell("gui_re_run")
  85. while True:
  86. with contextlib.suppress(Exception):
  87. urlopen("http://127.0.0.1:5000/taipy-jsx/page1")
  88. break
  89. assert ">Hello</h1>" in urlopen("http://127.0.0.1:5000/taipy-jsx/page1").read().decode("utf-8")
  90. tb.execute_cell("gui_reload")
  91. while True:
  92. with contextlib.suppress(Exception):
  93. urlopen("http://127.0.0.1:5000/taipy-jsx/page1")
  94. break
  95. assert ">Hello</h1>" in urlopen("http://127.0.0.1:5000/taipy-jsx/page1").read().decode("utf-8")
  96. tb.execute_cell("gui_re_stop")
  97. with pytest.raises(Exception) as exc_info:
  98. urlopen("http://127.0.0.1:5000/taipy-jsx/page1")
  99. assert "refused" in str(exc_info.value)