Your question may already have an answer on the community forum. Please search for related topics, and then read through the guidelines before creating a new topic.
[EDITED] example
Pieces involved: iteratinoData from data.json, Postman app request body, Postman app pre-request script, newman js script
–
Is it possible to use an environment variable to the iteration data in a json file?
For example:
Scenario 1: create a storage object to be used in following scenario -> will return respective objects with an id to be used in next iteration/scenario
Scenario 2: create a template with a storageid -> use group from env variable in the pre-request script of “create template” request. the request body would be a variable {{newTemplate}}
from iteration data
run collection that runs scenario 1, then scenario 2
EXAMPLE:
Postman Request Body:
{{newTemplate}}
Postman Pre-Request Script:
// check if any new templates are left to create
let newTemplates = pm.variables.get('newTemplates');
// start of scenarios, so get new templates to create
if (!newTemplates) {
newTemplates = pm.iterationData.get("templates_data");
}
// set templates storage pool id in environment variables, using variable previously set
let templateStoragePool = JSON.parse(pm.variables.get('storagePool'));
pm.environment.set('templateStorageId', templateStoragePool.id);
// get first template from data.json templates_data object
let currentTemplate = newTemplates.shift();
// expect this to be used in Request Body with `{{templateStoragePool}}`
pm.environment.set("newTemplate", JSON.stringify(currentTemplate));
pm.environment.set("newTemplates", JSON.stringify(newTemplates));
// handle repeating request with next newTemplate if it exists
...
}
iterationData : data.json
[
{
"scenario": "create a template and then a user",
"routes": [
"Create a template",
"Create a user"
],
"templates_data": [
{
"name": "template A",
"storage": [
{
"storageId": "{{templateStorageId}}",
}
]
},
{
"name": "template B"
"storage": [
{
"storageId": "{{templateStorageId}}",
}
]
}
]
}
]
currently my newTemplate object that goes into the request looks like this when I do console.log:
{
"name": "template A",
"storage": [
{
"storageId": "{{templateStorageId}}",
}
]
}
but what I want would be:
{
"name": "template A",
"storage": [
{
"storageId": "190340-293849-13894",
}
]
}
newman script
function testCollection() {
newman.run({
collection: <myCollection>),
iterationData: data.json
}, function (err) {
if (err) { throw err; }
console.log('collection run complete!');
});
}
*this is a very oversimplified example i came up with, in reality my scenarios/routes are much more complex so being able to pass variables this way would be really useful
TIA!