how to access stdout,stderr, and error in cucumber.js step definition using nodejs child_process exec

房东的猫 提交于 2019-12-12 14:42:18

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/14466659/how-to-access-stdout-stderr-and-error-in-cucumber-js-step-definition-using-node

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!