Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

console.log doesn't work in CasperJS' evaluate with setTimeout

Why when I use console.log in evaluate, it works:

casper.then(function() {   this.evaluate( function() {     console.log('hello');    }); }); 

But this doesn't work:

casper.then(function() {   this.evaluate( function() {     setTimeout( function() {console.log('hello');}, 1000);   }); }); 
like image 587
Rustem Avatar asked Aug 08 '12 12:08

Rustem


2 Answers

Because you're mixing up casperjs and remote page environments. The evaluate function will execute code within the remote page env, so the console.log call won't output anything.

If you want to catch remote console.log calls, listen to the remote.message event:

casper.on('remote.message', function(msg) {     this.echo('remote message caught: ' + msg); }) 

Btw, documentation for events is pretty much exhaustive, as well as the one for evaluate.

like image 131
NiKo Avatar answered Sep 24 '22 12:09

NiKo


@NiKo's answer is critical.

I would also suggest adding the following, since if there's an error, you might not even make it far enough to print out a console.log() msg, and instead end up with silence.

casper.on( 'page.error', function (msg, trace) {     this.echo( 'Error: ' + msg, 'ERROR' ); }); 
like image 37
odigity Avatar answered Sep 22 '22 12:09

odigity