我使用 jasmine 和 Protractor 编写了一些测试,我希望在 @beforeach 中使用 require('child_process') 执行 .exe 文件,然后 @aftereach 我将重新启动浏览器。 问题是 .exe 文件在第一个规范中只执行一次。 这是 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");
});
});
然后我写了 2 个规范,之后我重新启动浏览器
afterEach(function() {
console.log("close the browser");
browser.restart();
});
最佳答案
您应该使用done
和done.fail
方法退出异步beforeEach
。您开始执行 Test.exe
并立即调用 done。这可能会产生不良结果,因为进程可能仍在执行。我不相信 process.on('exit'
每次都会被调用。下面可能会让您使用子进程中的事件发射器走上正确的轨道。
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);
});
});
关于javascript - 使用 Node 执行.exe文件仅在 Protractor 中运行一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42468965/