conftest.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import importlib
  2. import os
  3. import shutil
  4. from pathlib import Path
  5. from typing import Dict, Generator
  6. import icecream
  7. import pytest
  8. from selenium import webdriver
  9. from selenium.webdriver.chrome.service import Service
  10. from nicegui import Client, globals
  11. from nicegui.elements import plotly, pyplot
  12. from nicegui.page import page
  13. from .screen import Screen
  14. DOWNLOAD_DIR = Path(__file__).parent / 'download'
  15. icecream.install()
  16. @pytest.fixture
  17. def chrome_options(chrome_options: webdriver.ChromeOptions) -> webdriver.ChromeOptions:
  18. chrome_options.add_argument('headless')
  19. chrome_options.add_argument('disable-gpu')
  20. chrome_options.add_argument('window-size=600x600')
  21. chrome_options.add_experimental_option('prefs', {
  22. "download.default_directory": str(DOWNLOAD_DIR),
  23. "download.prompt_for_download": False, # To auto download the file
  24. "download.directory_upgrade": True,
  25. })
  26. return chrome_options
  27. @pytest.fixture
  28. def capabilities(capabilities: Dict) -> Dict:
  29. capabilities['goog:loggingPrefs'] = {'browser': 'ALL'}
  30. return capabilities
  31. @pytest.fixture(autouse=True)
  32. def reset_globals() -> Generator[None, None, None]:
  33. for path in {'/'}.union(globals.page_routes.values()):
  34. globals.app.remove_route(path)
  35. globals.app.openapi_schema = None
  36. globals.app.middleware_stack = None
  37. globals.app.user_middleware.clear()
  38. # NOTE favicon routes must be removed separately because they are not "pages"
  39. [globals.app.routes.remove(r) for r in globals.app.routes if r.path.endswith('/favicon.ico')]
  40. importlib.reload(globals)
  41. # repopulate globals.optional_features
  42. importlib.reload(plotly)
  43. importlib.reload(pyplot)
  44. globals.app.storage.clear()
  45. globals.index_client = Client(page('/'), shared=True).__enter__()
  46. globals.app.get('/')(globals.index_client.build_response)
  47. @pytest.fixture(scope='session', autouse=True)
  48. def remove_all_screenshots() -> None:
  49. if os.path.exists(Screen.SCREENSHOT_DIR):
  50. for name in os.listdir(Screen.SCREENSHOT_DIR):
  51. os.remove(os.path.join(Screen.SCREENSHOT_DIR, name))
  52. @pytest.fixture(scope='function')
  53. def driver(chrome_options: webdriver.ChromeOptions) -> webdriver.Chrome:
  54. s = Service()
  55. driver = webdriver.Chrome(service=s, options=chrome_options)
  56. driver.implicitly_wait(Screen.IMPLICIT_WAIT)
  57. driver.set_page_load_timeout(4)
  58. yield driver
  59. driver.quit()
  60. @pytest.fixture
  61. def screen(driver: webdriver.Chrome, request: pytest.FixtureRequest, caplog: pytest.LogCaptureFixture) \
  62. -> Generator[Screen, None, None]:
  63. screen = Screen(driver, caplog)
  64. yield screen
  65. if screen.is_open:
  66. screen.shot(request.node.name)
  67. logs = screen.caplog.get_records('call')
  68. assert not logs, f'There were unexpected logs:\n-------\n{logs}\n-------'
  69. screen.stop_server()
  70. if DOWNLOAD_DIR.exists():
  71. shutil.rmtree(DOWNLOAD_DIR)