1
0
mirror of https://github.com/django/django.git synced 2025-11-07 07:15:35 +00:00

Fixed #30220 -- Added support for headless mode in selenium tests.

This commit is contained in:
Johannes Hoppe
2019-02-26 17:23:49 +01:00
committed by Mariusz Felisiak
parent e286987a27
commit 8d010f3986
4 changed files with 27 additions and 1 deletions

View File

@@ -17,6 +17,8 @@ class SeleniumTestCaseBase(type(LiveServerTestCase)):
external_host = None
# Sentinel value to differentiate browser-specific instances.
browser = None
# Run browsers in headless mode.
headless = False
def __new__(cls, name, bases, attrs):
"""
@@ -62,11 +64,24 @@ class SeleniumTestCaseBase(type(LiveServerTestCase)):
def import_webdriver(cls, browser):
return import_string("selenium.webdriver.%s.webdriver.WebDriver" % browser)
@classmethod
def import_options(cls, browser):
return import_string('selenium.webdriver.%s.options.Options' % browser)
@classmethod
def get_capability(cls, browser):
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
return getattr(DesiredCapabilities, browser.upper())
def create_options(self):
options = self.import_options(self.browser)()
if self.headless:
try:
options.headless = True
except AttributeError:
pass # Only Chrome and Firefox support the headless mode.
return options
def create_webdriver(self):
if self.selenium_hub:
from selenium import webdriver
@@ -74,7 +89,7 @@ class SeleniumTestCaseBase(type(LiveServerTestCase)):
command_executor=self.selenium_hub,
desired_capabilities=self.get_capability(self.browser),
)
return self.import_webdriver(self.browser)()
return self.import_webdriver(self.browser)(options=self.create_options())
@tag('selenium')