Loading challenge...
Arm a beforeunload handler on a dirty form and handle the native leave-confirmation in automation
Editing the field arms a beforeunload handler that fires the browser's native 'leave site?' confirm β saving disarms it
Type into the field below and it becomes dirty: a beforeunload listener is registered that calls preventDefault() and sets returnValue, so closing the tab or navigating away pops the browser's native confirmation. Clicking Save marks the work persisted and removes the listener β a clean field leaves silently.
Current state: clean (safe to leave)
Make the field dirty, then try to navigate away
Automation hints
beforeunload dialog unless you register page.on('dialog', β¦) before navigating β without it the prompt is dismissed silently and your test leaks the unsaved state instead of asserting it#guarded-input, dismiss the prompt, stay), and edit-leave (type, accept the prompt, navigate via #leave-link)#guard-state[data-dirty="true"] after filling, and data-dirty="false" after clicking #save-button#save-button is disabled while clean β fill #guarded-input first; saving clears the guard without erasing the text, so a re-edit re-arms it