Assertion of Multiple collection variables

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:

let jsonData = pm.response.json().result
jsonData.forEach((item, index) => {
pm.collectionVariables.set(Name-${index}, item[“name”])
})

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):

let jsonData = pm.response.json().result
jsonData.forEach((item, index) => {
pm.environment.set(Name-${index}, item[“name”])
});

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.

Hi @barryd172

Is this what you mean?


Example Response 1;

{
    "data": [
        {
            "name": "test1"
        },
        {
            "name": "test2"
        },
        {
            "name": "test3"
        }
    ]
}

Example Response 2;

{
    "data": [
        {
            "name": "test4"
        },
        {
            "name": "test2"
        },
        {
            "name": "test6"
        }
    ]
}

Response 1 Test Tab;
image

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;
image

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]));
}


Output

image

Hello @w4dd325

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

image

I tried to have a look but I just haven’t had the time to sit and play with it.
And it’s quite new to me as I don’t do much with XML currently.

But here are 2 interesting posts that might help;

If you get it working please do share the solution!!
And if not I’ll try and find a bit of time to have a better look :+1: