Hello community,
I need some help. Below is attached my code, the problem is accessing to array’s objects properties and using them as variables.
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.
First of all You can run the pre-request script to get the array of objects, it’s something like this:
let requestsArray = [{"r_type":"01", "r_target":"1", "u_range":"1"},{"r_type":"02", "r_target":"11", "u_range":"54"},{...}, ...]
I have three properties in my array’s objects - "r_type", "r_target", "u_range"
.
My request body is x-www-form-urlencoded and looks like this:
<ReportType><![CDATA[{{r_type}}]]></ReportType>
<RequestTarget>{{r_target}}</RequestTarget>
<UsageRange>{{u_range}}</UsageRange>
How can I access to the objects within array and use its properties as variables ?
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);
}