conftest.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import importlib
  2. import os
  3. from typing import Dict, Generator
  4. import icecream
  5. import pytest
  6. from selenium import webdriver
  7. from nicegui import Client, globals
  8. from nicegui.page import page
  9. from .screen import Screen
  10. icecream.install()
  11. @pytest.fixture
  12. def chrome_options(chrome_options: webdriver.ChromeOptions) -> webdriver.ChromeOptions:
  13. chrome_options.add_argument('headless')
  14. chrome_options.add_argument('disable-gpu')
  15. chrome_options.add_argument('window-size=600x600')
  16. return chrome_options
  17. @pytest.fixture
  18. def capabilities(capabilities: Dict) -> Dict:
  19. capabilities['goog:loggingPrefs'] = {'browser': 'ALL'}
  20. return capabilities
  21. @pytest.fixture
  22. def selenium(selenium: webdriver.Chrome) -> webdriver.Chrome:
  23. selenium.implicitly_wait(Screen.IMPLICIT_WAIT)
  24. selenium.set_page_load_timeout(4)
  25. return selenium
  26. @pytest.fixture(autouse=True)
  27. def reset_globals() -> Generator[None, None, None]:
  28. for path in {'/'}.union(globals.page_routes.values()):
  29. globals.app.remove_route(path)
  30. globals.app.middleware_stack = None
  31. importlib.reload(globals)
  32. globals.index_client = Client(page('/'), shared=True).__enter__()
  33. globals.app.get('/')(globals.index_client.build_response)
  34. @pytest.fixture(scope='session', autouse=True)
  35. def remove_all_screenshots() -> None:
  36. if os.path.exists(Screen.SCREENSHOT_DIR):
  37. for name in os.listdir(Screen.SCREENSHOT_DIR):
  38. os.remove(os.path.join(Screen.SCREENSHOT_DIR, name))
  39. @pytest.fixture
  40. def screen(selenium: webdriver.Chrome, request: pytest.FixtureRequest, caplog: pytest.LogCaptureFixture) \
  41. -> Generator[Screen, None, None]:
  42. screen = Screen(selenium, caplog)
  43. yield screen
  44. if screen.is_open:
  45. screen.shot(request.node.name)
  46. logs = screen.caplog.get_records('call')
  47. assert not logs, f'There were unexpected logs:\n-------\n{logs}\n-------'
  48. screen.stop_server()