0

In my tests, the Protractor Test script is clicking on a button to download a file.

Bellow is the code snippet:

var today = new Date(),
    timeStamp = moment(today).format('MMDDYYYY');
let G = GV;
let file = './downloaded-files/StudentList'+timeStamp+'.xlsx';
let Worksheet = 'StudentList'+timeStamp+'.pdf';
let XL = require('exceljs');
let Workbook = new XL.Workbook();

let RowLength= 0;
 =======
 G.Excel_Button.click().then(function () {

    browser.driver.wait(function () {
        return fs.existsSync(file);
    }).then(function () {
        readExcelFile()
    });

function readExcelFile() {
    try {
        expect(fs.existsSync(file)).toBe(true);
        Workbook.xlsx.readFile(file).then(function () {
            var worksheet = Workbook.getWorksheet(Worksheet);
            worksheet.rowCount.then(function(RC){
                console.log('\nTotal rows in the workbook is:  ' + RC + '\n');
            });

            expect(worksheet.actualRowCount).toBe(RowLength + 1);
        });
    } catch (err) {
        reject();
    }
}

It is apparent that the G.Excel_Button.click() causes the timeout error

 ScriptTimeoutError: script timeout: result was not received in 11 seconds

Also, the log shows the following unhandled promise rejections :

   (node:33984) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'rowCount' of undefined
    at C:\Protractor\specs\TestBed.js:132:23
(node:33984) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not
handled with .catch(). (rejection id: 1)
(node:33984) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:33984) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
.(node:33984) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'rowCount' of undefined
    at C:\Protractor\specs\TestBed.js:132:23
(node:33984) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not
handled with .catch(). (rejection id: 2)

I have spent many hours to resolve the timeout error and tried all the solutions that I could find including https://github.com/angular/protractor/blob/master/docs/timeouts.md, but nothing was successful.

Is there any way to resolve this?

nhrcpt
  • 862
  • 3
  • 21
  • 51

2 Answers2

0

Could you try this code and see what it produces?

var today = new Date(),
    timeStamp = moment(today).format('MMDDYYYY');
let G = GV;
let file = './downloaded-files/StudentList' + timeStamp + '.xlsx';
let Worksheet = 'StudentList' + timeStamp + '.pdf';
let XL = require('exceljs');
let Workbook = new XL.Workbook();

let RowLength = 0;
G.Excel_Button.click().then(function () {

    browser.driver.wait(function () {
        return fs.existsSync(file);
    }, 10 * 1000, `File path '${file}' did not get created within 10 seconds.`).then(function () {
        console.log('File Exists');

        Workbook.xlsx.readFile(file).then(function () {
            console.log('Reading File');

            var worksheet = Workbook.getWorksheet(Worksheet);
            worksheet.rowCount.then(function (RC) {
                console.log('\nTotal rows in the workbook is:  ' + RC + '\n');
                expect(RC).toBe(RowLength + 1);
            });
        });
    });
})
DublinDev
  • 2,318
  • 2
  • 8
  • 31
  • It did not work for me. the log is the same as what we got earlier. Actually, the file is being downloaded and the app can read the file, but it looks like something is going on in the background. I can share my full code with you in the chat window if that helps. – nhrcpt Feb 21 '19 at 18:42
  • Good to know. Before sharing that could you explain why the reject() function is there within the catch block? – DublinDev Feb 22 '19 at 13:48
  • Actually, I was following some advice on SO so as a trial I put this try/catch block. Not sure what it is returning, – nhrcpt Feb 22 '19 at 17:49
  • Those reject() callbacks are typical uses when declaring [promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) so don't appear to apply here. The try catch can also be removed. Could you confirm for me the behavior you are expecting is to download the file and get the total count of the rows inside? Also I've updated my answer so could you take a look and see if that helps at all – DublinDev Feb 22 '19 at 18:41
0

solution 1" - Write the async function and use await. This will definately solve your async issue solution 2: - try writing return statement as below this will resolve the promise