Javascript 使用节点执行.exe文件在量角器中仅运行一次
我使用jasmine和量角器编写了一些测试,我希望在@beforeach中使用require('child_process')执行.exe文件,然后@aftereach我将重新启动浏览器。 问题是.exe文件在第一个规范中只执行一次。 以下是beforeach()中的代码 然后我写了两个规格,在每次之后我重新启动浏览器Javascript 使用节点执行.exe文件在量角器中仅运行一次,javascript,node.js,jasmine,protractor,Javascript,Node.js,Jasmine,Protractor,我使用jasmine和量角器编写了一些测试,我希望在@beforeach中使用require('child_process')执行.exe文件,然后@aftereach我将重新启动浏览器。 问题是.exe文件在第一个规范中只执行一次。 以下是beforeach()中的代码 然后我写了两个规格,在每次之后我重新启动浏览器 afterEach(function() { console.log("close the browser"); browser.restart(
afterEach(function() {
console.log("close the browser");
browser.restart();
});
您应该使用
done
和done.fail
方法在每次之前退出异步。您开始执行Test.exe
,并立即调用done。这可能会产生不期望的结果,因为进程可能仍在执行。我不相信process.on('exit'
each被调用。下面可能会让您使用子进程的事件发射器开始正确的工作
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);
});
});
我尝试了您的解决方案。子进程已退出,但只运行了一次。在第二个规范中,它未被执行,因此我们需要有关您的测试的更多信息。我将添加一小段描述,在每个之前,以及它块。
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);
});
});