0

I have a nested 'if, else if, else' test script for #Postman tests but i get some condition executed when it is not meant to be and others not executed properly.

When I comment some section out of the condition in the test, it works as desired.

var jsonData = JSON.parse(responseBody);

if (responseCode.code === 200){
    tests["Http status is 200"] = responseCode.code === 200;
try{
    tests["ID: "] = jsonData.id === jsonData.id;
    tests["Name is: "] = jsonData.name === jsonData.name;
    tests["Description is: "]= jsonData.description === jsonData.description;
    }
    catch(e){}
}

else if (responseCode.code !== 200){
    //using invalid token
       tests["Http status is 401"] = responseCode.code === 401;
try{
   tests["Response Message is: " + jsonData.error] = jsonData.error;
}
catch(e){}
}

else(responseCode.code !== 200){
    //record not found
       tests["Http status is 404"] = responseCode.code === 404;
try{
    tests["Status Code is " + responseStatus.status] = responseStatus.status;
    tests["Response Message is: " + jsonData.message] = jsonData.message;
    tests["Time is " + jsonData.timestamp] = jsonDat.timestamp;
}catch(e){}
}

See Sample result

Senny
  • 1
  • 1
  • Any reason why you have so many conditions in one test? You should try to break these out to isolate what you are testing. Send a payload so that you get the response code of 200, so your tests just look for that. Another test and payload for 404, 401, etc. The problem you run into is your test code is now as convoluted as the source code you are testing. Errors can now be found by simply having bugs in your tests, not in the API you are calling. – Steven Scott Oct 09 '19 at 15:27
  • @Steven Scott: thanks, that is what I did eventually and creating requests for other conditions using dynamic variable in some cases. Thanks – Senny Oct 14 '19 at 09:24

0 Answers0