mirror of
https://github.com/django/django.git
synced 2025-10-23 21:59:11 +00:00
Fixed #29883 -- Added selenium hub support to runtests.py.
This commit is contained in:
@@ -2,7 +2,11 @@ import sys
|
||||
import unittest
|
||||
from contextlib import contextmanager
|
||||
|
||||
from selenium import webdriver
|
||||
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
|
||||
|
||||
from django.test import LiveServerTestCase, tag
|
||||
from django.utils.decorators import classproperty
|
||||
from django.utils.module_loading import import_string
|
||||
from django.utils.text import capfirst
|
||||
|
||||
@@ -10,6 +14,10 @@ from django.utils.text import capfirst
|
||||
class SeleniumTestCaseBase(type(LiveServerTestCase)):
|
||||
# List of browsers to dynamically create test classes for.
|
||||
browsers = []
|
||||
# A selenium hub URL to test against.
|
||||
selenium_hub = None
|
||||
# The external host Selenium Hub can reach.
|
||||
external_host = None
|
||||
# Sentinel value to differentiate browser-specific instances.
|
||||
browser = None
|
||||
|
||||
@@ -29,6 +37,10 @@ class SeleniumTestCaseBase(type(LiveServerTestCase)):
|
||||
# either duplicate tests or prevent pickling of its instances.
|
||||
first_browser = test_class.browsers[0]
|
||||
test_class.browser = first_browser
|
||||
# Listen on an external interface if using a selenium hub.
|
||||
host = test_class.host if not test_class.selenium_hub else '0.0.0.0'
|
||||
test_class.host = host
|
||||
test_class.external_host = cls.external_host
|
||||
# Create subclasses for each of the remaining browsers and expose
|
||||
# them through the test's module namespace.
|
||||
module = sys.modules[test_class.__module__]
|
||||
@@ -37,7 +49,12 @@ class SeleniumTestCaseBase(type(LiveServerTestCase)):
|
||||
cls,
|
||||
"%s%s" % (capfirst(browser), name),
|
||||
(test_class,),
|
||||
{'browser': browser, '__module__': test_class.__module__}
|
||||
{
|
||||
'browser': browser,
|
||||
'host': host,
|
||||
'external_host': cls.external_host,
|
||||
'__module__': test_class.__module__,
|
||||
}
|
||||
)
|
||||
setattr(module, browser_test_class.__name__, browser_test_class)
|
||||
return test_class
|
||||
@@ -48,13 +65,31 @@ class SeleniumTestCaseBase(type(LiveServerTestCase)):
|
||||
def import_webdriver(cls, browser):
|
||||
return import_string("selenium.webdriver.%s.webdriver.WebDriver" % browser)
|
||||
|
||||
@classmethod
|
||||
def get_capability(cls, browser):
|
||||
return getattr(DesiredCapabilities, browser.upper())
|
||||
|
||||
def create_webdriver(self):
|
||||
if self.selenium_hub:
|
||||
return webdriver.Remote(
|
||||
command_executor=self.selenium_hub,
|
||||
desired_capabilities=self.get_capability(self.browser),
|
||||
)
|
||||
return self.import_webdriver(self.browser)()
|
||||
|
||||
|
||||
@tag('selenium')
|
||||
class SeleniumTestCase(LiveServerTestCase, metaclass=SeleniumTestCaseBase):
|
||||
implicit_wait = 10
|
||||
external_host = None
|
||||
|
||||
@classproperty
|
||||
def live_server_url(cls):
|
||||
return 'http://%s:%s' % (cls.external_host or cls.host, cls.server_thread.port)
|
||||
|
||||
@classproperty
|
||||
def allowed_host(cls):
|
||||
return cls.external_host or cls.host
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
|
||||
@@ -1303,6 +1303,10 @@ class LiveServerTestCase(TransactionTestCase):
|
||||
def live_server_url(cls):
|
||||
return 'http://%s:%s' % (cls.host, cls.server_thread.port)
|
||||
|
||||
@classproperty
|
||||
def allowed_host(cls):
|
||||
return cls.host
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
super().setUpClass()
|
||||
@@ -1316,7 +1320,7 @@ class LiveServerTestCase(TransactionTestCase):
|
||||
connections_override[conn.alias] = conn
|
||||
|
||||
cls._live_server_modified_settings = modify_settings(
|
||||
ALLOWED_HOSTS={'append': cls.host},
|
||||
ALLOWED_HOSTS={'append': cls.allowed_host},
|
||||
)
|
||||
cls._live_server_modified_settings.enable()
|
||||
cls.server_thread = cls._create_server_thread(connections_override)
|
||||
|
||||
Reference in New Issue
Block a user