使用节点执行.exe文件仅在量角器中运行一次 [英] Executing .exe file using node runs only once in protractor

查看:120
本文介绍了使用节点执行.exe文件仅在量角器中运行一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jasmine和量角器编写一些测试,我想在@beforeeach中使用require('child_process')执行.exe文件,然后@aftereach我将重新启动浏览器。
问题是.exe文件只用第一个规范执行一次。
这里是beforeEach的代码()

  beforeEach((done)=> {
console.log(在每个被调用之前);
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(错误);
}
console.log('已执行');
done();

process.on('exit',function(){
child.kill();
console.log(进程被杀死);
});

});

然后我写了2个规格,并在重新启动时重启浏览器

  afterEach(function(){
console.log(关闭浏览器);
browser.restart();
});


解决方案

您应该使用已完成 done.fail 退出async beforeEach 的方法。您开始执行 Test.exe 并立即调用done。这可能会产生不希望的结果,因为该过程仍然可以执行。我不相信 process.on('exit'每个被调用。下面可能会让你使用子进程中的事件发射器开始正确的轨道。



'pre> beforeEach((完成)=> {
常量的execfile =要求( 'child_process')的execfile;

browser.get('URL');

//子类型为ChildProcess
const child = execFile('Test.exe',(error,stdout,stderr)=> ; {
if(error){
done.fail(stderr);
}
console.log(stdout);
});

// ChildProcess有事件发射器,应该用来检查Test.exe
//是否完成,有错误等等。
//参见:https://nodejs.org/ API / child_process.html#child_process_class_childprocess

child.on( '退出',()=> {
DONE();
});
的孩子。 on('error',(err)=> {
done.fail(stderr);
});

});


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

解决方案

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

});

这篇关于使用节点执行.exe文件仅在量角器中运行一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆