test_notebook_simple_gui.py 5.2 KB

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