@@ -288,10 +288,9 @@ class TestDeviceAdminUnsavedChanges(
288288):
289289 browser = 'chrome'
290290
291- def test_unsaved_changes (self ):
291+ def test_unsaved_changes (self ):
292292 """
293293 Execute this test using Chrome instead of Firefox.
294-
295294 Firefox automatically accepts the beforeunload alert, which makes it
296295 impossible to test the unsaved changes alert.
297296 """
@@ -309,6 +308,13 @@ def test_unsaved_changes(self):
309308 self .fail ('Unsaved changes alert displayed without any change' )
310309
311310 with self .subTest ('Alert should be displayed after making changes' ):
311+ # The WebDriver automatically accepts the
312+ # beforeunload confirmation dialog. To verify the message,
313+ # we log it to the console and check its content.
314+ self .web_driver .execute_script (
315+ 'django.jQuery(window).on("beforeunload", function(e) {'
316+ ' console.warn(e.returnValue); });'
317+ )
312318 # simulate hand gestures
313319 self .find_element (by = By .TAG_NAME , value = 'body' ).click ()
314320 self .find_element (by = By .NAME , value = 'name' ).click ()
@@ -317,14 +323,14 @@ def test_unsaved_changes(self):
317323 # simulate hand gestures
318324 self .find_element (by = By .TAG_NAME , value = 'body' ).click ()
319325 self .web_driver .refresh ()
320- try :
321- WebDriverWait ( self . web_driver , 5 ). until ( EC . alert_is_present ())
322- except TimeoutException :
323- for entry in self . get_browser_logs ():
324- print ( entry )
325- self . fail ( 'Timed out wating for unsaved changes alert' )
326+ for entry in self . get_browser_logs () :
327+ if (
328+ entry [ 'level' ] == 'WARNING'
329+ and "You haven \' t saved your changes yet!" in entry [ 'message' ]
330+ ):
331+ break
326332 else :
327- self .web_driver . switch_to . alert . accept ( )
333+ self .fail ( 'Unsaved changes code was not executed.' )
328334
329335
330336@tag ('selenium_tests' )
0 commit comments