conftest.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. # NOTE favicon routes must be removed seperately because they are not "pages"
  31. [globals.app.routes.remove(r) for r in globals.app.routes if 'favicon' in r.path]
  32. importlib.reload(globals)
  33. globals.index_client = Client(page('/'), shared=True).__enter__()
  34. globals.app.get('/')(globals.index_client.build_response)
  35. @pytest.fixture(scope='session', autouse=True)
  36. def remove_all_screenshots() -> None:
  37. if os.path.exists(Screen.SCREENSHOT_DIR):
  38. for name in os.listdir(Screen.SCREENSHOT_DIR):
  39. os.remove(os.path.join(Screen.SCREENSHOT_DIR, name))
  40. @pytest.fixture
  41. def screen(selenium: webdriver.Chrome, request: pytest.FixtureRequest, caplog: pytest.LogCaptureFixture) \
  42. -> Generator[Screen, None, None]:
  43. screen = Screen(selenium, caplog)
  44. yield screen
  45. if screen.is_open:
  46. screen.shot(request.node.name)
  47. logs = screen.caplog.get_records('call')
  48. assert not logs, f'There were unexpected logs:\n-------\n{logs}\n-------'
  49. screen.stop_server()