3 回答

TA貢獻1786條經(jīng)驗 獲得超13個贊
我需要根據(jù)預期結(jié)果檢查下載文件的內(nèi)容(本例中為CSV導出),并發(fā)現(xiàn)以下內(nèi)容可以正常工作:
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"
);
});
我發(fā)現(xiàn)Leo的配置建議有助于將下載文件保存在可訪問的位置。
30000毫秒的超時是默認值,因此可以省略,但是我提醒您,以防萬一有人要更改它。
- 3 回答
- 0 關注
- 475 瀏覽
添加回答
舉報