test_notebook_simple_gui.py 5.3 KB

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