【发布时间】:2019-04-01 18:43:14
【问题描述】:
我正在测试下载 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', 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 文件,并且我在第一个规范中禁用了取消链接,则测试通过,没有任何问题。但是如果文件夹中没有这个文件,第二个规范必须等待第一个规范下载文件,或者如果在第一个规范中激活了取消链接功能,它不会等待并返回以下内容:
.错误:找不到文件:./downloaded-files/StudentList03312019.xlsx 在 C:\ProtractorTests\node_modules\exceljs\lib\xlsx\xlsx.js:60:17
看起来异步等待不适用于第二个规范。如何使规范在执行之前等待文件生成?
【问题讨论】:
标签: javascript protractor