66from urllib import request
77
88import requests
9- from selenium import webdriver
109from selenium .common .exceptions import NoSuchElementException , TimeoutException
11- from selenium .webdriver .chrome .options import Options as ChromiumOptions
1210from selenium .webdriver .common .by import By
13- from selenium .webdriver .common .desired_capabilities import DesiredCapabilities
1411from selenium .webdriver .support import expected_conditions as EC
1512from selenium .webdriver .support .ui import WebDriverWait
1613from utils import TestUtilities
@@ -70,6 +67,9 @@ def failureException(self):
7067 @classmethod
7168 def setUpClass (cls ):
7269 cls .failed_test = False
70+ cls .live_server_url = cls .config ['app_url' ]
71+ cls .admin_username = cls .config ['username' ]
72+ cls .admin_password = cls .config ['password' ]
7373 # Django Test Setup
7474 if cls .config ['load_init_data' ]:
7575 test_data_file = os .path .join (
@@ -105,38 +105,13 @@ def setUpClass(cls):
105105 )
106106 # Create base drivers (Firefox)
107107 if cls .config ['driver' ] == 'firefox' :
108- profile = webdriver .FirefoxProfile ()
109- profile .accept_untrusted_certs = True
110- options = webdriver .FirefoxOptions ()
111- options .set_capability ("loggingPrefs" , {'browser' : 'ALL' })
112- if cls .config ['headless' ]:
113- options .add_argument ('-headless' )
114- cls .base_driver = webdriver .Firefox (
115- options = options ,
116- service_log_path = '/tmp/geckodriver_base_driver.log' ,
117- firefox_profile = profile ,
118- )
119- cls .second_driver = webdriver .Firefox (
120- options = options ,
121- service_log_path = '/tmp/geckodriver_second_driver.log' ,
122- firefox_profile = profile ,
123- )
108+ cls .base_driver = cls .get_firefox_webdriver ()
109+ cls .second_driver = cls .get_firefox_webdriver ()
124110 # Create base drivers (Chromium)
125111 if cls .config ['driver' ] == 'chromium' :
126- options = ChromiumOptions ()
127- options .add_argument ('--ignore-certificate-errors' )
128- options .add_argument ('--no-sandbox' )
129- options .add_argument ('--disable-dev-shm-usage' )
130- if cls .config ['headless' ]:
131- options .add_argument ('--headless' )
132- options .add_argument (f'--remote-debugging-port={ 5003 + 100 } ' )
133- capabilities = DesiredCapabilities .CHROME
134- capabilities ['goog:loggingPrefs' ] = {'browser' : 'ALL' }
135- options .set_capability ('cloud:options' , capabilities )
136- cls .base_driver = webdriver .Chrome (options = options )
137- cls .second_driver = webdriver .Chrome (options = options )
138- cls .base_driver .set_window_size (1366 , 768 )
139- cls .second_driver .set_window_size (1366 , 768 )
112+ cls .base_driver = cls .get_chrome_webdriver ()
113+ cls .second_driver = cls .get_chrome_webdriver ()
114+ cls .web_driver = cls .base_driver
140115
141116 @classmethod
142117 def tearDownClass (cls ):
@@ -175,59 +150,30 @@ def test_topology_graph(self):
175150 self .create_network_topology (label )
176151 self .get_resource (label , path , select_field = 'field-label' )
177152 # Click on "Visualize topology graph" button
178- try :
179- WebDriverWait (self .base_driver , 2 ).until (
180- EC .presence_of_element_located ((By .CSS_SELECTOR , 'input.visualizelink' ))
181- )
182- except TimeoutException :
183- self .fail ('Topology visualize button not found.' )
184- else :
185- self .base_driver .find_element (
186- By .CSS_SELECTOR , 'input.visualizelink'
187- ).click ()
153+ self .find_element (By .CSS_SELECTOR , 'input.visualizelink' ).click ()
188154 # Click on sidebar handle
189- try :
190- WebDriverWait (self .base_driver , 2 ).until (
191- EC .presence_of_element_located (
192- (By .CSS_SELECTOR , 'button.sideBarHandle' )
193- )
194- )
195- except TimeoutException :
196- self .fail ('Topology visualize button not found.' )
197- else :
198- self .base_driver .find_element (
199- By .CSS_SELECTOR , 'button.sideBarHandle'
200- ).click ()
155+ self .find_element (By .CSS_SELECTOR , 'button.sideBarHandle' ).click ()
201156 # Verify topology label
202- try :
203- WebDriverWait (self .base_driver , 2 ).until (
204- EC .visibility_of_element_located ((By .CSS_SELECTOR , '.njg-valueLabel' ))
205- )
206- except TimeoutException :
207- self .fail ('Topology visualize button not found.' )
208- else :
209- self .assertEqual (
210- self .base_driver .find_element (
211- By .CSS_SELECTOR , '.njg-valueLabel'
212- ).text .lower (),
213- label ,
214- )
157+ self .assertEqual (
158+ self .find_element (By .CSS_SELECTOR , '.njg-valueLabel' ).text .lower (),
159+ label ,
160+ )
215161 self .assertEqual (len (self .console_error_check ()), 0 )
216162 self .action_on_resource (label , path , 'delete_selected' )
217- self .assertNotIn ('<li>Nodes: ' , self .base_driver .page_source )
163+ self .assertNotIn ('<li>Nodes: ' , self .web_driver .page_source )
218164 self .action_on_resource (label , path , 'update_selected' )
219165
220166 self .action_on_resource (label , path , 'delete_selected' )
221- self ._wait_for_element ()
222- self .assertIn ('<li>Nodes: ' , self .base_driver .page_source )
167+ self ._wait_until_page_ready ()
168+ self .assertIn ('<li>Nodes: ' , self .web_driver .page_source )
223169
224170 def test_admin_login (self ):
225171 self .login ()
226172 self .login (driver = self .second_driver )
227173 try :
228- self .base_driver . find_element (By .CLASS_NAME , 'logout' )
229- self .second_driver . find_element (By .CLASS_NAME , 'logout' )
230- except NoSuchElementException :
174+ self .find_element (By .CLASS_NAME , 'logout' )
175+ self .find_element (By .CLASS_NAME , 'logout' , driver = self . second_driver )
176+ except TimeoutError :
231177 message = (
232178 'Login failed. Credentials used were username: '
233179 f"{ self .config ['username' ]} & Password: { self .config ['password' ]} "
@@ -236,10 +182,8 @@ def test_admin_login(self):
236182
237183 def test_device_monitoring_charts (self ):
238184 self .login ()
239- self ._wait_for_element ()
240185 self .get_resource ('test-device' , '/admin/config/device/' )
241- self ._wait_for_element ()
242- self .base_driver .find_element (By .CSS_SELECTOR , 'ul.tabs li.charts' ).click ()
186+ self .find_element (By .CSS_SELECTOR , 'ul.tabs li.charts' ).click ()
243187 try :
244188 WebDriverWait (self .base_driver , 3 ).until (EC .alert_is_present ())
245189 except TimeoutException :
@@ -253,30 +197,27 @@ def test_device_monitoring_charts(self):
253197
254198 def test_default_topology (self ):
255199 self .login ()
256- self ._wait_for_element ()
257- self .get_resource ('test-device' , '/admin/topology/topology/' )
200+ self .get_resource (
201+ 'test-device' , '/admin/topology/topology/' , select_field = 'field-label'
202+ )
258203
259204 def test_create_prefix_users (self ):
260205 self .login ()
261206 prefix_objname = 'automated-prefix-test-01'
262207 # Create prefix users
263- self .base_driver .get (
264- f"{ self .config ['app_url' ]} /admin/openwisp_radius/radiusbatch/add/"
265- )
266- self ._wait_for_element ()
267- self .base_driver .find_element (By .NAME , 'strategy' ).find_element (
208+ self .open ('/admin/openwisp_radius/radiusbatch/add/' )
209+ self .find_element (By .NAME , 'strategy' ).find_element (
268210 By .XPATH , '//option[@value="prefix"]'
269211 ).click ()
270- self .base_driver . find_element (By .NAME , 'organization' ).find_element (
212+ self .find_element (By .NAME , 'organization' ).find_element (
271213 By .XPATH , '//option[text()="default"]'
272214 ).click ()
273- self .base_driver . find_element (By .NAME , 'name' ).send_keys (prefix_objname )
274- self .base_driver . find_element (By .NAME , 'prefix' ).send_keys ('automated-prefix' )
275- self .base_driver . find_element (By .NAME , 'number_of_users' ).send_keys ('1' )
276- self .base_driver . find_element (By .NAME , '_save' ).click ()
215+ self .find_element (By .NAME , 'name' ).send_keys (prefix_objname )
216+ self .find_element (By .NAME , 'prefix' ).send_keys ('automated-prefix' )
217+ self .find_element (By .NAME , 'number_of_users' ).send_keys ('1' )
218+ self .find_element (By .NAME , '_save' ).click ()
277219 # Check PDF available
278220 self .get_resource (prefix_objname , '/admin/openwisp_radius/radiusbatch/' )
279- self ._wait_for_element ()
280221 self .objects_to_delete .append (self .base_driver .current_url )
281222 prefix_pdf_file_path = self .base_driver .find_element (
282223 By .XPATH , '//a[text()="Download User Credentials"]'
@@ -323,19 +264,19 @@ def test_console_errors(self):
323264 ['default' , '/admin/pki/ca/' ],
324265 ['default' , '/admin/pki/cert/' ],
325266 ['default' , '/admin/openwisp_users/organization/' ],
326- ['test_superuser2' , '/admin/openwisp_users/user/' ],
267+ ['test_superuser2' , '/admin/openwisp_users/user/' , 'field-username' ],
327268 ]
328269 self .login ()
329270 self .create_mobile_location ('automated-selenium-location01' )
330271 self .create_superuser ('sample@email.com' , 'test_superuser2' )
331272 # url_list tests
332273 for url in url_list :
333- self .base_driver . get ( f" { self . config [ 'app_url' ] } { url } " )
274+ self .open ( url )
334275 self .assertEqual ([], self .console_error_check ())
335276 self .assertIn ('OpenWISP' , self .base_driver .title )
336277 # change_form_list tests
337278 for change_form in change_form_list :
338- self .get_resource (change_form [ 0 ], change_form [ 1 ] )
279+ self .get_resource (* change_form )
339280 self .assertEqual ([], self .console_error_check ())
340281 self .assertIn ('OpenWISP' , self .base_driver .title )
341282
@@ -353,11 +294,11 @@ def test_websocket_marker(self):
353294 self .get_resource (
354295 location_name , '/admin/geo/location/' , driver = self .second_driver
355296 )
356- self .base_driver . find_element (By .NAME , 'is_mobile' ).click ()
357- mark = len (self .base_driver . find_elements (By .CLASS_NAME , 'leaflet-marker-icon' ))
297+ self .find_element (By .NAME , 'is_mobile' , driver = self . base_driver ).click ()
298+ mark = len (self .find_elements (By .CLASS_NAME , 'leaflet-marker-icon' ))
358299 self .assertEqual (mark , 0 )
359300 self .add_mobile_location_point (location_name , driver = self .second_driver )
360- mark = len (self .base_driver . find_elements (By .CLASS_NAME , 'leaflet-marker-icon' ))
301+ mark = len (self .find_elements (By .CLASS_NAME , 'leaflet-marker-icon' ))
361302 self .assertEqual (mark , 1 )
362303
363304 def test_add_superuser (self ):
@@ -366,15 +307,16 @@ def test_add_superuser(self):
366307 self .create_superuser ()
367308 self .assertEqual (
368309 'The user “test_superuser” was changed successfully.' ,
369- self .base_driver . find_element (By .CLASS_NAME , 'success' ).text ,
310+ self .find_element (By .CLASS_NAME , 'success' ).text ,
370311 )
371312
372313 def test_forgot_password (self ):
373314 """Test forgot password to ensure that postfix is working properly."""
374- self .base_driver .get (f"{ self .config ['app_url' ]} /accounts/password/reset/" )
375- self .base_driver .find_element (By .NAME , 'email' ).send_keys ('admin@example.com' )
376- self .base_driver .find_element (By .XPATH , '//button[@type="submit"]' ).click ()
377- self ._wait_for_element ()
315+
316+ self .open ('/accounts/password/reset/' )
317+ self .find_element (By .NAME , 'email' ).send_keys ('admin@example.com' )
318+ self .find_element (By .XPATH , '//button[@type="submit"]' ).click ()
319+ self ._wait_until_page_ready ()
378320 self .assertIn (
379321 'We have sent you an email. If you have not received '
380322 'it please check your spam folder. Otherwise contact us '
@@ -535,4 +477,9 @@ def test_containers_down(self):
535477
536478
537479if __name__ == '__main__' :
538- unittest .main (verbosity = 2 )
480+ # unittest.main(verbosity=2)
481+ # Run only the test_websocket_marker test
482+ suite = unittest .TestSuite ()
483+ suite .addTest (TestServices ('test_console_errors' ))
484+ runner = unittest .TextTestRunner (verbosity = 2 )
485+ runner .run (suite )
0 commit comments