test_notebook_simple_gui.py 2.6 KB

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