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);
1 Like

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:

4 Likes

I really appreciate your help @danny-dainton I will review that. Thank you!

@danny-dainton thank you so much for this. It helped me with a problem I had.

1 Like

@danny-dainton , Actually i used ur code for one of my issue, I changed as per my requirement, but the issue is – FAIL *** S U C C E S S *** Unauthenticated. | AssertionError: expected ‘error’ to equal ‘success’

==========================

let jsonData = pm.response.json(),

 messages = 

 [

       "The reason code was saved.",

       "We were unable to save the reason in Finesse",

       "We were unable to create the reason in ICM",

       "Unauthenticated"

 ]
 if (jsonData.type === "ERROR") 

 {

    pm.test(`*** E R R O R *** ${jsonData.message}`, () => 

    {

        pm.expect(jsonData.type).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.type).to.equal("success")

    })   

 }

===============================
Pls correct this one for failure i should get it fail with the proper message…like FAIL *** ERROR *** then Unauthenticated.

{

    "type": "error",

    "message": "Unauthenticated."

}

@danny-dainton, I got the Solution thanks sir.

tks - ajudou muito : )

I too am new to this and have a question. Is it possible to put some type of WhatIf statement somewhere to test as a dry run to return the output of the .JSON file prior to running it live?

{“NewGuestHostname”:“test01”,“RequestTicketNumber”:“Ticket001”,“Temp”:“True”,“NIC1”:{“Subnet”:“xx.xx.xxx.0”},“Template”:“Template_linux”}