Day 11: Monitoring

Your question may already have an answer on the community forum. Please search for related topics, and then read through the guidelines before creating a new topic.

Here’s an outline with best practices for making your inquiry.

My question:
I am trying to write a test to compare the response body and make decision based on it.

Details (like screenshots):

This is my script
pm.test(“Status code is 200”, function () {

    pm.response.to.have.status(200);

});

var jsonData = JSON.parse(responseBody);

if(pm.response.jsonData < "0.60"){

    postman.setNextRequest("water the plants");

} else {

    postman.setNextRequest("null");

    // Insert logic here for other tests

}

This is the error:
Tests added correctly | TypeError: Cannot read property ‘find’ of undefined

Hello @hameethapostman :wave:

Usually, this kind of error denotes that there is an issue with the response parsing.

var jsonData = JSON.parse(responseBody);

you have already parsing the response and storing it in a variable "jsonData "

this variable will have the entire response, to reach the element inside that, you need to pick the “key”. You might have already done this before in your previous scripts. Let us know if you couldn’t still fix it :slightly_smiling_face:

  1. Numbers don’t need double quotes in JS. so it should be 0.60 :blush:

  2. Please check the syntax for stopping the execution

image

All of @bpricilla’s comments are correct (there are definitely some adjustments that you will need to make in your “should I water the plants?” script) but the error that you listed is actually coming from the Submit your solution test.

Cannot read property ‘find’ of undefined

The test is using .find in several places, in order to verify the content of your tests; so please check the following (as they would cause the above error if false):

  • Your first request must be named should I water the plants? (ensure the case is identical)
  • Your second request must be named water the plants (ensure the case is identical) (it seems this is probably correct, because you have referenced it in your test script)

I tried to do some corrections to the script but still see the same error

pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});
var jsonData = JSON.parse(responseBody);
if(jsonData.level < 0.60){
    postman.setNextRequest("water the plants");
} else {
    postman.setNextRequest(null);
}

@hameethapostman Did you checked the points which @neilstudd have mentioned above?

I believe you have added the above script to the “Tests” section of “should I water the plants?” request. And there’s no error in the script.

Please read the documentation again and check if you have added both the request names correctly and you have added correct “Tests” for the second request “water the plants”.

After all this try submitting again and provide the screenshot which test case among the five is failing for “submit” request :slightly_smiling_face:

@bpricilla @neilstudd your comments really helped me the explore more and correct my test and keep moving to the next challenge.

Below is the solution that worked for me.