How to validate whether query parameter value in the GET req is a substring of the string present in the JSON response?

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 following is the JSON response I have

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 written the following script

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 there,

thanks for sharing all these details.

First of all, you are overcomplicating your tests, making them hard to read.

One more thing: I don’t think pm.expect does not return a boolean you can use in your if blocks.

When you are dealing with a problem you don’t know to solve, try breaking it down in smaller pieces.

For example:

  1. Get the data you want to assert logged with console.log.
  2. Write an assertion using hardcoded data.
  3. Use Postman variables to make the assertion dynamic.

I hope this helps.

1 Like

pm.expect throws an exception , it doesn’t return boolean so using it inside if statement doesn’t do anything.

also to assert substring use

       pm.expect("dasd").to.contain("hi")

postman uses chai:

https://www.chaijs.com/api/bdd/

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

Should I write If-case normally without pm.expect() and just have the assertion in it?

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.

OK.Thanks for sharing