conftest.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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, app, binding, core
  11. from nicegui.page import page
  12. from .screen import Screen
  13. DOWNLOAD_DIR = Path(__file__).parent / 'download'
  14. icecream.install()
  15. @pytest.fixture
  16. def chrome_options(chrome_options: webdriver.ChromeOptions) -> webdriver.ChromeOptions:
  17. chrome_options.add_argument('disable-dev-shm-using')
  18. chrome_options.add_argument('no-sandbox')
  19. chrome_options.add_argument('headless')
  20. chrome_options.add_argument('disable-gpu')
  21. chrome_options.add_argument('window-size=600x600')
  22. chrome_options.add_experimental_option('prefs', {
  23. "download.default_directory": str(DOWNLOAD_DIR),
  24. "download.prompt_for_download": False, # To auto download the file
  25. "download.directory_upgrade": True,
  26. })
  27. if 'CHROME_BINARY_LOCATION' in os.environ:
  28. chrome_options.binary_location = os.environ['CHROME_BINARY_LOCATION']
  29. return chrome_options
  30. @pytest.fixture
  31. def capabilities(capabilities: Dict) -> Dict:
  32. capabilities['goog:loggingPrefs'] = {'browser': 'ALL'}
  33. return capabilities
  34. @pytest.fixture(autouse=True)
  35. def reset_globals() -> Generator[None, None, None]:
  36. for path in {'/'}.union(Client.page_routes.values()):
  37. app.remove_route(path)
  38. app.openapi_schema = None
  39. app.middleware_stack = None
  40. app.user_middleware.clear()
  41. # NOTE favicon routes must be removed separately because they are not "pages"
  42. for route in app.routes:
  43. if route.path.endswith('/favicon.ico'):
  44. app.routes.remove(route)
  45. importlib.reload(core)
  46. Client.instances.clear()
  47. Client.page_routes.clear()
  48. Client.index_client = Client(page('/'), shared=True).__enter__()
  49. app.reset()
  50. app.get('/')(Client.index_client.build_response)
  51. binding.reset()
  52. @pytest.fixture(scope='session', autouse=True)
  53. def remove_all_screenshots() -> None:
  54. if os.path.exists(Screen.SCREENSHOT_DIR):
  55. for name in os.listdir(Screen.SCREENSHOT_DIR):
  56. os.remove(os.path.join(Screen.SCREENSHOT_DIR, name))
  57. @pytest.fixture(scope='function')
  58. def driver(chrome_options: webdriver.ChromeOptions) -> webdriver.Chrome:
  59. s = Service()
  60. driver_ = webdriver.Chrome(service=s, options=chrome_options)
  61. driver_.implicitly_wait(Screen.IMPLICIT_WAIT)
  62. driver_.set_page_load_timeout(4)
  63. yield driver_
  64. driver_.quit()
  65. @pytest.fixture
  66. def screen(driver: webdriver.Chrome, request: pytest.FixtureRequest, caplog: pytest.LogCaptureFixture) \
  67. -> Generator[Screen, None, None]:
  68. screen_ = Screen(driver, caplog)
  69. yield screen_
  70. if screen_.is_open:
  71. screen_.shot(request.node.name)
  72. logs = screen_.caplog.get_records('call')
  73. screen_.stop_server()
  74. if DOWNLOAD_DIR.exists():
  75. shutil.rmtree(DOWNLOAD_DIR)
  76. if logs:
  77. pytest.fail('There were unexpected logs. See "Captured log call" below.', pytrace=False)