Using newman programmatically with iterationData, not making requests

I have a collection with a single request: https://api.onlinedatabasetool.com/api/catalogline/{{id}}

When I run newman using the command line with a data file I successfully make 30 requests:
newman run Product-enviroment.postman_collection.json -d response.json

When I try to run the same collection programmatically it runs 30 iterations (which corresponds to the amount of rows in the data file) but it is not actually doing the requests, just iterations.

This is the file I’m executing:

const newman = require('newman'),
          fs = require('fs');
    newman.run({
        collection: require('./Product-enviroment.postman_collection.json'),
        iterationData: './response.csv',
        reporters: 'cli'
    }).on('beforeRequest', function (error, args) {
        console.log(args.request.body.raw);
    }).on('request', function (error, args) {
        console.log(args.response.stream);
    });

Here is an article which is where I got the file from:

Not sure what I’m doing wrong.

1 Like

Hey @vandenbusken

Great to see you on the community forum :wave:

In order to write something to a file using the fs module, you would need to add the function you require to the script itself. You’re requiring it but not actually using it. :smiley:

Taking the script from that post you shared, it’s using fs.writeFile() to create and write that file locally.

const newman = require('newman'),
      fs = require('fs');

newman.run({
    collection: require('./postman_collection.json'),
    reporters: 'cli'
}).on('beforeRequest', function (error, args) {
    if (error) {
        console.error(error);
    } else {
        fs.writeFile(`request.txt`, args.request.body.raw, function (error) {
            if (error) { 
                console.error(error); 
            }
        });    
    }
}).on('request', function (error, args) {
    if (error) {
        console.error(error);
    }
    else {
        fs.writeFile(`response.txt`, args.response.stream, function (error) {
            if (error) { 
                console.error(error); 
            }
        });        
    }
});

As you’re looping through a datafile and making the same requests 30 times, it’s going to overwrite the data in the file and you’ll end up with the data of the last request. You would need to make the filename unique if you want to capture each individual request in its own file.

That’s mentioned in the last part of the post and a basic solution is offered that would add a random number to the filename:

const newman = require('newman'),
      fs = require('fs'),
      randomNumber = Math.random().toString(36).substring(7);

newman.run({
        collection: require('./Product-enviroment.postman_collection.json'),
        iterationData: './response.csv',
        reporters: 'cli'
}).on('beforeRequest', function (error, args) {
    if (error) {
        console.error(error);
    } else {
        fs.writeFile(`${randomNumber}-request.txt`, args.request.body.raw, function (error) {
            if (error) { 
                console.error(error); 
            }
        });    
    }
}).on('request', function (error, args) {
    if (error) {
        console.error(error);
    }
    else {
        fs.writeFile(`${randomNumber}-response.txt`, args.response.stream, function (error) {
            if (error) { 
                console.error(error); 
            }
        });        
    }
});
1 Like

Hi @danny-dainton,

Thanks for the reply.

I actually removed some of the content of the write js file because i thought it wasn’t relevant for the question. When replace my file with your code the same thing still happens. The iterations run but not a request with variable data.

This is the result:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ β”‚ executed β”‚ failed β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ iterations β”‚ 30 β”‚ 0 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ requests β”‚ 0 β”‚ 0 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ test-scripts β”‚ 30 β”‚ 0 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ prerequest-scripts β”‚ 0 β”‚ 0 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ assertions β”‚ 0 β”‚ 0 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ total run duration: 446ms β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ total data received: 0B (approx) β”‚

It runs iterations like this:
Iteration 30/30
β†’ https://api.onlinedatabasetool.com/api/catalogline/{{id}}

when I run newman from the command line it runs like this:
β†’ https://api.onlinedatabasetool.com/api/catalogline/{{id}}
GET https://api.onlinedatabasetool.com/api/catalogline/239453 [200 OK, 10.73KB, 241ms]

So what is missing in the programmatic method is the actual GET request.

1 Like

Without seeing what’s in the Collection file I have no idea what’s happening :smiley:

Have you created the node project with a package.json file which includes newman and installed that as a library? Or have you just created a script and run it?

Running it from the command line is not the same as running it within a project and using newman as a library.

1 Like

Hi @danny-dainton,

I have a package.json with newman included and installed.
I am getting test results so it seems newman is executing as it’s supposed to.

Sorry, figured it wasn’t necessary to show the content of the collection. See below:

{
	"info": {
		"_postman_id": "76791e04-5685-4a21-a7dc-1d837354fb5a",
		"name": "Product enviroment",
		"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
	},
	"item": [
		{
			"name": "https://api.onlinedatabasetool.com/api/catalogline/{{id}}",
			"event": [
				{
					"listen": "test",
					"script": {
						"id": "69f9551a-6543-4c1b-bcc9-b5f676b73fc2",
						"exec": [
							""
						],
						"type": "text/javascript"
					}
				}
			],
			"request": {
				"method": "GET",
				"header": [],
				"url": {
					"raw": "https://api.onlinedatabasetool.com/api/catalogline/{{id}}",
					"protocol": "https",
					"host": [
						"api",
						"onlinedatabasetool",
						"com"
					],
					"path": [
						"api",
						"catalogline",
						"{{id}}"
					]
				}
			},
			"response": []
		}
	],
	"variable": [
		{
			"id": "47d58980-3c5d-42cf-96c9-ce3ba7b56290",
			"key": "id",
			"value": "0"
		}
	],
	"protocolProfileBehavior": {}
}
1 Like

It’s the thing that drives everything so quite important to know what’s inside it :smiley: - And this is the file that’s run on both the CLI and referenced in the script?

Is there Collection level variable in the file, is that just used for testing in the app and not part of the datafile?

Running that collection from this side (in the most basic sense), shows it making the single request.

When I create a mock CSV file it’s running the requests:

1 Like

Hello everybody,

I need your help please.
I would like to retrieve a value from the json. a variable.
how can i get it back please? I tried the command in the .js
args.item.request.auth.basic[1].key

const newman = require(β€˜newman’),
fs = require(β€˜fs’);
const request= Math.random().toString(36).substring(7) + β€˜-request.txt’;

newman.run({
collection: require(β€˜./G-test1.postman_collection.json’),
iterationData: β€˜./data.csv’,
environment : require(β€˜./.postman_environment.json’),
reporters: β€˜cli’,
iterationCount : 1

}).on(β€˜request’, function (error, args) {
if (error) {
console.error(error);
}
else {

    fs.appendFile(request,     args.item.request.auth.basic[1].key  , function (error) {

        if (error) { 
            console.error(error); 

        }
    });        
}	

});

my json est

},
		"request": {
			"auth": {
				"type": "basic",
				"basic": [
					{	"key": "password",
						"value": "{{MDP}}",
						"type": "string"
					},
					{	"key": "username",
						"value": "{{NO}}",
						"type": "string"
					}
				]
			},
			"method": "POST",
			"header": [
				{
					"key": "OP",
					"value": "{{NOM_O}}",
					"type": "text"
				}
			],
			"body": {
				"mode": "raw",
				"raw": "{    \r\n    \"....

Thank you