screen_plugin.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import os
  2. import shutil
  3. from pathlib import Path
  4. from typing import Dict, Generator
  5. import pytest
  6. from selenium import webdriver
  7. from selenium.webdriver.chrome.service import Service
  8. from .general_fixtures import ( # noqa: F401 # pylint: disable=unused-import
  9. nicegui_reset_globals,
  10. prepare_simulation,
  11. pytest_configure,
  12. )
  13. from .screen import Screen
  14. # pylint: disable=redefined-outer-name
  15. DOWNLOAD_DIR = Path(__file__).parent / 'download'
  16. @pytest.fixture
  17. def nicegui_chrome_options(chrome_options: webdriver.ChromeOptions) -> webdriver.ChromeOptions:
  18. """Configure the Chrome options for the NiceGUI tests."""
  19. chrome_options.add_argument('disable-dev-shm-usage')
  20. chrome_options.add_argument('no-sandbox')
  21. chrome_options.add_argument('headless')
  22. chrome_options.add_argument('disable-gpu' if 'GITHUB_ACTIONS' in os.environ else '--use-gl=angle')
  23. chrome_options.add_argument('window-size=600x600')
  24. chrome_options.add_experimental_option('prefs', {
  25. 'download.default_directory': str(DOWNLOAD_DIR),
  26. 'download.prompt_for_download': False, # To auto download the file
  27. 'download.directory_upgrade': True,
  28. })
  29. if 'CHROME_BINARY_LOCATION' in os.environ:
  30. chrome_options.binary_location = os.environ['CHROME_BINARY_LOCATION']
  31. return chrome_options
  32. @pytest.fixture
  33. def capabilities(capabilities: Dict) -> Dict:
  34. """Configure the Chrome driver capabilities."""
  35. capabilities['goog:loggingPrefs'] = {'browser': 'ALL'}
  36. return capabilities
  37. @pytest.fixture(scope='session')
  38. def nicegui_remove_all_screenshots() -> None:
  39. """Remove all screenshots from the screenshot directory before the test session."""
  40. if os.path.exists(Screen.SCREENSHOT_DIR):
  41. for name in os.listdir(Screen.SCREENSHOT_DIR):
  42. os.remove(os.path.join(Screen.SCREENSHOT_DIR, name))
  43. @pytest.fixture()
  44. def nicegui_driver(nicegui_chrome_options: webdriver.ChromeOptions) -> Generator[webdriver.Chrome, None, None]:
  45. """Create a new Chrome driver instance."""
  46. s = Service()
  47. driver_ = webdriver.Chrome(service=s, options=nicegui_chrome_options)
  48. driver_.implicitly_wait(Screen.IMPLICIT_WAIT)
  49. driver_.set_page_load_timeout(4)
  50. yield driver_
  51. driver_.quit()
  52. @pytest.fixture
  53. def screen(nicegui_reset_globals, # noqa: F811, pylint: disable=unused-argument
  54. nicegui_remove_all_screenshots, # pylint: disable=unused-argument
  55. nicegui_driver: webdriver.Chrome,
  56. request: pytest.FixtureRequest,
  57. caplog: pytest.LogCaptureFixture,
  58. ) -> Generator[Screen, None, None]:
  59. """Create a new SeleniumScreen fixture."""
  60. prepare_simulation(request)
  61. screen_ = Screen(nicegui_driver, caplog)
  62. yield screen_
  63. logs = screen_.caplog.get_records('call')
  64. if screen_.is_open:
  65. screen_.shot(request.node.name)
  66. screen_.stop_server()
  67. if DOWNLOAD_DIR.exists():
  68. shutil.rmtree(DOWNLOAD_DIR)
  69. if logs:
  70. pytest.fail('There were unexpected logs. See "Captured log call" below.', pytrace=False)