Executing .exe file using node runs only once in p

2019-08-04 06:30发布

I write some tests using jasmine and protractor i want in the @beforeeach to execute .exe file using require('child_process') and then @aftereach i will restart the browser. The problem is that the .exe file is executed only once with the first spec. here is the code in the beforeEach()

beforeEach((done) => {
    console.log("before each is called");
    var exec = require('child_process').execFile;

    browser.get('URL'); 
    console.log("fun() start");
    var child = exec('Test.exe', function(err, data) {
        if (err) {
            console.log(err);
        }
        console.log('executed');
        done();

        process.on('exit', function() {
            child.kill();
            console.log("process is killed");
        });

    });

Then i wrote 2 specs and in the aftereach i restart the browser

afterEach(function() {
        console.log("close the browser");
        browser.restart();
    });

1条回答
劳资没心,怎么记你
2楼-- · 2019-08-04 07:28

You should use the done and done.fail methods to exit the async beforeEach. You begin to execute Test.exe and immediately call done. This could have undesired results since the process could still be executing. I do not believe process.on('exit' every gets called. Below might get you started on the right track using event emitters from the child process.

beforeEach((done) => {
  const execFile = require('child_process').execFile;

  browser.get('URL'); 

  // child is of type ChildProcess
  const child = execFile('Test.exe', (error, stdout, stderr) => {
    if (error) {
      done.fail(stderr);
    }
    console.log(stdout);
  });

  // ChildProcess has event emitters and should be used to check if Test.exe
  // is done, has an error, etc.
  // See: https://nodejs.org/api/child_process.html#child_process_class_childprocess

  child.on('exit', () => {
    done();
  });
  child.on('error', (err) => {
    done.fail(stderr);
  });

});
查看更多
登录 后发表回答