Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent unload dialogs (...from preventing .end()) #825

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions lib/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,29 @@ window.addEventListener('error', function(e) {
});

(function(){
// prevent 'unload' and 'beforeunload' from being bound
var defaultAddEventListener = window.addEventListener;
window.addEventListener = function (type) {
if (type === 'unload' || type === 'beforeunload') {
return;
}
defaultAddEventListener.apply(window, arguments);
};

// prevent 'onunload' and 'onbeforeunload' from being set
Object.defineProperties(window, {
onunload: {
enumerable: true,
writable: false,
value: null
},
onbeforeunload: {
enumerable: true,
writable: false,
value: null
}
});

// listen for console.log
var defaultLog = console.log;
console.log = function() {
Expand Down
17 changes: 17 additions & 0 deletions test/fixtures/unload/add-event-listener.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<title>Unload</title>
<script>
var unloadHandler = function (event) {
var confirmationMessage = 'Are you sure?';
event.returnValue = confirmationMessage;
return confirmationMessage;
};
window.addEventListener('unload', unloadHandler);
window.addEventListener('beforeunload', unloadHandler);
</script>
</head>
<body>
</body>
</html>
13 changes: 13 additions & 0 deletions test/fixtures/unload/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<title>Unload</title>
<script>
window.onunload = window.onbeforeunload = function () {
return 'Are you sure?';
};
</script>
</head>
<body>
</body>
</html>
16 changes: 15 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ describe('Nightmare', function () {
it('should gracefully handle electron being killed', function(done) {
var child = child_process.fork(
path.join(__dirname, 'files', 'nightmare-unended.js'));

child.once('message', function(electronPid) {
process.kill(electronPid, 'SIGINT');
child.once('exit', function(){
Expand Down Expand Up @@ -146,6 +146,20 @@ describe('Nightmare', function () {
.then(() => done());
});

it('should successfully end on pages setting onunload or onbeforeunload', function(done) {
var nightmare = Nightmare();
nightmare.goto(fixture('unload'))
.end()
.then(() => done());
});

it('should successfully end on pages binding unload or beforeunload', function(done) {
var nightmare = Nightmare();
nightmare.goto(fixture('unload/add-event-listener.html'))
.end()
.then(() => done());
});

it('should provide useful errors for .click', function(done) {
var nightmare = Nightmare();

Expand Down