I know i can add same Json request to capture value of itemlist again and save it diff variables.
Is there another way to run same request in multiple iteration, instead overwriting same Env variable it should save itemlist (since its different each time) with different variables like for var2, var3 so on…
Run same request in iteration and save itemid[i] into Variable_1,Variable_2 etc for Itemid1, itemid2…so on respectively
for e.g like For 5 iteration in can save 5 env variable with same run via newman on postmanrunner?
Token Generated
ItemId -1(captured itemid and saved it in Variable_1)
Saved it in variable_1
If I understand you correctly, you want to run the same request multiple times, and grab a unique value and save it to a new variable each time the request is run?
You could do something like this:
let iterationCount = pm.collectionVariables.get('iterationCount');
if(iterationCount) {
iterationCount = Number(iterationCount);
}
else {
iterationCount = 1;
}
const jsonData = pm.response.json();
const value = jsonData.value; // this would be whatever your property you want
let values = pm.collectionVariables.get('values');
if(values) {
values = JSON.parse(values);
}
else {
values = [];
}
values.push(value);
pm.collectionVariables.set('values', JSON.stringify(values));
iterationCount++;
if(iterationCount <= 5) { // This would be your threshold for how many times to loop
pm.collectionVariables.set('iterationCount', iterationCount);
postman.setNextRequest('Name of this request');
}
else {
pm.collectionVariables.unset('iterationCount');
}
Yes @allenheltondev, i am looking the same thing. Let me run and comeback to you.
i have updated the value and request name. There is too much of “value” text used getting confused
@allenheltondev seems like it did run 5 times but getting error on declaration part. Also variables were not added in env. i used this code in test script.
After bit cleaning, i am not getting ReferenceError: Cannot access ‘jsonData’ before initialization
I had the variables set to collection variables, not environment variables. You’ll have to change that in the javascript if you want it to be set to an environment variable.
@allenheltondev. Thank but the requirement is iteration = 5 and values are saved in variable like: i used these item id separately must not used same itemid again
No problem, you can just create the variables dynamically:
let iterationCount = pm.environment.get('iterationCount');
if(iterationCount) {
iterationCount = Number(iterationCount);
}
else {
iterationCount = 1;
}
const jsonData = pm.response.json();
pm.environment.set(`item_${iterationCount+1}`, jsonData.itemid);
iterationCount++;
if(iterationCount <= 5) { // This would be your threshold for how many times to loop
pm.environment.set('iterationCount', iterationCount);
postman.setNextRequest('GenerateItem');
}
else {
pm.environment.unset('iterationCount');
}
Are you sure that value exists in the API response?
I’d recommend adding a test in there.
let iterationCount = pm.environment.get('iterationCount');
if(iterationCount) {
iterationCount = Number(iterationCount);
}
else {
iterationCount = 1;
}
const jsonData = pm.response.json();
pm.test('Response has itemId', function(){
pm.expect(jsonData).to.have.property('itemid');
});
pm.environment.set(`item_${iterationCount+1}`, jsonData.itemid);
iterationCount++;
if(iterationCount <= 5) { // This would be your threshold for how many times to loop
pm.environment.set('iterationCount', iterationCount);
postman.setNextRequest('GenerateItem');
}
else {
pm.environment.unset('iterationCount');
}
I want to make sure you’re testing this by running it through the collection runner.
I just copied this exact script into my workspace and ran it through the runner without issues. it iterated 5 times and created 5 environment variables.
Opps, Sorry i ran it seperately. but when i ran it with collection it only make 3 env variables. There was some problem in my collection. let me move this and ran again…hopefully it will work.