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 external_host = None
# Sentinel value to differentiate browser-specific instances. # Sentinel value to differentiate browser-specific instances.
browser = None browser = None
# Run browsers in headless mode.
headless = False
def __new__(cls, name, bases, attrs): def __new__(cls, name, bases, attrs):
""" """
@ -62,11 +64,24 @@ class SeleniumTestCaseBase(type(LiveServerTestCase)):
def import_webdriver(cls, browser): def import_webdriver(cls, browser):
return import_string("selenium.webdriver.%s.webdriver.WebDriver" % 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 @classmethod
def get_capability(cls, browser): def get_capability(cls, browser):
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
return getattr(DesiredCapabilities, browser.upper()) 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): def create_webdriver(self):
if self.selenium_hub: if self.selenium_hub:
from selenium import webdriver from selenium import webdriver
@ -74,7 +89,7 @@ class SeleniumTestCaseBase(type(LiveServerTestCase)):
command_executor=self.selenium_hub, command_executor=self.selenium_hub,
desired_capabilities=self.get_capability(self.browser), 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') @tag('selenium')

View File

@ -230,6 +230,9 @@ See the `selenium.webdriver`_ package for the list of available browsers.
Specifying ``--selenium`` automatically sets ``--tags=selenium`` to run only Specifying ``--selenium`` automatically sets ``--tags=selenium`` to run only
the tests that require selenium. the tests that require selenium.
Some browsers (e.g. Chrome or Firefox) support headless testing, which can be
faster and more stable. Add the ``--headless`` option to enable this mode.
.. _selenium.webdriver: https://github.com/SeleniumHQ/selenium/tree/master/py/selenium/webdriver .. _selenium.webdriver: https://github.com/SeleniumHQ/selenium/tree/master/py/selenium/webdriver
.. _running-unit-tests-dependencies: .. _running-unit-tests-dependencies:

View File

@ -254,6 +254,9 @@ Tests
references, and entity references that refer to the same character as references, and entity references that refer to the same character as
equivalent. equivalent.
* Django test runner now supports headless mode for selenium tests on supported
browsers. Add the ``--headless`` option to enable this mode.
URLs URLs
~~~~ ~~~~

View File

@ -446,6 +446,10 @@ if __name__ == "__main__":
'--selenium', action=ActionSelenium, metavar='BROWSERS', '--selenium', action=ActionSelenium, metavar='BROWSERS',
help='A comma-separated list of browsers to run the Selenium tests against.', help='A comma-separated list of browsers to run the Selenium tests against.',
) )
parser.add_argument(
'--headless', action='store_true',
help='Run selenium tests in headless mode, if the browser supports the option.',
)
parser.add_argument( parser.add_argument(
'--selenium-hub', '--selenium-hub',
help='A URL for a selenium hub instance to use in combination with --selenium.', help='A URL for a selenium hub instance to use in combination with --selenium.',
@ -506,6 +510,7 @@ if __name__ == "__main__":
if options.selenium_hub: if options.selenium_hub:
SeleniumTestCaseBase.selenium_hub = options.selenium_hub SeleniumTestCaseBase.selenium_hub = options.selenium_hub
SeleniumTestCaseBase.external_host = options.external_host SeleniumTestCaseBase.external_host = options.external_host
SeleniumTestCaseBase.headless = options.headless
SeleniumTestCaseBase.browsers = options.selenium SeleniumTestCaseBase.browsers = options.selenium
if options.bisect: if options.bisect: