Ad

Can't Get Console.log Working Inside Evaluate In CasperJS

- 1 answer

I at testing some stuff with casperjs, and I am having troubles debugging simple things, I want to run a console.log inside evaluate, read some examples but still can't get it working. This is my test code:

var casper = require("casper").create();
var system = require('system');
var url = casper.cli.get(0);

var casper = require('casper').create({
    verbose: true,
    logLevel: "debug"
});

casper.onConsoleMessage = function(msg) {
    system.stderr.writeLine('console: ' + msg);
};

casper.start(url, function () {
    this.evaluate(function() { console.log('test'); });
});

casper.run(function() {
    console.log('done');
    this.exit();
});

I do get the "done" printed, but never the "test", what am I doing wrong?

Ad

Answer

You can try the below solution as well

//Code to display Console errors
casper.on('remote.message', function (msg) {
     console.log('remote message caught: ' + msg);
 });

//Code to display errors from the page
 casper.on('page.error', function (msg, trace) {
     console.log('Error: ' + msg, 'ERROR');
 });
Ad
source: stackoverflow.com
Ad