selenium_check.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # -*- coding: utf-8 -*-
  2. from __future__ import print_function, absolute_import
  3. import os
  4. import sys
  5. import time
  6. import threading
  7. from tornado import ioloop
  8. from notebook.notebookapp import NotebookApp, flags, aliases
  9. from traitlets import Bool, Unicode
  10. from jupyterlab_launcher import LabConfig, add_handlers
  11. from selenium import webdriver
  12. from .commands import get_app_dir
  13. here = os.path.dirname(__file__)
  14. test_flags = dict(flags)
  15. test_flags['core-mode'] = (
  16. {'TestApp': {'core_mode': True}},
  17. "Start the app in core mode."
  18. )
  19. test_aliases = dict(aliases)
  20. test_aliases['app-dir'] = 'TestApp.app_dir'
  21. class TestApp(NotebookApp):
  22. default_url = Unicode('/lab')
  23. open_browser = Bool(False)
  24. base_url = '/foo'
  25. flags = test_flags
  26. aliases = test_aliases
  27. core_mode = Bool(False, config=True,
  28. help="Whether to start the app in core mode")
  29. app_dir = Unicode('', config=True,
  30. help="The app directory to build in")
  31. def start(self):
  32. self.io_loop = ioloop.IOLoop.current()
  33. config = LabConfig()
  34. if self.core_mode:
  35. config.assets_dir = os.path.join(here, 'build')
  36. elif self.app_dir:
  37. config.assets_dir = os.path.join(self.app_dir, 'static')
  38. else:
  39. config.assets_dir = os.path.join(get_app_dir(), 'static')
  40. print('****Testing assets dir %s' % config.assets_dir)
  41. config.settings_dir = ''
  42. add_handlers(self.web_app, config)
  43. self.io_loop.call_later(1, self._run_selenium)
  44. super(TestApp, self).start()
  45. def _run_selenium(self):
  46. thread = threading.Thread(target=run_selenium,
  47. args=(self.display_url, self._selenium_finished))
  48. thread.start()
  49. def _selenium_finished(self, result):
  50. self.io_loop.add_callback(lambda: sys.exit(result))
  51. def run_selenium(url, callback):
  52. """Run the selenium test and call the callback with the exit code.exit
  53. """
  54. print('Starting Firefox Driver')
  55. driver = webdriver.Firefox()
  56. print('Navigating to page:', url)
  57. driver.get(url)
  58. completed = False
  59. # Start a poll loop.
  60. t0 = time.time()
  61. while time.time() - t0 < 10:
  62. el = driver.find_element_by_id('main')
  63. if el:
  64. completed = True
  65. break
  66. # Avoid hogging the main thread.
  67. time.sleep(0.5)
  68. driver.quit()
  69. # Return the exit code.
  70. if not completed:
  71. callback(1)
  72. else:
  73. if os.path.exists('./geckodriver.log'):
  74. os.remove('./geckodriver.log')
  75. callback(0)
  76. if __name__ == '__main__':
  77. TestApp.launch_instance()