I have a GET request that saves parts of the response body (entries of “name”) to the collection variable (there could any number of names) to be used by the next request and this works correctly:
I then run the second GET request to a different endpoint with much the same instruction but it saves the response body (entries of “name”) to an environment variable (so that I have both the first and second responses saved):
In the second request I am trying to follow up with a test instruction that takes “Name-01” from the collection variable and checks if it is present in the environment variable, then takes Name-02 from the collection variable and checks if it is present in the environment variable and so on for however many entries were saved to the collection variable.
What I have been able to do is check any 1 entry with the instruction below:
pm.test(“New response has same value for Name”, function(){
pm.expect(pm.collectionVariables.get(“Name-3”)).to.eql(pm.environment.get(“Name-3”))
});
But my question is "Can I add some instruction to that pm.test that gets it to loop through each indexed name saved in my collection variable? Using ("Name-${index}) in place of say Name-3 does not work for me.
It has taken me 5 hours to get to this point so any happy help would be greatly appreciated!
Thank you @danny-dainton for the pieces that do work as I found them on here as your answers to other questions.
let jsonData = pm.response.json()
let collArr = [];
for (item of jsonData.data) {
collArr.push(item.name);
}
console.log("Collection Array", collArr);
pm.collectionVariables.set("collArr", collArr);
Response 2 Test Tab;
let jsonData = pm.response.json()
let envArr = [];
for (item of jsonData.data) {
envArr.push(item.name);
}
console.log("Environment Array", envArr);
pm.environment.set("envArr", envArr);
let x = pm.collectionVariables.get("collArr");
let y = pm.environment.get("envArr");
for (i=0; i < x.length; i++)
{
console.log(x.includes(y[i]));
}
Thank you very very much for taking the time to set this up for me.
I am only getting a chance to try this out today. It looks exactly like what I was attempting (badly) to achieve.
The only issue on my end is that I believe my response is in XML (snippet of it below)
I have tried to convert this to JSON by using var jsonObject = xml2Json(responseBody);
But that does not seem to work or else I am using it incorrectly.
Would you be able to suggest what I must do with my response to get it to cooperate with your script please? where each “ItemKey” is the entry I want to save to the variable