let jsonData = pm.response.json()
pm.test('The `srNo` in each record' , () => {
_.each(jsonData.srNo, (item) => {
pm.expect(item.srNo)
})
})
//This is what I Tried not going well
//Thanks for the Reply
let jsonData = pm.response.json()
pm.test('The `srNo` in each record' , () => {
_.each(jsonData.srNo, (item) => {
pm.expect(item.srNo)
})
})
//This is what I Tried not going well
//Thanks for the Reply
There are a few ways that you could do this - Sticking to the direction that you were heading in, this could work:
let jsonData = pm.response.json();
pm.test('The `srNo` property is in each record' , () => {
_.each(jsonData, (item) => {
pm.expect(item.person, 'Does not have the property').to.have.property('srNo');
});
});
Hey,
Thanks for the suggestion, Itâs working fine now.
again Thank you
I am new to Postman. I want to check Id=3
, I have tried like below:
pm.test('The `Id` contains 3', () => {
_.each(jsonData.Fee, (item) => {
pm.expect(item.Id).to.include(1)
})
})
Actually, it is giving an assertion error. expected 2 to include 1
Hey @arunece09
Iâm not sure what it is youâre trying to assert against, you might need to share the response body too.
The code is looping through the data so it will check each object and if all the object Id
properties are not 1
then it would fail like you can see.
This is from another script, Hope you can make this together.
pm.test("The response has all properties.", () =>
{
pm.expect(jsonData).to.be.an("array");
pm.expect(jsonData[0].channel.id).to.be.a("string");
pm.expect(jsonData[0].postList).to.be.an("array");
});
Hey @danny-dainton
I want to ask you about this request
pm.test('The "Number_1" contains ', () => {
_.each(jsonData.Fee, (item) => {
pm.expect(item.Number_1).to.include('+33 ')
})
})
But it didnât work unfortunately when i searched throw 2 arrays like this i want to get except for the âNumber_1â and âNumber_2â
{
"Client_Data":{
"User":{
"Name":"Juan",
"Surname":"Pablo",
"Birthday":"12.07.1991",
"Contact":{
"Telefon":[
{
"Number_1":"+33 6084986932",
"Number_2":"+33 4596127896"
}
]
}
}
}
}
Thank you so much for your quick response. Here is my response body.
{
"data":[
{
"id":1,
"email":"george.bluth@reqres.in",
"first_name":"George",
"last_name":"Bluth",
"avatar":"https://reqres.in/img/faces/1-image.jpg"
},
{
"id":2,
"email":"janet.weaver@reqres.in",
"first_name":"Janet",
"last_name":"Weaver",
"avatar":"https://reqres.in/img/faces/2-image.jpg"
}
]
}
Below is my Tests, where I need to check email is george.bluth@reqres.in
present in the response body.
let JsonData= pm.response.json();
pm.test("Verify email", () => {
_.each(JsonData.data, (item) => {
pm.expect(item.email).to.include('george.bluth@reqres.in')
})
})
it is giving the Assertion error: expected janet.weaver@reqres.in
to include george.bluth@reqres.in
Hey @mw49
As youâre copying the code from a completely different response body, all the references youâre using are not correct.
You could use something like this to check the values of the items in that single object, with the array. Itâs using .match
to check the value.
This is a good site to get more understanding of how regex works - regex101: build, test, and debug regex
As youâre looking at all the values in that object, you would need to get them before you can loop through them, thatâs why iâm using _.values
to get them from the object. You can see what values are captured by uncommenting the console.log
statement.
let jsonData = pm.response.json();
pm.test('The numbers contain +33 ', () => {
let phoneNumbers = _.values(jsonData['Client_Data'].User.Contact.Telefon[0]);
// console.log(phoneNumbers);
_.each(phoneNumbers, (number) => {
pm.expect(number).to.match(/\+(33)\s\d+/);
});
});
As youâre using a loop, itâs looking at each of the email
properties and checking if it includes that value. As the second object doesnât include that, itâs failing.
This would target that specific value in the first object:
pm.test("Verify email", () => {
pm.expect(jsonData.data[0].email).to.eql('george.bluth@reqres.in')
})
You could use something like this to check them all and give a true/false
if itâs present:
pm.test("Verify email", () => {
pm.expect(jsonData.data.some(a => a.email === 'george.bluth@reqres.in'), 'Email not in array').to.be.true;
})
I can see that many people are asking follow up questions on this thread that donât relate to the original question.
Iâm going to close this topic and ask that you create a new topic that contains all the details for your particular question, in your own context.
It will save others on the thread receiving notifications for non-related questions.