Hi,
I am new to Postman. I recently started working on API testing as part of my work. I have a usecase where I need to validate whether JSON response has records filtered based on the substring passed as a query parameter in the GET request.
The API is {{url}}/api/control-branch-master?branchCode=sa&branchName=test
I have two input parameters “branchCode” and “branchName”. Based on the value passed in the request for these two parameters, those records containing these values as substring will be filtered and returned in JSON response .
I have tried validating the string by using “has()”. I do not know if there is any alternate way though I tried searching alot.
The above script runs without any errors or failed cases. But irrespective of the no. of query parameters passed in the request, the console prints both the console.log statements in if and else. The request can contain single or both query parameters. I have also made some changes and in the “has” part of the above script and wrote as follows
Even in the above script irrespective of making any changes to the has() part, the console.log statements of both if else cases get printed and not else if.
I am actually not sure whether the above script is the right way to validate the input substring with a string in response.
I Kindly request anyone to help me out with this as am completely a beginner in this . What is the right way to validate the input with response and is the above script right? What changes can I make in above script to make it right? Is there an alternative way to do this by using regular expression or include()?
Hi…Firstly thank you for replying. Thank u for sharing the suggestions. Will follow them hereafter. I have rewritten my script above and it worked out. Though I have seen your reply only today. But as u said will try not complicating them hereafter. I just want to share a screnshot of what I have written
The above script however worked. Am not sure if there is any other way to write it.
Thanks for replying. I tried contain first, it didn’t work for me.So used has…Am a complete beginner to postman. Will take care while using pm.expect hereafter…I thought can use pm.test to check whether test passed or failed and hence put IF-else conditions within pm.expect(). However I made changes to my script and it worked before I have seen ur reply. So I used pm.expect() like earlier. This is how I wrote
You can imagine how surprised I am that your code “works”. I recently did a talk around this subject. Maybe it is worth watching:
Don’t get me wrong but I think you are missing some basics around writing assertions in Postman. It would also not hurt to know some JavaScript basics.