本文介绍了如何使用nodejs child_process exec访问cucumber.js步骤定义中的stdout,stderr和错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  var exec = require('child_process')。 

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();
});看起来像是。

  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);
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);
}


I am new to Cucumber.js, trying to execute a shell command in a step definition. Sample below is a step definition snippet, Cucumber.js does not print stdout. I basically need to access stdout and stderr in a step.

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();
});
解决方案

Looks like a good example on 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);
 });

这篇关于如何使用nodejs child_process exec访问cucumber.js步骤定义中的stdout,stderr和错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 06:09