Writing test with if/else statements

Hello, I am attempting to write tests for the response. My issue that I am experiencing is that if the results will either be a “SUCCESS” or an “ERROR”, but the call will always return a status code of 200.

My real problem is that the body does not show a traditional response.
EXAMPLE:
“{“Status”:“SUCCESS”,“Message”:”",“Contactid”:"{GUID}"}"

I also would like to write a test that will “pass” or “fail” based on the responses since there are about 4 unique Errors and 1 Success.

"{\"Status\":\"SUCCESS\",\"Message\":\"\",\"Contactid\":\"{GUID}\"}"

"{\"Status\":\"ERROR\",\"Message\":\"Request JSON is not properly formatted.\",\"Contactid\":\"\"}"

"{\"Status\":\"ERROR\",\"Message\":\"Hard match found. A contact with the provided (Name, Home Phone, Email) already exists {GUID}\",\"Contactid\":\"\"}"

"{\"Status\":\"ERROR\",\"Message\":\"Error while inserting contact. It looks like this address has been used before.  Want to take a look at these potential duplicates?\",\"Contactid\":\"\"}"

"{\"Status\":\"ERROR\",\"Message\":\"Error while inserting contact. Use one of these records?\",\"Contactid\":\"\"}"

This is my current test:

var jsonData = JSON.parse(responseBody);

if (responseBody.has("ERROR")){
tests["Body matches string *** E R R O R ***"] = responseBody.has("Hard match found. A contact with the provided (Name, Home Phone, Email) already exists");
tests["Body matches string *** E R R O Rs ***"] = responseBody.has("Error while inserting contact. Use one of these records?");
tests["Body matches string *** E R R O Ra ***"] = responseBody.has("Error while inserting contact. It looks like this address has been used before.  Want to take a look at these potential duplicates?");
tests["Body matches string *** E R R O Rd ***"] = responseBody.has("Request JSON is not properly formatted.");
console.log("Failed");
}
else
{
tests["Body matches string *** S U C C E S S ***"] = responseBody.has("SUCCESS");

}

console.log(responseBody);

Hey @martigi,

Welcome to the community! :rocket:

You seemed to be using a mixture of the older and newer scripting syntax. I’ve changed the code slightly to the newer syntax and added in an if/else statement:

let jsonData = pm.response.json(),
    messages = [
        "Hard match found. A contact with the provided (Name, Home Phone, Email) already exists",
        "Error while inserting contact. Use one of these records?",
        "Error while inserting contact. It looks like this address has been used before.  Want to take a look at these potential duplicates?",
        "Request JSON is not properly formatted."
    ]

if (jsonData.Status === "ERROR") {
    pm.test(`*** E R R O R *** ${jsonData.Message}`, () => {
        pm.expect(jsonData.Status).to.equal("ERROR")
        pm.expect(jsonData.Message).to.be.oneOf(messages)
    })
}
else {
    pm.test(`*** S U C C E S S *** ${jsonData.Message}`", () => {
        pm.expect(jsonData.Status).to.equal("SUCCESS")
    })
}

I extracted out the error message and placed them into an array, this is hardcoded at the moment but could be changed to be an environment variable or even driven from a datafile.

The if is checking the response to check for the ERROR status in the response body. If true, this will check to assert that Status value and also check to see if the Message text was one of the pre-defined messages from the array.

If the response is not ERROR it will choose the else route and assert that the Status is SUCCESS.

This is by no means a floorless solution and got be improved in some many ways but if it’s something simple you need to get you going, there you go. :slight_smile:

I really appreciate your help @dannydainton I will review that. Thank you!