diff --git a/lib/preload.js b/lib/preload.js
index 75f57266..096ed095 100644
--- a/lib/preload.js
+++ b/lib/preload.js
@@ -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() {
diff --git a/test/fixtures/unload/add-event-listener.html b/test/fixtures/unload/add-event-listener.html
new file mode 100644
index 00000000..3fa52473
--- /dev/null
+++ b/test/fixtures/unload/add-event-listener.html
@@ -0,0 +1,17 @@
+
+
+
+ Unload
+
+
+
+
+
diff --git a/test/fixtures/unload/index.html b/test/fixtures/unload/index.html
new file mode 100644
index 00000000..177b1fd4
--- /dev/null
+++ b/test/fixtures/unload/index.html
@@ -0,0 +1,13 @@
+
+
+
+ Unload
+
+
+
+
+
diff --git a/test/index.js b/test/index.js
index f5238433..bcdbd0a8 100644
--- a/test/index.js
+++ b/test/index.js
@@ -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(){
@@ -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();