3 回答
TA贡献1811条经验 获得超6个赞
我需要根据预期结果检查下载文件的内容(本例中为CSV导出),并发现以下内容可以正常工作:
var filename = '/tmp/export.csv';
var fs = require('fs');
if (fs.existsSync(filename)) {
// Make sure the browser doesn't have to rename the download.
fs.unlinkSync(filename);
}
$('a.download').click();
browser.driver.wait(function() {
// Wait until the file has been downloaded.
// We need to wait thus as otherwise protractor has a nasty habit of
// trying to do any following tests while the file is still being
// downloaded and hasn't been moved to its final location.
return fs.existsSync(filename);
}, 30000).then(function() {
// Do whatever checks you need here. This is a simple comparison;
// for a larger file you might want to do calculate the file's MD5
// hash and see if it matches what you expect.
expect(fs.readFileSync(filename, { encoding: 'utf8' })).toEqual(
"A,B,C\r\n"
);
});
我发现Leo的配置建议有助于将下载文件保存在可访问的位置。
30000毫秒的超时是默认值,因此可以省略,但是我提醒您,以防万一有人要更改它。
添加回答
举报