@@ -523,24 +523,39 @@ class TestDeviceAdminUnsavedChanges(
523523):
524524 browser = "chrome"
525525
526+ def _is_unsaved_changes_alert_present (self ):
527+ for entry in self .get_browser_logs ():
528+ if (
529+ entry ["level" ] == "WARNING"
530+ and "You haven't saved your changes yet!" in entry ["message" ]
531+ ):
532+ return True
533+ return False
534+
535+ def _override_unsaved_changes_alert (self ):
536+ self .web_driver .execute_script (
537+ 'django.jQuery(window).on("beforeunload", function(e) {'
538+ " console.warn(e.returnValue); });"
539+ )
540+
526541 def test_unsaved_changes (self ):
527542 """
528543 Execute this test using Chrome instead of Firefox.
529544 Firefox automatically accepts the beforeunload alert, which makes it
530545 impossible to test the unsaved changes alert.
531546 """
532547 self .login ()
548+ self ._create_template (default = True , default_values = {"ssid" : "default" })
533549 device = self ._create_config (organization = self ._get_org ()).device
534550 path = reverse ("admin:config_device_change" , args = [device .id ])
535551
536552 with self .subTest ("Alert should not be displayed without any change" ):
537553 self .open (path )
538554 self .hide_loading_overlay ()
539- try :
540- WebDriverWait (self .web_driver , 1 ).until (EC .alert_is_present ())
541- except TimeoutException :
542- pass
543- else :
555+ self ._override_unsaved_changes_alert ()
556+ # Simulate navigating away from the page
557+ self .open (reverse ("admin:index" ))
558+ if self ._is_unsaved_changes_alert_present ():
544559 self .fail ("Unsaved changes alert displayed without any change" )
545560
546561 with self .subTest ("Alert should be displayed after making changes" ):
@@ -550,10 +565,9 @@ def test_unsaved_changes(self):
550565 #
551566 # our own JS code sets e.returnValue when triggered
552567 # so we just need to ensure it's set as expected
553- self .web_driver .execute_script (
554- 'django.jQuery(window).on("beforeunload", function(e) {'
555- " console.warn(e.returnValue); });"
556- )
568+ self .open (path )
569+ self .hide_loading_overlay ()
570+ self ._override_unsaved_changes_alert ()
557571 # simulate hand gestures
558572 self .find_element (by = By .TAG_NAME , value = "body" ).click ()
559573 self .find_element (by = By .NAME , value = "name" ).click ()
@@ -562,13 +576,7 @@ def test_unsaved_changes(self):
562576 # simulate hand gestures
563577 self .find_element (by = By .TAG_NAME , value = "body" ).click ()
564578 self .web_driver .refresh ()
565- for entry in self .get_browser_logs ():
566- if (
567- entry ["level" ] == "WARNING"
568- and "You haven't saved your changes yet!" in entry ["message" ]
569- ):
570- break
571- else :
579+ if not self ._is_unsaved_changes_alert_present ():
572580 self .fail ("Unsaved changes code was not executed." )
573581
574582
0 commit comments