test_html_rendering.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 inspect
  12. import os
  13. import time
  14. from importlib import util
  15. from pathlib import Path
  16. from urllib.request import urlopen
  17. import pytest
  18. if util.find_spec("playwright"):
  19. from playwright._impl._page import Page
  20. from taipy.gui import Gui, Html
  21. from taipy.gui.server import _Server
  22. @pytest.mark.teste2e
  23. def test_html_render_with_style(page: "Page", gui: Gui, helpers):
  24. html_content = """<!DOCTYPE html>
  25. <html lang="en">
  26. <head>
  27. <style>
  28. .taipy-text {
  29. color: green;
  30. }
  31. .custom-text {
  32. color: blue;
  33. }
  34. </style>
  35. </head>
  36. <body>
  37. <taipy:text id="text1">Hey</taipy:text>
  38. <taipy:text id="text2" class="custom-text">There</taipy:text>
  39. </body>
  40. </html>"""
  41. gui._set_frame(inspect.currentframe())
  42. gui.add_page("page1", Html(html_content))
  43. helpers.run_e2e(gui)
  44. page.goto("./page1")
  45. page.expect_websocket()
  46. page.wait_for_selector("#text1")
  47. retry = 0
  48. while (
  49. retry < 10
  50. and page.evaluate('window.getComputedStyle(document.querySelector("#text1"), null).getPropertyValue("color")')
  51. != "rgb(0, 128, 0)"
  52. ):
  53. retry += 1
  54. time.sleep(0.2)
  55. assert (
  56. page.evaluate('window.getComputedStyle(document.querySelector("#text1"), null).getPropertyValue("color")')
  57. == "rgb(0, 128, 0)"
  58. )
  59. assert (
  60. page.evaluate('window.getComputedStyle(document.querySelector("#text2"), null).getPropertyValue("color")')
  61. == "rgb(0, 0, 255)"
  62. )
  63. @pytest.mark.teste2e
  64. def test_html_render_bind_assets(page: "Page", gui: Gui, helpers, e2e_base_url, e2e_port):
  65. gui._set_frame(inspect.currentframe())
  66. gui.add_pages(pages=f"{Path(Path(__file__).parent.resolve())}{os.path.sep}test-assets")
  67. helpers.run_e2e(gui)
  68. assert ".taipy-text" in urlopen(
  69. f"http://127.0.0.1:{e2e_port}{e2e_base_url}test-assets/style/style.css"
  70. ).read().decode("utf-8")
  71. page.goto("./test-assets/page1")
  72. page.expect_websocket()
  73. page.wait_for_selector("#text1")
  74. retry = 0
  75. while (
  76. retry < 10
  77. and page.evaluate('window.getComputedStyle(document.querySelector("#text1"), null).getPropertyValue("color")')
  78. != "rgb(0, 128, 0)"
  79. ):
  80. retry += 1
  81. time.sleep(0.1)
  82. assert (
  83. page.evaluate('window.getComputedStyle(document.querySelector("#text1"), null).getPropertyValue("color")')
  84. == "rgb(0, 128, 0)"
  85. )
  86. assert (
  87. page.evaluate('window.getComputedStyle(document.querySelector("#text2"), null).getPropertyValue("color")')
  88. == "rgb(0, 0, 255)"
  89. )
  90. @pytest.mark.teste2e
  91. def test_html_render_path_mapping(page: "Page", gui: Gui, helpers, e2e_base_url, e2e_port):
  92. gui._server = _Server(
  93. gui,
  94. path_mapping={"style": f"{Path(Path(__file__).parent.resolve())}{os.path.sep}test-assets{os.path.sep}style"},
  95. flask=gui._flask,
  96. async_mode="gevent",
  97. )
  98. gui.add_page("page1", Html(f"{Path(Path(__file__).parent.resolve())}{os.path.sep}page1.html"))
  99. helpers.run_e2e(gui)
  100. assert ".taipy-text" in urlopen(f"http://127.0.0.1:{e2e_port}{e2e_base_url}/style/style.css").read().decode("utf-8")
  101. page.goto("./page1")
  102. page.expect_websocket()
  103. page.wait_for_selector("#text1")
  104. retry = 0
  105. while (
  106. retry < 10
  107. and page.evaluate('window.getComputedStyle(document.querySelector("#text1"), null).getPropertyValue("color")')
  108. != "rgb(0, 128, 0)"
  109. ):
  110. retry += 1
  111. time.sleep(0.1)
  112. assert (
  113. page.evaluate('window.getComputedStyle(document.querySelector("#text1"), null).getPropertyValue("color")')
  114. == "rgb(0, 128, 0)"
  115. )
  116. assert (
  117. page.evaluate('window.getComputedStyle(document.querySelector("#text2"), null).getPropertyValue("color")')
  118. == "rgb(0, 0, 255)"
  119. )