Javascript 量角器测试不会等待文件下载
我正在测试,当下载Excel文件时,它应该验证文件中的数据。我有两个规格:Javascript 量角器测试不会等待文件下载,javascript,protractor,Javascript,Protractor,我正在测试,当下载Excel文件时,它应该验证文件中的数据。我有两个规格: it('should be able to download the excel file of the student data', async function(){ expect( homePage.ExcelFieDownloaded()).toBe(true); }); it('Should match total records in Excel File with the data table
it('should be able to download the excel file of the student data', async function(){
expect( homePage.ExcelFieDownloaded()).toBe(true);
});
it('Should match total records in Excel File with the data table', async function(){
expect (await regDBPage.NumberOfRecordsinExcelFile()).toBe(await regDBPage.getCountofRecordsinDatabase(await regDBPage.userName())+1)
});
excel文件下载+读取的方式有:
this.ExcelFieDownloaded = async function(){
var today = new Date(),
timeStamp = moment(today).format('MMDDYYYY');
let file = './downloaded-files/StudentList'+timeStamp+'.xlsx';
let Worksheet = 'StudentList'+timeStamp+'.pdf';
let XL = require('exceljs');
let Workbook = new XL.Workbook();
let RowLength= 0;
if(fs.existsSync(file)){
fs.unlinkSync(file);
}else{
console.log('There is no pre-existing .xlsx file in the directory ');
}
await sync.waitUntilElementClickable(locator.ExcelButton, 5000);
await locator.ExcelButton.click();
let file_found = await fs.existsSync(file);
return (await file_found)
};
this.NumberOfRecordsinExcelFile = async function(){
const filePath ='./downloaded-files/StudentList'+timeStamp+'.xlsx';
try{
let excelFile = await filePath;
await fs.existsSync(excelFile);
let WB = await Workbook.xlsx.readFile(await excelFile);
let WS = await WB.getWorksheet(1);
let RC = await WS.actualRowCount;
console.log('Number of actual data in excel file: ' + RC);
return await RC
} catch (err) {
console.log(err);
}
}
如果所需的excel文件在测试开始时已经存在于文件夹中,并且我在第一个规范中禁用了取消链接,那么测试将顺利通过。但是,如果文件夹中没有此类文件,第二个规范必须等待第一个规范下载该文件,或者如果在第一个规范中激活了取消链接功能,则它不会等待并返回以下内容:
。错误:找不到文件:./download files/StudentList03312019.xlsx
在C:\granterTests\node\u modules\exceljs\lib\xlsx\xlsx.js:60:17
看起来async await不适用于第二个规范。如何使规范在执行之前等待生成文件 等待fs.existsSyncexcelFile;仅等待fs.existsSync函数完成,如果文件确实存在,则返回true,而不是实际等待文件存在
您可以尝试添加一个browser.wait,它将像这样等待文件下载
await browser.wait(async function () {
return await fs.existsSync(excelFile);
}, 30*1000, "File has not downloaded within 30 seconds")