How to use a single array object as a data input to collection file with multiple API triggering

Hello All,
I’m kind of a beginner with postman / newman. As part of my project requirement like to do some testing mechanism to enable with newman .
I have to test few graphql API’s with help of data input file .
The requirements are as follows .

  1. want to create a single collection file with multiple API’s ,call configured
    collection file format :
    { item1 : api 01 (accepts value from json tc1.template.name and tc1.template.description)
    item2: api02 (accepts value from json tc1.nodes.name and tc1.nodes.equipement.name )
    item3: api02 (accepts value from json tc1.links.name and tc1.links.direction)
    }
  2. like to have a single data file with multiple dataset which will feed for the collection file above
    sample format : ¨
    [
    {“tc1”:
    {“template”: {“name”: “TC138 Config 1”,“desc”: “TC138 Config 1 from newman”},
    “nodes”: [
    {“name”: “NR_RU1”,“equipmentName”: “Radio 4455 B2/B25 B66A”},
    {“name”: “NR_RU2”,“equipmentName”: “Radio 4449 B5 B13”},
    {“name”: “Viavi E500.SVR.FES”,“equipmentName”: “VIAVI E500 MK4.3 SVR.FES”}
    ],
    “links”: [
    {“name”:“Rf6”,“direction”:“UL”,“type”:“CPRI”,“sourceNodeName”:“NR_RU2”,“sourcePortName”:“RFA”,“targetNodeName”:“Viavi E500.SVR.FES”,“targetPortName”:“PRC1.TX2”},
    {“name”:“Rf7”,“direction”:“DL”,“type”:“CPRI”,“sourceNodeName”:“NR_RU2”,“sourcePortName”:“RFB”,“targetNodeName”:“Viavi E500.SVR.FES”,“targetPortName”:“PRC1.TR3”},
    {“name”:“Rf8”,“direction”:“UL”,“type”:“CPRI”,“sourceNodeName”:“NR_RU2”,“sourcePortName”:“RFB”,“targetNodeName”:“Viavi E500.SVR.FES”,“targetPortName”:“PRC1.TX3”}
    ]
    }
    }
    ]

Can anyone help me how to achieve this … ?

Please use the preformatted text option when posting code, JSON structures or example responses.

It stops everything being aligned to the left.

Have you looked at the Collection Runner?

Using CSV and JSON Data Files in the Postman Collection Runner | Postman Blog

@michaelderekjones Hi mike ,
I tried the simple array as for eg: [{name:value1,desc: value1},{name:value2,desc: value2}] this is working if we configure as one collection file VS one data file .
But as part of my project requirement , the input data will be varied in the future (new data will be added). So want to make the API calling will be permanent and new data input will be variant .
In this case the main requirement is when we add new data input , other team member has to follow the same format and needs to be in a single file.

I hope I explained it better to keep atleast this format . Basically configuring data input for different API in same input file

OR you can suggest any other better way to achieve this

btw, I’m using newman in all sense . As it requires the CLI operation in priority

Sorry, I’m not quite following.

You have a collection with 3 requests in.

You have a JSON file with test data.

The JSON file has multiple records. You may add extra records in the future, or potentially change the existing records.

You iterate over the JSON test file using the collection runner and it will run once for each record.

Newman runs the same way. So get it working in the Collection Runner first.

If you add more records, then I understand you will need to increase the iteration count in the Newman command line, and you should be good to go.

For CSV files, it will run once per line in the CSV, but for JSON data files I understood that you need to tell it the number of iterations (although I haven’t tried JSON files recently, as I always default to the CSV method).

As far as I can tell, using the Collection Runner\Newman with a data file should meet your requirement.

The whole point of using the data files, is that you can add, remove, or edit the data being sent at any time.

Therefore, I’m not really sure what the issue is, or what the question is. :flushed:

@michaelderekjones
Hi Mike ,

The command i’m using is newman run -d

I’m attaching the collection files and data files using :

the mapping is :

collection file : laas_nm_coll_createTemplate uses the datafile : templateList.json
collection file : laas_nm_coll_createNode uses the datafile : templateNodeList.json

The entire collection file and data file is pasted below, as i’m not able to upload .

My requirement is to combine all these 2 API into a single collection file and combine all data file into one data file . Due to a number of reasons my team doesnt want to use these separate collection files or data files.
Another benefit with doing like this is i’m generating a summary report and a junit output xml, using a nodejs . If i run these collection file sperately i’m getting number of summary files and junit files, which is not good .

To combine all data file, i made format which is with filename : TesctCase01 , but i tried to define the variable in createTemplate API collection file as tc1.template.name and different json format string commands , but nothing is working.
Ideally, i want to know how can force and API call using newman, with only required data from a list of data.

Hope its explains now.

BR
Deepu

===================== collection file:laas_nm_coll_createTemplate =======================
{
“info”: {
“name”: “createTemplate”,
“description”: “A sample collection to demonstrate collections as a set of related requests”
},

"item": [{
  "name": "createTemplate",
  "event": [{
	"listen": "test",
	"script": {
	  "type": "text/javascript",
	  "exec": [
		"pm.test('expect API response be 200', function () {",
		"    var jsonData = pm.response.json();",
		"    pm.response.to.be.ok && pm.response.to.have.status(200);",
		"})",
		"pm.test('Response contains no error object', function () {",
		"    const curren_response = pm.response.json();",
		"    pm.expect(curren_response.errors == undefined, \"Response containse graphql errors\").to.be.true",
		"});"
	  ]
	}
  }],
  "request": {
	"url": "http://orchestrator-gui:8011/graphql",
	"method": "POST",
	"body": {
		"mode": "graphql",
		"graphql": {
			"query":"mutation {templateCreate (input: {name: \"{{createTemplName}}\"description: \"{{templDesc}}\"}){id}}"
		}
	}
  }
}]

}
=========================== collection file:laas_nm_coll_createNode ======================================
{
“info”: {
“name”: “Sample Postman Collection”,
“description”: “A sample collection to demonstrate collections as a set of related requests”
},

"item": [{
  "name": "createTemplateNode",
  "event": [{
	"listen": "test",
	"script": {
	  "type": "text/javascript",
	  "exec": [
		"pm.test('expect API response be 200', function () {",
		"    var jsonData = pm.response.json();",
		"     console.log(jsonData);",
		"    pm.response.to.be.ok && pm.response.to.have.status(200);",
		"})",
		"pm.test('Response contains no error object', function () {",
		"    const curren_response = pm.response.json();",
		"    pm.expect(curren_response.errors == undefined, \"Response containse graphql errors\").to.be.true",
		"});"
	  ]
	}
  }],
  "request": {
	"url": "http://orchestrator-gui:8011/graphql",
	"method": "POST",
	"body": {
		"mode": "graphql",
		"graphql": {
			"query": "mutation {nodesCreate (input: {templateName: \"{{templateName}}\"nodes: [{name: \"{{nodeName}}\" eqmTypeName: \"{{EquipmentName}}\"}]}) {id}}"
		}
	}
  }
}]

}

=========================== data file:laas_nm_coll_createNode ======================================
[
{“createTemplName”: “TC138 Config 1”,“templDesc”: “TC138 Config 1 from newman”},
{“createTemplName”: “TC138 Config 2”,“templDesc”: “TC138 Config 2 from newman”},
{“createTemplName”: “TC138 Config 3”,“templDesc”: “TC138 Config 3 from newman”}
]
=========================== data file:templateNodeList ======================================
[
{“templateName”: “TC138 Config 1”,“nodeName”: “vDU”,“EquipmentName”: “HPE EDGELINE E920T”},
{“templateName”: “TC138 Config 1”,“nodeName”: “gNB&eNB”,“EquipmentName”: “BASEBAND 6648”},
{“templateName”: “TC138 Config 1”,“nodeName”: “RGW”,“EquipmentName”: “RADIO GATEWAY R608”},
{“templateName”: “TC138 Config 1”,“nodeName”: “LTE_RU1”,“EquipmentName”: “RADIO 2212 B66A”},
{“templateName”: “TC138 Config 1”,“nodeName”: “NR_RU1”,“EquipmentName”: “Radio 4455 B2/B25 B66A”},
{“templateName”: “TC138 Config 1”,“nodeName”: “NR_RU2”,“EquipmentName”: “Radio 4449 B5 B13”},
{“templateName”: “TC138 Config 1”,“nodeName”: “Viavi E500.SVR.FES”,“EquipmentName”: “VIAVI E500 MK4.3 SVR.FES”}
]
============================= combine data file : TesctCase01 =================================
[
{“tc1”:
{“template”: {“name”: “TC138 Config 1”,“desc”: “TC138 Config 1 from newman”},
“nodes”: [
{“name”: “vDU”,“equipmentName”: “HPE EDGELINE E920T”},
{“name”: “gNB&eNB”,“equipmentName”: “BASEBAND 6648”},
{“name”: “RGW”,“equipmentName”: “RADIO GATEWAY R608”},
{“name”: “LTE_RU1”,“equipmentName”: “RADIO 2212 B66A”},
{“name”: “NR_RU1”,“equipmentName”: “Radio 4455 B2/B25 B66A”},
{“name”: “NR_RU2”,“equipmentName”: “Radio 4449 B5 B13”},
{“name”: “Viavi E500.SVR.FES”,“equipmentName”: “VIAVI E500 MK4.3 SVR.FES”}
]
}
}
]

I can only re-iterate that Newman mirrors the functionality of the Collection Runner to a certain extent, so try and get it running in the Collection Runner first.

Newman has a lot of command line options, including adding a csv file, or environment file, or just sending what should be contained in variable through the pipeline.

When posting code, JSON, examples responses, please use the preformatted text option in this editor, as its stops all of the code being aligned to the left, which is practically impossible to read without reformatting it.

Think about the Collection as being a Test Suite, the folders being a Test Case, and the requests being the Test Steps.

Therefore you can have the two different API requests in separate folders.

There are options for how to get to the test data, but it depends on how you want to use the data.

It looks like you have three elements in the first set of test data, so are you expecting these request to loop, and the second test of test data has 7, so same question, are you wanting to loop through that? You can’t achieve that with the collection runner alone. You will need code to control the loop.

The way I would deal with the test data is to setup the JSON file, and then minify it, and then send it in the Newman command line so it gets stored in a Postman environment variable. This means you need to include the environment in the Newman command line as well as the collection JSON as well as the option for the variable.

Why a Environment Variable instead of collection? With Environment variables, you can set them as “secret” which means the value does not get included when you export the file or get copied to the Postman cloud. If the test data is not confidential, then by all means use the Collection Variable. If the test data is not confidential, then you could just include the test data in the variable and save it to your code repository. Not something I recommend unless the test data is static and never changes.

So your test data is now available to your Collection. Now what?

If you are just running each API once, you just need a pre-request script to parse the environment variable and set new variables for each element you are going to parameterise in your test.

When you fetch the variable which will need to be parsed as it will be stored as a string, you would just parse the array that contains the test data for each specific test. Therefore you should be able to have all of the test data in one file, as long as the first element is the test case name. The test data needs to be an array.

If you are going to loop through the test data, then you will still need to parse the data in the pre-request script, but you are going to need to use a concept called array.shift() which takes the first record from the test data array and save it as a variable, and then removes it from the array. You then have to keep saving the new array and current variable so they can be used in the current request. In the Tests tab, you retrieve the updated request and keep looping using setNextRequest while the array length is less than zero.

Here’s a couple of examples of using array.shift() in different ways.

Loop through values from GET to next PUT request - Help - Postman

Postman delete multiple resource IDs - Help - Postman

Post Looping through an Array not working - Help - Postman

Remember to stringify arrays when storing them as variables, and JSON.parse when retrieving.

Regarding the JSON structure, consider the following base line.

{
    "TestData": {
        "testCase1": [
            {
                "username": "mike",
                "password": "123456"
            },
            {
                "username": "dave",
                "password": "654321"
            }
        ],
        "testCase2": [
            {
                "fileSystem": "fs1"
            },
            {
                "fileSystem": "fs2"
            }
        ]
    }
}

This creates an object with the first element being another object called TestData.

There is only one object at this top level and is basically there to tell you what the JSON data is all about.

The next level, has array objects for the testCases, with each record as its own object. They can have completely different structures and data.

Then in the appropriate pre-request script, you would then parse the environment variable and then create a new variable of the array data. (Which will then allow you to start your loop).

const jsonData = JSON.parse(pm.environment.get("variableName"));
const testData = pm.response.json().TestData.TestCase1 // this will be an array