0

Not sure if the Title is quite right in this as I'm pretty much stumped (in over my head)...

I'm trying to pull the headers from a csv, as part of an automation test, to the validate those headers. I'm using csv-parse to read the csv file.

Once I've gathered the headers I'm then doing a simple assertion to go through and assert against each one. Using the string values I've entered into my test script.

However currently, the FOR is executing before the csv read and headers have been gathered. I'm not sure how to wait for this to finish before executing the loop.

const fs = require('fs');
const csv = require('csv-parser');
let headerArray = null;
const headerValues = values.split(',');
browser.pause(10000);
fs.createReadStream(""+global.downloadDir + "\\" + fs.readdirSync(global.downloadDir)[0])
  .pipe(csv())
  .on('headers', function (headers) {
    return headerArray = headers
  })
for(var i =0; i<headerValues.length; i++){
 assert.equal(headerValues[i], headerArray[i]);
}
Grubsniff
  • 25
  • 4

1 Answers1

1

The solution is to run the for loop with your assertions inside the 'headers' event handler, eg:

var results = [] // we'll collect the rows from the csv into this array

var rs = fs.createReadStream(""+global.downloadDir + "\\" + fs.readdirSync(global.downloadDir)[0])
  .pipe(csv())
  .on('headers', function (headers) {
    try {
      for(var i =0; i<headerValues.length; i++){
        assert.equal(headerValues[i], headers[i]);
      }
    } catch (err) {
      // an assertion failed so let's end
      // the stream (triggering the 'error' event)
      rs.destroy(err)
    }
  }).on('data', function(data) {
    results.push(data)
  }).on('end', function() {
    //
    // the 'end' event will fire when the csv has finished parsing. 
    // so you can do something useful with the `results` array here...
    //
  }).on('error', function(err) {
    // otherwise, the 'error' event will have likely fired.
    console.log('something went wrong:', err)
  })
ant
  • 1,140
  • 12
  • 19
  • Appreciate this, and can see that it's actually executing the Assertion! Thank you! However it's now not "failing" correctly when the assertion doesn't pass. The session is remaining open. Do you know how would I end the stream and close out of this completely? – Grubsniff Nov 17 '20 at 15:22
  • We'd have to manually tear down the readable stream if an assertion error is caught. Notice how the readable stream is now assigned to a variable called `rs`, which we can then use later on. – ant Nov 17 '20 at 16:30
  • Damn dude! Thanks!! Appreciate the comments in there too, help massively! EDIT: Sadly this doesn't seem to resolve my issue, but I think it might be a webdriver issue rather than this code. As the browser session seems to not close on the failure as it normally would. Even when the console output shows shutting down the browser has happened. – Grubsniff Nov 17 '20 at 16:57