全部then*
和wait*
函數是要執行的調度步驟。當CasperJS用完執行步驟並且沒有功能被傳入casper.run()
時,它會自動退出。
最簡單的解決將是總是傳遞一個空函數爲casper.run()
並計劃從函數內部新步驟:
casper.then(function(){
var p = cp.execFile('/path/parse', [], {}, function(error, stdout, stderr) {
console.log(stdout);
casper.thenOpen(parsedUrl).then(function(){
// do something on page
});
});
});
casper.run(function(){});
一個更清潔的方法是寫自己的階躍函數封裝了execFile
功能:
casper.waitForFileExec = function(process, callback, onTimeout, timeout){
this.then(function(){
var cp = require('child_process'),
finished = false,
self = this;
timeout = timeout === null || this.options.stepTimeout;
cp.execFile(process, [], {}, function(error, stdout, stderr) {
finished = true;
callback.call(self, error, stdout, stderr);
});
this.waitFor(function check(){
return finished;
}, null, onTimeout, timeout);
});
return this; // for builder/promise pattern
};
...
casper.waitForFileExec('/path/parse', function(error, stdout, stderr) {
this.echo(stdout);
this.thenOpen(parsedUrl).then(function(){
// do something on page
});
}).run();
這個答案應該是一個可以接受的答案。 – 2016-10-23 00:03:58