Postman JSON value check test fails - api

I have an API which has response as below:
"data": {
"catalog_list_items": [
{
"catalog_list_items": [
{
"catalog_list_items": [
{
"title": "L13",
"content_id": "58a85146b0000ec",
"sequence_no": 1,
"language": "hindi",
"genres": [
"xxxx"
]
I would like to create a test in postman to verify that the language key has value as 'hindi'. I am writing below test in Postman which always fails. Cannot find the reason.
var jsonData = JSON.parse(responseBody);
tests["Language is hindi"] = jsonData.data.catalog_list_items[3].language === "hindi";
However, when I use the similar structure to test sequence_no, it works fine
var jsonData = JSON.parse(responseBody);
tests["Sequence No is 1"]= jsonData.data.catalog_list_items[3].sequence_no === 1;
Can anyone shed a light for me?

I could see 3 level nested arrays in your response body.
So I don't know how are you getting your value through this expression:
jsonData.data.catalog_list_items[3].language
The correct expression to get the 'language' key from above response should be:
jsonData.data.catalog_list_items[0].catalog_list_items[0].catalog_list_items[0].language

Related

Karate API : * def from response is throwing syntax error

I am setting up an E2E test and chaining my request/responses. I am defining variables from each response and using them in the next call.
Its working up to a point, and then a problem surfaces when defining off the 2nd response.
If I def operationId, operationSubject, or operationStatus (e.g response.operationId), it works.
If I store anything from the results (e.g response.results.0.personId) it throws this error
Expected ; but found .0
response.results.0.personId
My response:
{
"operationId": "922459ecxxxxx",
"operationSubject": "BATCH_ENROLLMENT",
"operationStatus": "PROCESSED",
"results": {
"0": {
"personId": "367a73b5xxxx",
"status": "PRE_AUTH",
"email": "mquinter+TEST.69387488#email.com",
"loanNumber": null
},
"1": {
"personId": "56f060fd-e34xxxxxx",
"status": "PRE_AUTH",
"email": "mquintxxxx#email.com",
"loanNumber": null
}
}
}
That's not how to access data in JSON. See this similar question: https://stackoverflow.com/a/71847841/143475
Maybe you meant to do this:
* def foo = response.results[0].personId
https://stackoverflow.com/users/143475/peter-thomas
I see the issue - It wasn't finding the response because I wasn't giving it enough time before the next call.
I put a sleep in there and its working as expected.
Thanks

POSTMAN - Test failure false to be truthy

As a beginner I have few questions. I am using the Get request, which would populate json below.
https://reqres.in/api/users
{
"total": 12,
"total_pages": 4,
"data": [{
"id": 1,
"first_name": "George",
"last_name": "Bluth",
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/calebogden/128.jpg"
}]
}
for the 2 tests below while the 1st one passes the 2nd test fails with the message:
AssertionError: expected false to be truthy
//Verify Page number total is 12
var jsonData = JSON.parse(responseBody);
tests["Checking total page number-Manual"] = jsonData.total === 12;
//verify is exists and is 1
var jsonData = JSON.parse(responseBody);
tests["Checking ID exists and is 1"] = jsonData.id === 1;
Question 1:
A github post that I found says there may be an error and suggests to use
the new pm.* equivalent instead. However I do not see any difference between the 1st and the 2nd. So why does the 2nd test fail?
Question 2:
Is it possible to write test to verify that for ID:1 the firstname is George?
Thanks in advance for your time.
The reason that your 2nd test fails is because data is an array and in this case you must access the first element. You would want to do something like this (new syntax):
pm.test("Verify id is equal to 1", function() {
var jsonData = pm.response.json();
pm.expect(jsonData.data[0].id).to.equal(1);
});
Similarly for testing first name is George:
pm.test("Verify id is equal to 1", function() {
var jsonData = pm.response.json();
pm.expect(jsonData.data[0].first_name).to.equal("George");
});
If you always expect it to only be a single element in the array then you're safe to use index 0 i.e. data[0]. However if you expect there to be more elements in the data array then you would have to iterate through them to look for the correct element.
Here's a good reference for the API:
https://learning.getpostman.com/docs/postman/scripts/postman_sandbox_api_reference/

Karate - actual value is not a string

I am checking for an 'OK' parameter in response but getting above mentioned error. The same code was working with my previous karate version, now I am using 0.9.0 - This might be some issue with my coding, can't figure out.
Response:
"testResponse": {
"planSummary": {
"includedServicesList": [
{
"some elements goes here": "test"
}
],
"status": {
"statusCd": "200",
"statusTxt": "OK"
}
}
}
My Feature File Code:
When method get
Then status 200
Then match response contains 'OK'
I get expected: 'OK', reason: actual value is not a string error.
I tried with Then match response contains {statusTxt: 'OK'} as well.
You can try this,
* match response.testResponse.planSummary.status contains {'statusTxt' : 'OK'}
OR
* match response.testResponse.planSummary.status.statusTxt == 'OK'

Matching values from response to values from DB

My response looks as follows :
{
"data":[
{
"foo":bar1
"user_email":"user#user.com",
"user_id":1
},
{
"foo":bar2
"user_email":"user#user.com",
"user_id":1
}
]
}
* def DBOutput = #fetching values from DB
* match response.data[*].foo contains [DBOutput1[0][0],DBOutput1[1][0]]
DBOutput1 has data as follows : [["bar1"],["bar2"]]
This match fails, for some reasons value passed into the expected list in match statement is DBOutput1[0][0]
This is the error I am getting actual: ["bar1","bar2"], expected: 'DBOutput1[0][0]',
You have some seriously malformed JSON in your example above. The below snippet works, just paste it into a new Scenario:
* def response =
"""
{
"data":[
{
"foo": "bar1",
"user_email":"user#user.com",
"user_id":1
},
{
"foo": "bar2",
"user_email":"user#user.com",
"user_id":1
}
]
}
"""
* match response.data[*].foo contains ['bar1', 'bar2']
Now it is up to you to fix your test, without knowing what your DBOutput is no one can help further.
I iterated over the DBoutput and stored them in a new array list. I then matched the response to the list and it worked.
match response.data[*].foo contains ListFromDB

Keen-io: i can't delete special event using extraction query filter

using extraction query (which used url decoded for reading):
https://api.keen.io/3.0/projects/xxx/queries/extraction?api_key=xxxx&event_collection=dispatched-orders&filters=[{"property_name":"features.tradeId","operator":"eq","property_value":8581}]&timezone=28800
return
{
result: [
{
mobile: "13185716746",
keen : {
timestamp: "2015-02-10T07:10:07.816Z",
created_at: "2015-02-10T07:10:08.725Z",
id: "54d9aed03bc6964a7d311f9e"
},
data : {
itemId: 2130,
num: 1
},
features: {
communityId: 2000,
dispatcherId: 39,
tradeId: 8581
}
}
]
}
but if i use the same filters in my delete query url (which used url decoded for reading):
https://api.keen.io/3.0/projects/xxxxx/events/dispatched-orders?api_key=xxxxxx&filters=[{"property_name":"features.tradeId","operator":"eq","property_value":8581}]&timezone=28800
return
{
properties: {
data.num: "num",
keen.created_at: "datetime",
mobile: "string",
keen.id: "string",
features.communityId: "num",
features.dispatcherId: "num",
keen.timestamp: "datetime",
features.tradeId: "num",
data.itemId: "num"
}
}
plz help me ...
It looks like you are issuing a GET request for the delete comment. If you perform a GET on a collection you get back the schema that Keen has inferred for that collection.
You'll want to issue the above as a DELETE request. Here's the cURL command to do that:
curl -X DELETE "https://api.keen.io/3.0/projects/xxxxx/events/dispatched-orders?api_key=xxxxxx&filters=[{"property_name":"features.tradeId","operator":"eq","property_value":8581}]&timezone=28800"
Note that you'll probably need to URL encode that JSON as you mentioned in your above post!