'How do supply all the CSV data files under folder to the newman script and execute?

What I have done?

I have a collection which needs to be executed for a set of data sets which is passed from a CSV data file. I am able to execute with 1 CSV data file.

What I am looking for?

I need to execute the set of CSV files from the folder iteratively and supply the file to the newman script file.

How the newman script looks like?

    var fs = require('fs'),
    cheerio = require('cheerio'),
    path = require('path'),
    newman = require('newman'),
    os = require("os");

const directoryPath = path.join(__dirname, 'payload');

fs.readdir(directoryPath, function(err, files) {
    if (err) {
        return console.log('Invalid directory: ' + err);
    }
    files.forEach(function(file) {
        var dataFile = 'payload/' + file + ".csv";
        var dataLog = 'payload/' + file + ".txt";
        newman.run({
                reporters: 'cli',
                collection: 'valid-routes-finder.json',
                iterationData: dataFile,
                delayRequest: 500,
                iterationCount: 2
            })
            .on('start', function(err, args) {
                console.log(args);
                console.log(err);
            })
            .on('console', function(err, args) {
                var dataSupplied = args.messages[0];
                var totalRecords = dataSupplied.split(",")[0];
                if (parseInt(totalRecords) > 0) {
                    fs.appendFileSync(dataLog, dataSupplied);
                    fs.appendFileSync(dataLog, '\r\n');
                }
            })
            .on('error', function(err, summary) {
                console.log(err);
            })
            .on('done', function(err, summary) {
                console.log("Execution completed.");
            });
    });
});

Support Needed:

I would like to pass the data file dynamically to the newman to script to run for each CSV data file.



Solution 1:[1]

I found a way to achieve this. Sorry for being stupid. Declared a method and called.

var fs = require('fs'),
    path = require('path');
cheerio = require('cheerio'),
    newman = require('newman'),
    os = require("os");

const directoryPath = path.join(__dirname, 'payload');

fs.readdir(directoryPath, function(err, files) {
    if (err) {
        return console.log('Unable to scan directory: ' + err);
    }
    files.forEach(function(file) {
        runNewman(file);
    });
});

function runNewman(data) {

    var csvFile = "payload/" + data;
    var logFile = "payload/" + data + ".txt";

    newman.run({
            reporters: ['htmlextra', 'cli'],
            collection: 'valid-routes-finder.json',
            iterationData: csvFile,
            delayRequest: 100
        })
        .on('console', function(err, args) {
            var dataSupplied = args.messages[0];
            var totalRecords = dataSupplied.split(",")[0];
            if (parseInt(totalRecords) > 0) {
                fs.appendFileSync(logFile, dataSupplied);
                fs.appendFileSync(logFile, '\r\n');
            }
        })
        .on('done', function(err, summary) {
            console.log("Execution completed.");
        });
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ramkumar-yoganathan