test_html_rendering.py 4.8 KB

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