4

我遇到了问题,甚至不确定从哪里开始进行故障排除。

我正在使用稍微修改过的 mocha-casperjs。CasperJS 是 PhantomJS 的包装器。我正在尝试在完成测试时集成 Growl 通知。

我可以在调用 mocha.run 之前成功执行通知,如下所示:

execFile("terminal-notifier", ["-message", "Tests Begin"], null, function (err, stdout, stderr) {
  console.log("execFileSTDOUT:", JSON.stringify(stdout))
  console.log("execFileSTDERR:", JSON.stringify(stderr))
})



// for convience, expose the current runner on the mocha global
    mocha.runner = mocha.run(function() {
    ...

但是,这失败了:

// for convience, expose the current runner on the mocha global
mocha.runner = mocha.run(function() {
    execFile("terminal-notifier", ["-message", "Tests Begin"], null, function (err, stdout, stderr) {
      console.log("execFileSTDOUT:", JSON.stringify(stdout))
      console.log("execFileSTDERR:", JSON.stringify(stderr))
    })
    ...

我不太了解 Mocha 或 PhantomJS 的内容。Mocha 是否会吃掉标准输出或类似的东西,导致 execFile 调用失败?还有什么我没有得到的吗?

谢谢,凯文

- - 更新 - -

情节变厚了。仅仅包括 casper 对象就会杀死 execFile。

使用“casperjs test.js”运行以下代码成功输出 execFile。取消注释 casper 对象不会导致输出。

'use strict';
var process = require("child_process");
var spawn = process.spawn;
var execFile = process.execFile;

execFile("ls", ["-lF", "/usr"], null, function (err, stdout, stderr) {
  console.log("execFileSTDOUT:", JSON.stringify(stdout));
  console.log("execFileSTDERR:", JSON.stringify(stderr));
});

//var casper = require('casper').create();
//casper.exit();
4

2 回答 2

1

我遇到了同样的问题。 execFile异步运行,所以你需要给它一个执行的机会。通过立即退出 CasperJS,没有输出,因为 ls 没有完成,所以没有调用回调。一种解决方法是使用setTimeout. 这是我在 PhantomJS 中所做的。我认为对于 CasperJS 来说也是一样的。

'use strict';
var process = require("child_process");
var spawn = process.spawn;
var execFile = process.execFile;

execFile("ls", ["-lF", "/usr"], null, function (err, stdout, stderr) {
  console.log("execFileSTDOUT:", JSON.stringify(stdout));
  console.log("execFileSTDERR:", JSON.stringify(stderr));
});

setTimeout(function() { phantom.exit(0) },1000);
于 2016-12-09T19:01:00.047 回答
-1

您可以尝试将日志行更改为此:

console.log("execFileSTDOUT:" + JSON.stringify(stdout));

然后,您应该看到,如果调用了 execFile: execFileSTDOUT:""

于 2016-03-07T11:37:13.497 回答