Is there anyway to check status code with if-else condition for testing?

How could I check status code 403 within a “if statement”? I want to make decision for test cases based on status code.

For example:

if status code is 403:
DO this
return(do not need to check the next condition)

else if status code is 401:
DO this
return(do not need to check the last condition)

else:
DO this

when I try to put the condition like:

if (pm.response.to.have.status(403)){
pm.test(“Response is showing error, token expired”, function(){
});
pm.test(“Status code is 403 Forbidden”, function(){
pm.response.to.have.status(403)
});
return
}

the if condition works fine but the problem is when this condition is not true it still enters in this block and look for “(pm.response.to.have.status(403))” which shows an error because the status is not 403 and next two conditions do not work.

I’ve already tried:

if (pm.response.to.have.status(403)){
    pm.test("Response is showing error, token expired", function(){   
    });
    pm.test("Status code is 403 Forbidden", function(){
        pm.response.to.have.status(403)
    });
    return
}

else if (jsonData.message == "User not found"){
    pm.test("User not found", function(){
        pm.expect(jsonData.message).contains("User not found");
    })
    pm.test("Status code is 404", function () {
        pm.response.to.have.status(404);
    });
    return;
}

else {
    var phoneNumber = jsonData.user.phone_number;
    pm.collectionVariables.set("phoneNumber", phoneNumber)
    pm.test("Success, response is showing the details of user with phone number, " + phoneNumber, function(){
        pm.expect(jsonData.user.phone_number).to.be.eql(phoneNumber);
    })
    pm.test("Status code is 200", function () {
        pm.response.to.have.status(200);
    });
}

Hi @masum-abid

You could try using a “try/catch” statement… Here is an example of one I built to continue through all conditions, instead of stopping after the first match.

pm.test("API response contains all of the expected fields", () => {

//Parse response and save as variable
const response = pm.response.json(); 
let count = 0;

    //Tests to PASS
    try{pm.expect(response).to.have.property("status", "available");
    }catch(e){pm.test("Check status value", () => {throw new Error(e.message)}), count++;}

    try{pm.expect(response).to.have.nested.property("category.id", 0);
    }catch(e){pm.test("Check category.id value", () => {throw new Error(e.message)}), count++;}

    try{pm.expect(response).to.have.property("id", 0);
    }catch(e){pm.test("Check id value", () => {throw new Error(e.message)}), count++;}

    //Tests to FAIL with WRONG DATA
    try{pm.expect(response).to.have.property("name", "WRONG DATA");
    }catch(e){pm.test("Check name value", () => {throw new Error(e.message)}), count++;}
        
    try{pm.expect(response).to.have.nested.property("category.name", "WRONG DATA");  
    }catch(e){pm.test("Check category.name value", () => {throw new Error(e.message)}), count++;}

    //Count how many tests above fail, and fail the test case using pm.expect.fail if there is 1 or more failures
    if(count > 0) {
        pm.expect.fail("There are " + count + " failures within the above checks, please check...")
    } 

});

Off the top of my head I think yours would be something like;

try{pm.response.to.have.status(403)
    pm.test("Status code is 403 Forbidden", function(){
        pm.response.to.have.status(403)
    });
    return
    }
catch(e){pm.test("Check status value", () => {throw new Error(e.message)})
}