如何使用nodejs child_process exec访问黄瓜步骤定义中的stdout、stderr和error



我是Cucumber.js的新手,试图在步骤定义中执行shell命令。下面的示例是一个步骤定义片段,Cucumber.js不打印标准输出。我基本上需要在一步中访问标准输出和标准错误。

var exec = require('child_process').exec;
this.Given(/^XYZ server is running$/, function(callback) {
child = exec('pwd', function (error, stdout, stderr) {
  console.log('stdout: ' + stdout);
  console.log('stderr: ' + stderr);
  if (error !== null) {
    console.log('exec error: ' + error);
  }
});
callback();
});

看起来像一个关于nodejitsu的好例子。

 var childProcess = require('child_process'),
     ls;
 ls = childProcess.exec('ls -l', function (error, stdout, stderr) {
   if (error) {
     console.log(error.stack);
     console.log('Error code: '+error.code);
     console.log('Signal received: '+error.signal);
   }
   console.log('Child Process STDOUT: '+stdout);
   console.log('Child Process STDERR: '+stderr);
 });
 ls.on('exit', function (code) {
   console.log('Child process exited with exit code '+code);
 });

最新更新