2017-08-10 46 views
0

我使用CasperJS來捕獲瀏覽器控制檯的日誌,但它不起作用。如何在CasperJS中輸出客戶端console.log?

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

var domain, port, userName, designID, token, outPath, reportUrl, explorerUrl, sourcePage; 
var images = []; 

if(casper.cli.has(5)){ 
    // Get argvs 
    domain = casper.cli.get(0).toLowerCase(); 
    port = casper.cli.get(1); 
    userName = casper.cli.get(2); 
    designID = casper.cli.get(3); 
    token = casper.cli.get(4); 
    outPath = casper.cli.get(5); 
} 

reportUrl = 'http://' + domain + ':' + port + '/design?file_id=' + designID; 

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

casper.start(reportUrl, function() { 
    this.evaluate(function sendLog(log) { 
    // you can access the log from page DOM 
    console.log('from the browser, I can tell you there are ' + 
    log.length + ' entries in the log'); 
    }, this.result.log); 
}); 

casper.run(); 

在Chrome中,頁面會輸出一些日誌,如:

get userdata 
get designdata 
Success! 

但在CasperJS,它不輸出任何日誌。

回答

0

如果用echo替換log,你應該看到下面(簡化)腳本工作:

var casper = require('casper').create(); 

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

casper.start('http://example.com/'); 

casper.waitForSelector('body', function() { 
    this.evaluate(function() { 
    console.log('Hello, World!'); 
    }); 
}); 

casper.run(); 

但是,如果你真的想使用log,你需要指定與沿verbose選項日誌級別...

在命令行:

casperjs --log-level=debug --verbose=true example.js 

來自腳本:

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