@@ -523,24 +523,39 @@ class TestDeviceAdminUnsavedChanges(
523
523
):
524
524
browser = "chrome"
525
525
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
+
526
541
def test_unsaved_changes (self ):
527
542
"""
528
543
Execute this test using Chrome instead of Firefox.
529
544
Firefox automatically accepts the beforeunload alert, which makes it
530
545
impossible to test the unsaved changes alert.
531
546
"""
532
547
self .login ()
548
+ self ._create_template (default = True , default_values = {"ssid" : "default" })
533
549
device = self ._create_config (organization = self ._get_org ()).device
534
550
path = reverse ("admin:config_device_change" , args = [device .id ])
535
551
536
552
with self .subTest ("Alert should not be displayed without any change" ):
537
553
self .open (path )
538
554
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 ():
544
559
self .fail ("Unsaved changes alert displayed without any change" )
545
560
546
561
with self .subTest ("Alert should be displayed after making changes" ):
@@ -550,10 +565,9 @@ def test_unsaved_changes(self):
550
565
#
551
566
# our own JS code sets e.returnValue when triggered
552
567
# 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 ()
557
571
# simulate hand gestures
558
572
self .find_element (by = By .TAG_NAME , value = "body" ).click ()
559
573
self .find_element (by = By .NAME , value = "name" ).click ()
@@ -562,13 +576,7 @@ def test_unsaved_changes(self):
562
576
# simulate hand gestures
563
577
self .find_element (by = By .TAG_NAME , value = "body" ).click ()
564
578
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 ():
572
580
self .fail ("Unsaved changes code was not executed." )
573
581
574
582
0 commit comments