Hi!
Hopefully someone out there can help me out with this.
Given the following response:
{
"page": 0,
"results": [
{
"name": "Rover",
"breed": "German Shepard",
"weight": "38.2 lbs",
}
},
{
"name": "Barney",
"breed": "Husky",
"weight": "47.1 lbs",
}
},
{
"name": "Marnie",
"breed": "Labrador Retriever",
"weight": "22.8 lbs",
}
}
]
}
There’s two things I am trying to accomplish:
- I am trying to loop through each item in the results array, and if the name is either “Rover” or “Barney” or “Marnie” then assert the name
- I also want to ensure that if the name “Rover” or “Barney” or “Marnie” is not found for any of elements of the array, to fail the test
Here’s what I have so far:
let jsonData = pm.response.json()
pm.test('Assert that names of the dogs include Barney, Rover and Marnie', () => {
dog_name1 = "Rover"
dog_name2 = "Barney"
dog_name3 = "Marnie"
for (var i = 0; i < jsonData.results.length; i++) {
if(jsonData.results[i].name == dog_name1) {
pm.expect(jsonData.results[i].name).to.include(dog_name1);
}
else if(jsonData.results[i].name == dog_name2) {
pm.expect(jsonData.results[i].name).to.include(dog_name2);
}
else if(jsonData.results[i].name == dog_name3) {
pm.expect(jsonData.results[i].name).to.include(dog_name3);
}
Hey @dogstar74
You could use something basic like this for that:
let jsonData = pm.response.json();
pm.test("Dogs name is correct", () => {
_.each(jsonData.results, (item) => {
pm.expect(item.name).to.be.oneOf(['Rover','Barney','Marnie']);
});
});
If a name is different than the one’s mentioned in the array, then the test will fail.
Thanks for the response @danny-dainton . I should clarify the second goal that I’m trying to accomplish: “I also want to ensure that if the name “Rover” or “Barney” or “Marnie” is not found for any of elements of the array, to fail the test”:
I do not want to fail the test if there are additional elements in the array with a name other than “Rover”, “Barney” or “Marnie”. But I do definitely want to fail the test if there are no elements in the array with the name of “Rover”, “Barney”, and “Marnie”.
In other words, Rover, Barney, and Marnie must exist in the array, but Kiko and Rintintin can also exist in the array.
I hope this make sense, and is clear. Thanks!
Just following up on my own question. I came up with a solution, that while definitely hacky, seems to work as well. I’m sure there’s far more elegant ways to achieve the same results, but I’m happy to have figured out a solution nonetheless:
Here’s the updated code:
let jsonData = pm.response.json()
pm.test('Assert the results for the expected job titles', () => {
dog_name1 = "Rover";
a = 0;
dog_name2 = "Barney";
b = 0;
dog_name3 = "Marnie";
c = 0;
for (var i = 0; i < jsonData.results.length; i++) {
if(jsonData.results[i].name == dog_name1) {
pm.expect(jsonData.results[i].name).to.equal(dog_name1);
a = 1;
console.log("this is the contents of `jsonData.results[i].name`:", jsonData.results[i].name);
console.log("this is the contents of `a`:", a);
}
else if(jsonData.results[i].name == dog_name2) {
pm.expect(jsonData.results[i].name).to.equal(dog_name2);
b = 1;
console.log("this is the contents of `jsonData.results[i].name`:", jsonData.results[i].name);
console.log("this is the contents of `b`:", b);
}
else if(jsonData.results[i].name == dog_name3) {
pm.expect(jsonData.results[i].name).to.equal(dog_name3);
c = 1;
console.log("this is the contents of `jsonData.results[i].name`:", jsonData.results[i].name);
console.log("this is the contents of `c`:", c);
}
}
if(a == 0) {
pm.expect.fail("Dog `Rover` was not found")
}
if(b == 0) {
pm.expect.fail("Dog `Barney` was not found")
}
if(c == 0) {
pm.expect.fail("Dog `Marnie` was not found")
}
})