browser_check.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. # Copyright (c) Jupyter Development Team.
  2. # Distributed under the terms of the Modified BSD License.
  3. """
  4. This module is meant to run JupyterLab in a headless browser, making sure
  5. the application launches and starts up without errors.
  6. """
  7. import asyncio
  8. import inspect
  9. import logging
  10. import os
  11. import shutil
  12. import subprocess
  13. import sys
  14. import time
  15. from concurrent.futures import ThreadPoolExecutor
  16. from os import path as osp
  17. from jupyter_server.serverapp import aliases, flags
  18. from jupyter_server.utils import pathname2url, urljoin
  19. from tornado.ioloop import IOLoop
  20. from tornado.iostream import StreamClosedError
  21. from tornado.websocket import WebSocketClosedError
  22. from traitlets import Bool, Unicode
  23. from .labapp import LabApp, get_app_dir
  24. from .tests.test_app import TestEnv
  25. here = osp.abspath(osp.dirname(__file__))
  26. test_flags = dict(flags)
  27. test_flags["core-mode"] = ({"BrowserApp": {"core_mode": True}}, "Start the app in core mode.")
  28. test_flags["dev-mode"] = ({"BrowserApp": {"dev_mode": True}}, "Start the app in dev mode.")
  29. test_flags["watch"] = ({"BrowserApp": {"watch": True}}, "Start the app in watch mode.")
  30. test_aliases = dict(aliases)
  31. test_aliases["app-dir"] = "BrowserApp.app_dir"
  32. class LogErrorHandler(logging.StreamHandler):
  33. """A handler that exits with 1 on a logged error."""
  34. def __init__(self):
  35. super().__init__(stream=sys.stderr)
  36. self.setLevel(logging.ERROR)
  37. self.errored = False
  38. def filter(self, record):
  39. # Handle known StreamClosedError from Tornado
  40. # These occur when we forcibly close Websockets or
  41. # browser connections during the test.
  42. # https://github.com/tornadoweb/tornado/issues/2834
  43. if (
  44. hasattr(record, "exc_info")
  45. and record.exc_info is not None
  46. and isinstance(record.exc_info[1], (StreamClosedError, WebSocketClosedError))
  47. ):
  48. return False
  49. return super().filter(record)
  50. def emit(self, record):
  51. self.errored = True
  52. super().emit(record)
  53. def run_test(app, func):
  54. """Synchronous entry point to run a test function.
  55. func is a function that accepts an app url as a parameter and returns a result.
  56. func can be synchronous or asynchronous. If it is synchronous, it will be run
  57. in a thread, so asynchronous is preferred.
  58. """
  59. IOLoop.current().spawn_callback(run_test_async, app, func)
  60. async def run_test_async(app, func):
  61. """Run a test against the application.
  62. func is a function that accepts an app url as a parameter and returns a result.
  63. func can be synchronous or asynchronous. If it is synchronous, it will be run
  64. in a thread, so asynchronous is preferred.
  65. """
  66. handler = LogErrorHandler()
  67. app.log.addHandler(handler)
  68. env_patch = TestEnv()
  69. env_patch.start()
  70. app.log.info("Running async test")
  71. # The entry URL for browser tests is different in notebook >= 6.0,
  72. # since that uses a local HTML file to point the user at the app.
  73. if hasattr(app, "browser_open_file"):
  74. url = urljoin("file:", pathname2url(app.browser_open_file))
  75. else:
  76. url = app.display_url
  77. # Allow a synchronous function to be passed in.
  78. if inspect.iscoroutinefunction(func):
  79. test = func(url)
  80. else:
  81. app.log.info("Using thread pool executor to run test")
  82. loop = asyncio.get_event_loop()
  83. executor = ThreadPoolExecutor()
  84. task = loop.run_in_executor(executor, func, url)
  85. test = asyncio.wait([task])
  86. try:
  87. await test
  88. except Exception as e:
  89. app.log.critical("Caught exception during the test:")
  90. app.log.error(str(e))
  91. app.log.info("Test Complete")
  92. result = 0
  93. if handler.errored:
  94. result = 1
  95. app.log.critical("Exiting with 1 due to errors")
  96. else:
  97. app.log.info("Exiting normally")
  98. app.log.info("Stopping server...")
  99. try:
  100. app.http_server.stop()
  101. app.io_loop.stop()
  102. env_patch.stop()
  103. except Exception as e:
  104. app.log.error(str(e))
  105. result = 1
  106. finally:
  107. time.sleep(2)
  108. os._exit(result)
  109. async def run_async_process(cmd, **kwargs):
  110. """Run an asynchronous command"""
  111. proc = await asyncio.create_subprocess_exec(*cmd, **kwargs)
  112. stdout, stderr = await proc.communicate()
  113. if proc.returncode != 0:
  114. raise RuntimeError(str(cmd) + " exited with " + str(proc.returncode))
  115. return stdout, stderr
  116. async def run_browser(url):
  117. """Run the browser test and return an exit code."""
  118. browser = os.environ.get("JLAB_BROWSER_TYPE", "chromium")
  119. if browser not in {"chromium", "firefox", "webkit"}:
  120. browser = "chromium"
  121. target = osp.join(get_app_dir(), "browser_test")
  122. if not osp.exists(osp.join(target, "node_modules")):
  123. if not osp.exists(target):
  124. os.makedirs(osp.join(target))
  125. await run_async_process(["npm", "init", "-y"], cwd=target)
  126. await run_async_process(["npm", "install", "playwright@^1.9.2"], cwd=target)
  127. await run_async_process(["npx", "playwright", "install", browser], cwd=target)
  128. shutil.copy(osp.join(here, "browser-test.js"), osp.join(target, "browser-test.js"))
  129. await run_async_process(["node", "browser-test.js", url], cwd=target)
  130. def run_browser_sync(url):
  131. """Run the browser test and return an exit code."""
  132. browser = os.environ.get("JLAB_BROWSER_TYPE", "chromium")
  133. if browser not in {"chromium", "firefox", "webkit"}:
  134. browser = "chromium"
  135. target = osp.join(get_app_dir(), "browser_test")
  136. if not osp.exists(osp.join(target, "node_modules")):
  137. os.makedirs(target)
  138. subprocess.call(["npm", "init", "-y"], cwd=target) # noqa S603 S607
  139. subprocess.call(["npm", "install", "playwright@^1.9.2"], cwd=target) # noqa S603 S607
  140. subprocess.call(["npx", "playwright", "install", browser], cwd=target) # noqa S603 S607
  141. shutil.copy(osp.join(here, "browser-test.js"), osp.join(target, "browser-test.js"))
  142. return subprocess.check_call(["node", "browser-test.js", url], cwd=target) # noqa S603 S607
  143. class BrowserApp(LabApp):
  144. """An app the launches JupyterLab and waits for it to start up, checking for
  145. JS console errors, JS errors, and Python logged errors.
  146. """
  147. name = __name__
  148. open_browser = False
  149. serverapp_config = {"base_url": "/foo/"}
  150. default_url = Unicode("/lab?reset", config=True, help="The default URL to redirect to from `/`")
  151. ip = "127.0.0.1"
  152. flags = test_flags
  153. aliases = test_aliases
  154. test_browser = Bool(True)
  155. def initialize_settings(self):
  156. self.settings.setdefault("page_config_data", {})
  157. self.settings["page_config_data"]["browserTest"] = True
  158. self.settings["page_config_data"]["buildAvailable"] = False
  159. self.settings["page_config_data"]["exposeAppInBrowser"] = True
  160. super().initialize_settings()
  161. def initialize_handlers(self):
  162. def func(*args, **kwargs):
  163. return 0
  164. if self.test_browser:
  165. func = run_browser_sync if os.name == "nt" else run_browser
  166. run_test(self.serverapp, func)
  167. super().initialize_handlers()
  168. def _jupyter_server_extension_points():
  169. return [{"module": __name__, "app": BrowserApp}]
  170. def _jupyter_server_extension_paths():
  171. return [{"module": "jupyterlab.browser_check"}]
  172. if __name__ == "__main__":
  173. skip_options = ["--no-browser-test", "--no-chrome-test"]
  174. for option in skip_options:
  175. if option in sys.argv:
  176. BrowserApp.test_browser = False
  177. sys.argv.remove(option)
  178. BrowserApp.launch_instance()