Created
February 17, 2014 11:27
-
-
Save Wirone/9048945 to your computer and use it in GitHub Desktop.
CasperJS onbeforeunload: not catched alert issue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>CasperJS - onbeforeunload issue</title> | |
<meta name="author" value="Grzegorz 'Wirone' Korba" /> | |
<script type="text/javascript">alert('Just alerting...');</script> | |
</head> | |
<body> | |
<h1>Page without onbeforeunload</h1> | |
<a class="next" href="page_onbeforeunload.html">Next</a> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> | |
<html> | |
<head> | |
<title>CasperJS - onbeforeunload issue</title> | |
<meta name="author" value="Grzegorz 'Wirone' Korba" /> | |
</head> | |
<body> | |
<script type="text/javascript"> | |
function beforeExit() { | |
window.alert("This should be catched by casper.on('remote.alert')"); | |
return 'Do you really want to exit?'; | |
} | |
window.onbeforeunload = beforeExit; | |
document.getElementsByTagName("body")[0].onunload = beforeExit; | |
</script> | |
<h1>Page with onbeforeunload</h1> | |
<a class="next" href="page_end.html">Next</a> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var casper = require('casper').create({ | |
verbose: true, | |
waitTimeout: 1000, | |
viewportSize: { | |
width: 1024, | |
height: 768 | |
} | |
}); | |
casper.on('remote.alert', function(message) { this.echo('Remote alert: ' + message); }); | |
casper.on('remote.message', function(msg) { this.echo('Remote console.log: ' + msg); }); | |
casper.on('load.finished', function(status) { | |
this.echo('[load.finished] ' + status); | |
}); | |
casper.start('page.html', function(){ | |
this.log('Casper started'); | |
this.wait(1000, function() { | |
this.echo('Waiting.'); | |
this.click('body a.next'); | |
//this.open('page_onbeforeunload.html'); | |
}); | |
}); | |
casper.wait(1000, function() { | |
this.echo('Waiting.'); | |
// Works this way... | |
//try { notExistingFunction(); } catch (e) { casper.echo(e); } | |
// Without try/catch it throws alert | |
notExistingFunction(); | |
this.click('body a.next'); | |
}); | |
casper.run(function() { | |
this.exit(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment