【问题标题】:Protractor test does not wait for file to be downloaded量角器测试不等待文件下载
【发布时间】: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


    【解决方案1】:

    await fs.existsSync(excelFile); 只是在等待 fs.existsSync 函数完成(如果文件存在则返回 true),而不是真正等待文件存在。

    您可以尝试添加一个 browser.wait(),它会像这样等待文件下载。

    await browser.wait(async function () {
        return await fs.existsSync(excelFile);
    }, 30*1000, "File has not downloaded within 30 seconds")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-06
      • 2014-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-26
      • 1970-01-01
      相关资源
      最近更新 更多