从PhantomJS / CasperJS中单独运行nodejs文件



我试图从CasperJS执行nodejs脚本。这应该可以从PhantomJS childprocess库中实现。但这似乎对我不起作用。它没有给出任何错误,但也没有执行节点脚本。因为那个脚本会发送推送通知。当我直接从命令运行脚本时,它工作得很好。但不是从我的CasperJS脚本。

var childProc = require("child_process");
var casper = require('casper').create({
    viewportSize: {width: 800, height: 400}
});
var utils = require("utils");
casper.start('http://www.google.com', function() {
    this.echo('Home page opened');
    this.echo(this.getTitle());
    childProc.execFile('C:\Google Drive\nodejs\push.js', [], null, function (err, stdout, stderr){});
    this.exit();
});
有人知道我做错了什么吗?

你有两个问题:

  • 忘记添加要执行的命令
  • 您退出的太早了,因为execFile是一个异步函数。

试题:

casper.start('http://www.google.com', function() {
    this.echo('Home page opened');
    this.echo(this.getTitle());
    childProc.execFile('C:\Google Drive\nodejs\push.js', [], null, function (err, stdout, stderr){
        utils.dump(arguments);
        casper.echo("Exiting..."):
        casper.exit();
    });
}).run(function(){/* This prevents CasperJS from exiting */});

最新更新