我为Web应用程序的某些部分编写了一个测试。我需要在测试运行过程中运行批处理文件(batch.exe
)。我的测试是:
var exec = require('child_process').execFile;
describe('Sample Test', function () {
describe('When click on browse', function () {
beforeEach(function () {
browser.get('http://192.168.1.152/public/documents');
element(by.linkText('upload')).click();
element(by.css(".dropzone")).click();
browser.sleep(5000);
// <---------- **** this place need to run file
exec('file-upload.exe', function(err, data) {
console.log(err);
console.log(data.toString());
});
element(by.css("button")).click();
});
it('Should be', function () {
expect(element(by.css("span")).getText()).toBe('file uploaded');
});
});
});
我使用了child_process
节点模块,但这不起作用吗?我该怎么办?有什么办法可以解决这个问题?
答案 0 :(得分:0)
我用以下代码解决了问题:
var exec = require('child_process').execFile;
describe('Sample Test', function () {
describe('When click on browse', function () {
beforeEach(function () {
browser.get('http://192.168.1.152/public/documents');
element(by.linkText('upload')).click();
element(by.css(".dropzone")).click();
browser.sleep(5000);
// <---------- **** this place need to run file
setTimeout(function () {
execFile('file-upload.exe', function(error, stdout, stderr) {
if (error) {
throw error;
}
console.log(stdout);
});
},3000);
element(by.css("button")).click();
});
it('Should be', function () {
expect(element(by.css("span")).getText()).toBe('file uploaded');
});
});
});
由于execFile在beforeAll
函数之前运行,并且我需要将其暂停几秒钟,因此我将execFile
函数放入setTimeOut
进行延迟。