Skip to content

Commit c9238fe

Browse files
authored
[chores] Use SeleniumTestMixin from openwisp-utils
1 parent 0666a9b commit c9238fe

File tree

3 files changed

+3
-92
lines changed

3 files changed

+3
-92
lines changed

openwisp_controller/config/tests/test_selenium.py

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1-
from django.conf import settings
21
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
32
from django.urls.base import reverse
4-
from selenium import webdriver
53
from selenium.common.exceptions import (
64
StaleElementReferenceException,
75
TimeoutException,
86
UnexpectedAlertPresentException,
97
)
108
from selenium.webdriver.common.alert import Alert
119
from selenium.webdriver.common.by import By
12-
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
1310
from selenium.webdriver.support import expected_conditions as EC
1411
from selenium.webdriver.support.ui import Select, WebDriverWait
1512

1613
from openwisp_users.tests.utils import TestOrganizationMixin
14+
from openwisp_utils.test_selenium_mixins import SeleniumTestMixin
1715

18-
from ...tests.test_selenium import SeleniumTestMixin
1916
from .utils import CreateConfigTemplateMixin
2017

2118

@@ -25,29 +22,6 @@ class TestDeviceAdmin(
2522
SeleniumTestMixin,
2623
StaticLiveServerTestCase,
2724
):
28-
admin_username = 'admin'
29-
admin_password = 'password'
30-
31-
@classmethod
32-
def setUpClass(cls):
33-
super().setUpClass()
34-
chrome_options = webdriver.ChromeOptions()
35-
if getattr(settings, 'SELENIUM_HEADLESS', True):
36-
chrome_options.add_argument('--headless')
37-
chrome_options.add_argument('--window-size=1366,768')
38-
chrome_options.add_argument('--ignore-certificate-errors')
39-
chrome_options.add_argument('--remote-debugging-port=9222')
40-
capabilities = DesiredCapabilities.CHROME
41-
capabilities['goog:loggingPrefs'] = {'browser': 'ALL'}
42-
cls.web_driver = webdriver.Chrome(
43-
options=chrome_options, desired_capabilities=capabilities
44-
)
45-
46-
@classmethod
47-
def tearDownClass(cls):
48-
cls.web_driver.quit()
49-
super().tearDownClass()
50-
5125
def setUp(self):
5226
self.admin = self._create_admin(
5327
username=self.admin_username, password=self.admin_password

openwisp_controller/tests/test_selenium.py

Lines changed: 1 addition & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,31 @@
1-
from django.conf import settings
21
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
32
from django.core.management import call_command
43
from django.urls.base import reverse
54
from reversion.models import Version
6-
from selenium import webdriver
75
from selenium.common.exceptions import TimeoutException, UnexpectedAlertPresentException
86
from selenium.webdriver.common.alert import Alert
97
from selenium.webdriver.common.by import By
10-
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
118
from selenium.webdriver.support import expected_conditions as EC
129
from selenium.webdriver.support.ui import WebDriverWait
1310
from swapper import load_model
1411

1512
from openwisp_controller.connection.tests.utils import CreateConnectionsMixin
1613
from openwisp_controller.geo.tests.utils import TestGeoMixin
14+
from openwisp_utils.test_selenium_mixins import SeleniumTestMixin
1715

1816
Device = load_model('config', 'Device')
1917
DeviceConnection = load_model('connection', 'DeviceConnection')
2018
Location = load_model('geo', 'Location')
2119
DeviceLocation = load_model('geo', 'DeviceLocation')
2220

2321

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-
6222
class TestDeviceConnectionInlineAdmin(
6323
CreateConnectionsMixin, TestGeoMixin, SeleniumTestMixin, StaticLiveServerTestCase
6424
):
6525
config_app_label = 'config'
66-
admin_username = 'admin'
67-
admin_password = 'password'
6826
location_model = Location
6927
object_location_model = DeviceLocation
7028

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-
9129
def setUp(self):
9230
self.admin = self._create_admin(
9331
username=self.admin_username, password=self.admin_password

requirements-test.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ pytest~=6.0
22
pytest-django~=4.5.2
33
pytest-asyncio~=0.14.0
44
pytest-cov~=2.10.0
5-
openwisp-utils[qa] @ https://github.com/openwisp/openwisp-utils/tarball/master
5+
openwisp-utils[qa,selenium] @ https://github.com/openwisp/openwisp-utils/tarball/master
66
redis~=4.5.4
77
channels_redis~=4.1.0
88
django_redis~=5.2.0
99
mock-ssh-server~=0.9.1
1010
responses~=0.12.1
11-
selenium~=4.9.0
1211
psycopg2-binary~=2.8.0

0 commit comments

Comments
 (0)