Executing .exe file using node runs only once in protractor

你说的曾经没有我的故事 提交于 2019-12-14 04:20:10

问题


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:


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

});


来源:https://stackoverflow.com/questions/42468965/executing-exe-file-using-node-runs-only-once-in-protractor

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