In my collection, for all the requests I have 3 scenarios:
- when the response code is 200 and the response body is not empty.
- when the response code is 200 and the response body is empty.
- When response code is other than 200
For 2nd and 3rd, I have added functions at the collection level and that’s working fine.
For the 1st, I want to add a function at the collection level, basically, the function should check if the response code is not 200 then don’t executed the block of scripts written at the request level.
The purpose of adding this conditional check (responseCode.code === 200 && jsonData.data.length > 0) at the collection level because I have more than 500 requests in my collection and I don’t want to go and add this piece of code in all 500 requests.
This check is important in this case when the response body is empty and the response code is 200 then it’s throwing
"Cannot read property '0' of undefined" because the script is trying to validate some elements which are not present in the response body.
I tried so many things but it didn’t work out.
Please suggest how we can add a function at the collection level to check if the response code !=200 then skip the whole block of test scripts written at the request level