Team
I am facing a challenge while trying to use an arrayList variable. Tried setting this up in an env variable. It is not retaining the old values.
The arraylist is always holding the latest value. Looks like it is initializing the arraylist for every iteration. I want to retain and append yo the arrayList till the end of CSV iteration.
Here is my scenario. I need to print the key which is not working.
There are list of keys in the CSV file.
Steps:
-
Iterating through the CSV file (2000 rows) to read the Key values - PASS
-
Passing the key value from csv in the post body of the request {
“key”: {{keys}}
} - PASS -
Execute the request - PASS
-
If the request for that iteration key is not working, pushing that missing key into an array
missedContentList.push(pm.iterationData.get(‘keys’)); - PASS -
At the end of the iterations, print the arraylist - FAIL - It is printing only the last value instead of printing entire array
I have copied my code snippet.
var resultObject;
var missedContentList = [];
pm.test("1. Response status is 200", function () {
pm.response.to.have.status(200);
});
pm.test("2. Response should be okay to process", function () {
pm.response.to.not.be.error;
pm.response.to.not.have.jsonBody("error");
});
console.log("iteration count, current iteration = ", pm.info.iterationCount, pm.info.iteration);
try {
resultObject = JSON.parse(responseBody);
if (responseCode.code !== 200) {
console.log("Content not found for the keys from csv = " + pm.iterationData.get('keys'));
missedContentList.push(pm.iterationData.get('keys')); //This is always pushing the latest key
pm.environment.set("mList", (missedContentList)); //Tried setting it here as well.
console.log("Missing key = ", missedContentList);
}
if (pm.info.iterationCount - 1 === pm.info.iteration) {
console.log("length of the missing content in if = ", missedContentList.length); // This is always 1
pm.environment.set("mList", (missedContentList)); /set the value at the end of iteration
m1 = pm.environment.get("mList");
printMissingContent();
}
}
catch (ex) {
console.log("Unexpected response body: " + ex);
console.log(responseBody);
pm.test("FAIL - Empty / Invalid response", () => { throw new Error(ex.message) });
return;
}
function printMissingContent() {
m = pm.environment.get("mList");
console.log("Length of missedContentList inside the function= ", m.length);
console.log("List of all missing content ");
//This is always printing the last key not all the missing keys
console.log("=============================");
console.log(m);
return;
}
Thanks, Raje