|
| 1 | +from django.db import connections |
| 2 | +from django.conf import settings |
| 3 | +from django.core.management import call_command |
1 | 4 | from django.test import LiveServerTestCase
|
2 | 5 | from selenium import webdriver
|
3 | 6 | from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
|
4 |
| -from wagtail.core.models import Page |
5 |
| -from selenium_tests.pages import BasePage |
6 |
| -from selenium_tests.pages import LoginPage |
7 |
| -from selenium_tests.pages import LogoutPage |
8 |
| -from wagtail.core.models import Site |
| 7 | +from wagtail.core.models import Page, Site |
9 | 8 | from wagtail_factories import SiteFactory
|
| 9 | + |
10 | 10 | from home.factories import HomePageFactory
|
| 11 | +from selenium_tests.pages import BasePage, LoginPage, LogoutPage |
| 12 | + |
11 | 13 |
|
12 | 14 | class BaseSeleniumTests(LiveServerTestCase):
|
13 | 15 |
|
14 |
| - fixtures = ['selenium_tests/locales.json'] |
| 16 | + fixtures = ['selenium_tests/locales.json'] |
15 | 17 |
|
16 |
| - host = 'django' |
17 |
| - port = 9000 |
| 18 | + host = settings.SE_APP_HOST |
18 | 19 |
|
19 | 20 | @classmethod
|
20 | 21 | def setUpClass(cls):
|
21 |
| - options = webdriver.ChromeOptions() |
22 |
| - options.add_argument('--ignore-ssl-errors=yes') |
23 |
| - options.add_argument('--ignore-certificate-errors') |
24 |
| - options.add_argument("--window-size=480,720") |
25 |
| - options.add_argument("--start-maximized") |
26 |
| - cls.selenium = webdriver.Remote( |
27 |
| - command_executor='http://selenium-hub:4444/wd/hub', |
28 |
| - desired_capabilities=DesiredCapabilities.CHROME, |
29 |
| - options=options |
30 |
| - ) |
31 |
| - cls.selenium.implicitly_wait(10) |
| 22 | + cls.selenium = create_remote_webdriver() |
32 | 23 | super(BaseSeleniumTests, cls).setUpClass()
|
33 | 24 |
|
34 | 25 | @classmethod
|
@@ -62,3 +53,56 @@ def setup_blank_site(self):
|
62 | 53 | is_default_site=True,
|
63 | 54 | root_page=self.home
|
64 | 55 | )
|
| 56 | + |
| 57 | + # https://github.com/wagtail/wagtail/issues/1824#issuecomment-450575883 |
| 58 | + # Identical to TransactionTestCase._fixture_teardown except that 'allow_cascade' is |
| 59 | + # forced. |
| 60 | + def _fixture_teardown(self): |
| 61 | + # Allow TRUNCATE ... CASCADE and don't emit the post_migrate signal |
| 62 | + # when flushing only a subset of the apps |
| 63 | + for db_name in self._databases_names(include_mirrors=False): |
| 64 | + # Flush the database |
| 65 | + inhibit_post_migrate = ( |
| 66 | + self.available_apps is not None or |
| 67 | + ( # Inhibit the post_migrate signal when using serialized |
| 68 | + # rollback to avoid trying to recreate the serialized data. |
| 69 | + self.serialized_rollback and |
| 70 | + hasattr(connections[db_name], '_test_serialized_contents') |
| 71 | + ) |
| 72 | + ) |
| 73 | + call_command('flush', verbosity=0, interactive=False, |
| 74 | + database=db_name, reset_sequences=False, |
| 75 | + allow_cascade=True, |
| 76 | + inhibit_post_migrate=inhibit_post_migrate) |
| 77 | + |
| 78 | + |
| 79 | +def create_remote_webdriver(preset: str = "chrome") -> webdriver.Remote: |
| 80 | + if preset == "chrome": |
| 81 | + options = webdriver.ChromeOptions() |
| 82 | + options.add_argument('--ignore-ssl-errors=yes') |
| 83 | + options.add_argument('--ignore-certificate-errors') |
| 84 | + options.add_argument("--window-size=480,720") |
| 85 | + options.add_argument("--start-maximized") |
| 86 | + driver = webdriver.Remote( |
| 87 | + command_executor=get_hub_url(), |
| 88 | + desired_capabilities=DesiredCapabilities.CHROME, |
| 89 | + options=options, |
| 90 | + ) |
| 91 | + elif preset == "firefox": |
| 92 | + options = webdriver.FirefoxOptions() |
| 93 | + driver = webdriver.Remote( |
| 94 | + command_executor=get_hub_url(), |
| 95 | + desired_capabilities=DesiredCapabilities.FIREFOX, |
| 96 | + options=options, |
| 97 | + ) |
| 98 | + else: |
| 99 | + raise Exception( |
| 100 | + f"Invalid webdriver preset ('{preset}'); must be 'chrome' or 'firefox'" |
| 101 | + ) |
| 102 | + driver.set_page_load_timeout(60) |
| 103 | + driver.implicitly_wait(10) |
| 104 | + return driver |
| 105 | + |
| 106 | + |
| 107 | +def get_hub_url(): |
| 108 | + return f"http://{settings.SE_HUB_HOST}:{settings.SE_HUB_PORT}/wd/hub" |
0 commit comments