Skip to content

Instantly share code, notes, and snippets.

@Wirone
Created February 17, 2014 11:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Wirone/9048945 to your computer and use it in GitHub Desktop.
Save Wirone/9048945 to your computer and use it in GitHub Desktop.
CasperJS onbeforeunload: not catched alert issue
<!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>
<!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>
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