Skip to content
Snippets Groups Projects
Commit 86ca53c5 authored by Brandon Rodriguez's avatar Brandon Rodriguez
Browse files

Correct selenium handling for firefox and also Channels TestCaseClass

parent 0c2045c6
Branches
Tags
No related merge requests found
...@@ -12,9 +12,14 @@ from selenium import webdriver ...@@ -12,9 +12,14 @@ from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.firefox.service import Service as FireFoxService from selenium.webdriver.firefox.service import Service as FireFoxService
from channels.testing import ChannelsLiveServerTestCase as DjangoChannelsLiveServerTestCase from channels.testing import ChannelsLiveServerTestCase as DjangoChannelsLiveServerTestCase
from django.conf import settings
# Internal Imports. # Internal Imports.
from django_expanded_test_cases.constants import (
ETC_SELENIUM_BROWSER,
ETC_SELENIUM_HEADLESS,
ETC_SELENIUM_DISABLE_CACHE,
ETC_SELENIUM_EXTRA_BROWSER_OPTIONS,
)
from django_expanded_test_cases.mixins.live_server_mixin import LiveServerMixin from django_expanded_test_cases.mixins.live_server_mixin import LiveServerMixin
...@@ -34,7 +39,7 @@ class ChannelsLiveServerTestCase(DjangoChannelsLiveServerTestCase, LiveServerMix ...@@ -34,7 +39,7 @@ class ChannelsLiveServerTestCase(DjangoChannelsLiveServerTestCase, LiveServerMix
cls._options = None cls._options = None
# Import/Initialize some values based on chosen testing browser. Default to chrome. # Import/Initialize some values based on chosen testing browser. Default to chrome.
cls._browser = str(getattr(settings, 'SELENIUM_TEST_BROWSER', 'chrome')).lower() cls._browser = str(ETC_SELENIUM_BROWSER).lower()
if cls._browser in ['chrome', 'chromium']: if cls._browser in ['chrome', 'chromium']:
# Setup for Chrome/Chromium browser. # Setup for Chrome/Chromium browser.
...@@ -44,20 +49,6 @@ class ChannelsLiveServerTestCase(DjangoChannelsLiveServerTestCase, LiveServerMix ...@@ -44,20 +49,6 @@ class ChannelsLiveServerTestCase(DjangoChannelsLiveServerTestCase, LiveServerMix
# Attempt driver auto-install, if webdriver_manager package is present. # Attempt driver auto-install, if webdriver_manager package is present.
cls._service = ChromeService() cls._service = ChromeService()
# Set required options to prevent crashing.
chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2})
chromeOptions.add_argument("--no-sandbox")
chromeOptions.add_argument("--disable-setuid-sandbox")
chromeOptions.add_argument("--remote-debugging-port=9222")
chromeOptions.add_argument("--disable-dev-shm-using")
chromeOptions.add_argument("--disable-extensions")
chromeOptions.add_argument("--disable-gpu")
chromeOptions.add_argument("disable-infobars")
# Save options.
cls._options = chromeOptions
except ModuleNotFoundError: except ModuleNotFoundError:
# Fall back to manual installation handling. # Fall back to manual installation handling.
...@@ -69,6 +60,28 @@ class ChannelsLiveServerTestCase(DjangoChannelsLiveServerTestCase, LiveServerMix ...@@ -69,6 +60,28 @@ class ChannelsLiveServerTestCase(DjangoChannelsLiveServerTestCase, LiveServerMix
# For Chromium. # For Chromium.
cls._service = ChromeService(executable_path='/usr/local/share/chromedriver') cls._service = ChromeService(executable_path='/usr/local/share/chromedriver')
# Set required chrome options.
chromeOptions = webdriver.ChromeOptions()
# Disable any existing extensions on local chrome setup, for consistent test runs across machines.
chromeOptions.add_argument('--disable-extensions')
# Add any user-provided options.
if ETC_SELENIUM_EXTRA_BROWSER_OPTIONS:
for browser_option in ETC_SELENIUM_EXTRA_BROWSER_OPTIONS:
chromeOptions.add_argument(browser_option)
# TODO: Document these? Seemed to come up a lot in googling errors and whatnot.
# # Avoid possible error in certain development environments about resource limits.
# # Error is along the lines of "DevToolsActivePort file doesn't exist".
# # See https://stackoverflow.com/a/69175552
# chromeOptions.add_argument('--disable-dev-shm-using')
# # Avoid possible error when many drivers are opened.
# # See https://stackoverflow.com/a/56638103
# chromeOptions.add_argument("--remote-debugging-port=9222")
# Save options.
cls._options = chromeOptions
# Everything else should handle the same for both. # Everything else should handle the same for both.
cls._browser = 'chrome' cls._browser = 'chrome'
...@@ -78,17 +91,34 @@ class ChannelsLiveServerTestCase(DjangoChannelsLiveServerTestCase, LiveServerMix ...@@ -78,17 +91,34 @@ class ChannelsLiveServerTestCase(DjangoChannelsLiveServerTestCase, LiveServerMix
# Setup browser driver to launch browser with. # Setup browser driver to launch browser with.
try: try:
# Attempt driver auto-install, if webdriver_manager package is present. # Attempt driver auto-install, if webdriver_manager package is present.
from webdriver_manager.firefox import GeckoDriverManager cls._service = FireFoxService()
cls._service = FireFoxService(executable_path=GeckoDriverManager().install())
except ModuleNotFoundError: except ModuleNotFoundError:
# Fall back to manual installation handling. # Fall back to manual installation handling.
cls._service = FireFoxService(executable_path='/usr/bin/geckodriver') cls._service = FireFoxService(executable_path='/usr/bin/geckodriver')
# Set required chrome options.
firefoxOptions = webdriver.FirefoxOptions()
# Add any user-provided options.
if ETC_SELENIUM_EXTRA_BROWSER_OPTIONS:
for browser_option in ETC_SELENIUM_EXTRA_BROWSER_OPTIONS:
firefoxOptions.add_argument(browser_option)
# Save options.
cls._options = firefoxOptions
else: else:
raise ValueError('Unknown browser "{0}".'.format(cls._browser)) raise ValueError('Unknown browser "{0}".'.format(cls._browser))
# Create initial testing driver. # Add universal options based on project settings.
cls.create_driver(cls) if ETC_SELENIUM_HEADLESS:
cls._options.add_argument('headless')
if ETC_SELENIUM_DISABLE_CACHE:
cls._options.add_argument('disable-application-cache')
# Create initial testing driver, one for each test.
cls.driver = cls.create_driver(cls)
def setUp(self): def setUp(self):
# Run parent setup logic. # Run parent setup logic.
...@@ -103,9 +133,20 @@ class ChannelsLiveServerTestCase(DjangoChannelsLiveServerTestCase, LiveServerMix ...@@ -103,9 +133,20 @@ class ChannelsLiveServerTestCase(DjangoChannelsLiveServerTestCase, LiveServerMix
# Run parent logic. # Run parent logic.
return super().subTest(*args, **kwargs) return super().subTest(*args, **kwargs)
@classmethod
def tearDownClass(cls):
# Close all remaining driver instances for class.
while len(cls._driver_set) > 0:
cls.close_driver(cls, cls._driver_set[0])
# Call parent teardown logic.
super().tearDownClass()
def tearDown(self): def tearDown(self):
# Close all remaining browser instances for test. # TODO: Below seems probably unnecessary? Research more.
self.close_all_drivers() # # Close all remaining window instances for test.
# # (Or at least attempt to for default driver for test).
# self.close_all_windows(self.driver)
# Call parent teardown logic. # Call parent teardown logic.
super().tearDown() super().tearDown()
......
...@@ -49,28 +49,6 @@ class LiveServerTestCase(DjangoLiveServerTestCase, LiveServerMixin): ...@@ -49,28 +49,6 @@ class LiveServerTestCase(DjangoLiveServerTestCase, LiveServerMixin):
# Attempt driver auto-install, if webdriver_manager package is present. # Attempt driver auto-install, if webdriver_manager package is present.
cls._service = ChromeService() cls._service = ChromeService()
# Set required options to prevent crashing.
chromeOptions = webdriver.ChromeOptions()
# Disable any existing extensions on local chrome setup, for consistent test runs across machines.
chromeOptions.add_argument('--disable-extensions')
# Add any user-provided options.
if ETC_SELENIUM_EXTRA_BROWSER_OPTIONS:
for browser_option in ETC_SELENIUM_EXTRA_BROWSER_OPTIONS:
chromeOptions.add_argument(browser_option)
# TODO: Document these? Seemed to come up a lot in googling errors and whatnot.
# # Avoid possible error in certain development environments about resource limits.
# # Error is along the lines of "DevToolsActivePort file doesn't exist".
# # See https://stackoverflow.com/a/69175552
# chromeOptions.add_argument('--disable-dev-shm-using')
# # Avoid possible error when many drivers are opened.
# # See https://stackoverflow.com/a/56638103
# chromeOptions.add_argument("--remote-debugging-port=9222")
# Save options.
cls._options = chromeOptions
except ModuleNotFoundError: except ModuleNotFoundError:
# Fall back to manual installation handling. # Fall back to manual installation handling.
...@@ -82,6 +60,28 @@ class LiveServerTestCase(DjangoLiveServerTestCase, LiveServerMixin): ...@@ -82,6 +60,28 @@ class LiveServerTestCase(DjangoLiveServerTestCase, LiveServerMixin):
# For Chromium. # For Chromium.
cls._service = ChromeService(executable_path='/usr/local/share/chromedriver') cls._service = ChromeService(executable_path='/usr/local/share/chromedriver')
# Set required chrome options.
chromeOptions = webdriver.ChromeOptions()
# Disable any existing extensions on local chrome setup, for consistent test runs across machines.
chromeOptions.add_argument('--disable-extensions')
# Add any user-provided options.
if ETC_SELENIUM_EXTRA_BROWSER_OPTIONS:
for browser_option in ETC_SELENIUM_EXTRA_BROWSER_OPTIONS:
chromeOptions.add_argument(browser_option)
# TODO: Document these? Seemed to come up a lot in googling errors and whatnot.
# # Avoid possible error in certain development environments about resource limits.
# # Error is along the lines of "DevToolsActivePort file doesn't exist".
# # See https://stackoverflow.com/a/69175552
# chromeOptions.add_argument('--disable-dev-shm-using')
# # Avoid possible error when many drivers are opened.
# # See https://stackoverflow.com/a/56638103
# chromeOptions.add_argument("--remote-debugging-port=9222")
# Save options.
cls._options = chromeOptions
# Everything else should handle the same for both. # Everything else should handle the same for both.
cls._browser = 'chrome' cls._browser = 'chrome'
...@@ -91,12 +91,23 @@ class LiveServerTestCase(DjangoLiveServerTestCase, LiveServerMixin): ...@@ -91,12 +91,23 @@ class LiveServerTestCase(DjangoLiveServerTestCase, LiveServerMixin):
# Setup browser driver to launch browser with. # Setup browser driver to launch browser with.
try: try:
# Attempt driver auto-install, if webdriver_manager package is present. # Attempt driver auto-install, if webdriver_manager package is present.
from webdriver_manager.firefox import GeckoDriverManager cls._service = FireFoxService()
cls._service = FireFoxService(executable_path=GeckoDriverManager().install())
except ModuleNotFoundError: except ModuleNotFoundError:
# Fall back to manual installation handling. # Fall back to manual installation handling.
cls._service = FireFoxService(executable_path='/usr/bin/geckodriver') cls._service = FireFoxService(executable_path='/usr/bin/geckodriver')
# Set required chrome options.
firefoxOptions = webdriver.FirefoxOptions()
# Add any user-provided options.
if ETC_SELENIUM_EXTRA_BROWSER_OPTIONS:
for browser_option in ETC_SELENIUM_EXTRA_BROWSER_OPTIONS:
firefoxOptions.add_argument(browser_option)
# Save options.
cls._options = firefoxOptions
else: else:
raise ValueError('Unknown browser "{0}".'.format(cls._browser)) raise ValueError('Unknown browser "{0}".'.format(cls._browser))
...@@ -132,7 +143,7 @@ class LiveServerTestCase(DjangoLiveServerTestCase, LiveServerMixin): ...@@ -132,7 +143,7 @@ class LiveServerTestCase(DjangoLiveServerTestCase, LiveServerMixin):
super().tearDownClass() super().tearDownClass()
def tearDown(self): def tearDown(self):
# TODO: Below seems probably unecessary? Research more. # TODO: Below seems probably unnecessary? Research more.
# # Close all remaining window instances for test. # # Close all remaining window instances for test.
# # (Or at least attempt to for default driver for test). # # (Or at least attempt to for default driver for test).
# self.close_all_windows(self.driver) # self.close_all_windows(self.driver)
......
...@@ -14,6 +14,8 @@ def runtests(): ...@@ -14,6 +14,8 @@ def runtests():
"""Run Tests""" """Run Tests"""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tests.settings') os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tests.settings')
os.environ.setdefault('PYTHONPATH', ROOT_DIR) os.environ.setdefault('PYTHONPATH', ROOT_DIR)
# os.environ.setdefault('EXPANDED_TEST_CASES_SELENIUM_BROWSER', 'firefox')
argv = ['pytest'] + sys.argv[1:] + ['--asyncio-mode=auto'] argv = ['pytest'] + sys.argv[1:] + ['--asyncio-mode=auto']
subprocess.run(argv, check=False) subprocess.run(argv, check=False)
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment