I am new to Postman automation and facing some problem with running the test with different data set.
I am getting an error, “There was an error in evaluating the Pre-request Script: TypeError: Cannot read property ‘shift’ of undefined”
Pre-request script:
var Ids= pm.globals.get("buyerProgramId")
if(!(Ids)){
var buyerProgramId= [1961, 1962, 1972, 1976, 1977];
}
var currentId= buyerProgramId.shift();
pm.globals.set("buyerProgramId", buyerProgramId);
pm.globals.set("currentId",currentId)
TEST:
var Ids= pm.globals.get("buyerProgramId");
if(Ids && Ids.length>0){
postman.setNextRequest('Get Buyer Program Information by ID');
}else{
postman.setNextRequest(null);
}
Appreciate your help if someone can tell me what’s wrong here?
buyerProgramId is a defined array ? if so why is it inside if loop? Can you please explain a little bit more here, what’s your expectation? And what are you trying to Test? Also kindly place your scripts under ``` so that it will be easy to copy paste and work for…
like this
var buyerProgramId= ["1961", "1962", "1972", "1976", "1977"];
var Ids= pm.globals.get("buyerProgramId")
if(!(Ids)){
var currentId= buyerProgramId.shift();
pm.globals.set("buyerProgramId", buyerProgramId);
pm.globals.set("currentId",currentId)
}
Your if statement is evaluating to false, so buyerProgramId is never set.
I’d recommend changing what you have so a slightly different version than what @bpricilla said.
This defaults your list of Ids to the hardcoded list, but if there is already a list defined in the globals, uses that instead.
In your test script, just modify it slightly:
var buyerProgramIds = JSON.parse(pm.globals.get('buyerProgramIds'));
if(buyerProgramIds && buyerProgramIds.length){
postman.setNextRequest('Get Buyer Program Information by ID');
} else{
postman.setNextRequest(null);
}
Thanks @bpricilla for pointing that out. Actually, my task is to run the collection with a different dataset in each run(which is buyerProgramId in my case) until the size of the array.
So, I want to run the Postman test with 1st Iteration: 1961, 2nd Iteration: 1962, and so on.
I have now removed the array from the if condition. Now, I am no longer getting the above error but now, the request seems to be moving in an indefinite loop.
It is always running the request with the buyerProgramId 1961 instead of picking other IDs in the array.
Pre-request script:
var Ids= pm.globals.get(‘buyerProgramId’)
var buyerProgramId= [‘1961’, ‘1962’, ‘1972’, ‘1976’, ‘1977’];
Thanks @allenheltondev for your feedback. I tried with what you have suggested but still I am not able to iterarte through each and every ID in the buyerProgramId Array.
With your version, It’s picking up any 1 random buyerProgramId in the array and running the iteration only once.
I want to check the results with each and every buyerProgramId. Can you please help here?
Pre-request script:
let buyerProgramId= [‘1961’, ‘1962’, ‘1972’, ‘1976’, ‘1977’];
@vaibhav.sh.05 I think you are resetting the array back to its original state every time, hence the reason why the request only runs for the first element and since the array never gets exhausted, you are stuck in an infinite loop.
You are calling Get Buyer Program Information by ID to perform the iteration, and if you are setting the buyerProgramId array either in this request or any request in between, the array will always be reset to its original state.
You need to structure your Collection in a way that the first request initializes your variables, and then the next requests are used for looping
I don’t think it’s resetting the array every time. It should be defaulting the array to the initial values, but then overwriting it with the value in the global variable.
I think where @vaibhav.sh.05 might be going wrong is the parameter he’s passing into postman.setNextRequest.
That value has to match the name of the request you want to run. If you want to make sure you loop back on the same request you are currently on, you could just do postman.setNextRequest(request.name)
I tried with your suggestion but still, the request is not picking up all the IDs I have given in the Array.
It is running the request in collection runner for only the 1st ID i.e. ‘1961’.
Pre-request script:
var buyerProgramId= [‘1961’, ‘1962’, ‘1972’, ‘1976’, ‘1977’];
Thanks @allenheltondev. The array manipulation is now working in the way I wanted it to work.
This is a perfect solution! Thanks for all your efforts and help.
I am not sure if I understand your question correctly. However, i am using setNextRequest to the same request as I wanted to loop in though all my buyer program IDs
Using this syntax: postman.setNextRequest(request.name)
And if you are talking about the data in body of postman request then I am not using any data in the body. I was getting the data by passing the path variable, id : {{currentId}}
Hi @allan.helton
I need help. Below you can see my code, the problem is very close to your answer. The difference is in array.
I have generated an array of objects. It is big array and I should run the collection with a different dataset in each run until the size of the array.
I have three properties in my array’s objects - "r_type", "r_target", "u_range".
Pre-request script:
let global1 = ['01', '02', '03', '05'];
let global2 = [1, 3, 4, 11];
let global3 = {};
global3['1'] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
global3['3'] = [31];
global3['4'] = [34];
global3['11'] = [54];
let requestsArray = [];
for (let u = 0; u < global1.length; u++)
{
for (let j = 0; j < global2.length; j++)
{
for (let x = 0; x < global3[global2[j]].length; x++)
{
const obj = {};
obj.r_type = global1[u];
obj.r_target = global2[j];
obj.u_range = global3[global2[j]][x];
requestsArray.push(obj);
}
}
}
console.log(requestsArray);
let f = pm.globals.get("f");
if (!f || f.length == 0) {
f = requestsArray;
}
let current = f.shift();
pm.globals.set("trio", current);
pm.globals.set("f", f);
Test script:
const f = pm.globals.get("f");
if (f && f.length > 0){
postman.setNextRequest(pm.info.requestName);
} else {
postman.setNextRequest(null);
}
How can I access to objects into array and use its properties as variables ?
I will be honest, this code is extremely hard to follow. I can’t figure out what its doing vs what you want it to do. Is there any way you could clarify, please?
Hi @allenheltondev , thanks for your feedback.
First of all I have generated an array of objects. Then I need to iterate through each object into this array and use object’s properties as variable.