|
1 | | -from django.conf import settings |
2 | 1 | from django.contrib.staticfiles.testing import StaticLiveServerTestCase |
3 | 2 | from django.core.management import call_command |
4 | 3 | from django.urls.base import reverse |
5 | 4 | from reversion.models import Version |
6 | | -from selenium import webdriver |
7 | 5 | from selenium.common.exceptions import TimeoutException, UnexpectedAlertPresentException |
8 | 6 | from selenium.webdriver.common.alert import Alert |
9 | 7 | from selenium.webdriver.common.by import By |
10 | | -from selenium.webdriver.common.desired_capabilities import DesiredCapabilities |
11 | 8 | from selenium.webdriver.support import expected_conditions as EC |
12 | 9 | from selenium.webdriver.support.ui import WebDriverWait |
13 | 10 | from swapper import load_model |
14 | 11 |
|
15 | 12 | from openwisp_controller.connection.tests.utils import CreateConnectionsMixin |
16 | 13 | from openwisp_controller.geo.tests.utils import TestGeoMixin |
| 14 | +from openwisp_utils.test_selenium_mixins import SeleniumTestMixin |
17 | 15 |
|
18 | 16 | Device = load_model('config', 'Device') |
19 | 17 | DeviceConnection = load_model('connection', 'DeviceConnection') |
20 | 18 | Location = load_model('geo', 'Location') |
21 | 19 | DeviceLocation = load_model('geo', 'DeviceLocation') |
22 | 20 |
|
23 | 21 |
|
24 | | -class SeleniumTestMixin: |
25 | | - """ |
26 | | - A base test case for Selenium, providing helped methods for generating |
27 | | - clients and logging in profiles. |
28 | | - """ |
29 | | - |
30 | | - def open(self, url, driver=None): |
31 | | - """ |
32 | | - Opens a URL |
33 | | - Argument: |
34 | | - url: URL to open |
35 | | - driver: selenium driver (default: cls.base_driver) |
36 | | - """ |
37 | | - if not driver: |
38 | | - driver = self.web_driver |
39 | | - driver.get(f'{self.live_server_url}{url}') |
40 | | - |
41 | | - def login(self, username=None, password=None, driver=None): |
42 | | - """ |
43 | | - Log in to the admin dashboard |
44 | | - Argument: |
45 | | - driver: selenium driver (default: cls.web_driver) |
46 | | - username: username to be used for login (default: cls.admin.username) |
47 | | - password: password to be used for login (default: cls.admin.password) |
48 | | - """ |
49 | | - if not driver: |
50 | | - driver = self.web_driver |
51 | | - if not username: |
52 | | - username = self.admin_username |
53 | | - if not password: |
54 | | - password = self.admin_password |
55 | | - driver.get(f'{self.live_server_url}/admin/login/') |
56 | | - if 'admin/login' in driver.current_url: |
57 | | - driver.find_element(by=By.NAME, value='username').send_keys(username) |
58 | | - driver.find_element(by=By.NAME, value='password').send_keys(password) |
59 | | - driver.find_element(by=By.XPATH, value='//input[@type="submit"]').click() |
60 | | - |
61 | | - |
62 | 22 | class TestDeviceConnectionInlineAdmin( |
63 | 23 | CreateConnectionsMixin, TestGeoMixin, SeleniumTestMixin, StaticLiveServerTestCase |
64 | 24 | ): |
65 | 25 | config_app_label = 'config' |
66 | | - admin_username = 'admin' |
67 | | - admin_password = 'password' |
68 | 26 | location_model = Location |
69 | 27 | object_location_model = DeviceLocation |
70 | 28 |
|
71 | | - @classmethod |
72 | | - def setUpClass(cls): |
73 | | - super().setUpClass() |
74 | | - chrome_options = webdriver.ChromeOptions() |
75 | | - if getattr(settings, 'SELENIUM_HEADLESS', True): |
76 | | - chrome_options.add_argument('--headless') |
77 | | - chrome_options.add_argument('--window-size=1366,768') |
78 | | - chrome_options.add_argument('--ignore-certificate-errors') |
79 | | - chrome_options.add_argument('--remote-debugging-port=9222') |
80 | | - capabilities = DesiredCapabilities.CHROME |
81 | | - capabilities['goog:loggingPrefs'] = {'browser': 'ALL'} |
82 | | - cls.web_driver = webdriver.Chrome( |
83 | | - options=chrome_options, desired_capabilities=capabilities |
84 | | - ) |
85 | | - |
86 | | - @classmethod |
87 | | - def tearDownClass(cls): |
88 | | - cls.web_driver.quit() |
89 | | - super().tearDownClass() |
90 | | - |
91 | 29 | def setUp(self): |
92 | 30 | self.admin = self._create_admin( |
93 | 31 | username=self.admin_username, password=self.admin_password |
|
0 commit comments