Writing IF statements in the Tests sandbox

My question:

Hi All! I’m a QA working with Postman and learning how to write Tests and i have a query regarding IF statements (if this is the right solution for my problem)

Essentially i have a folder labelled “Certificates” and have a couple of call within this folder, i’m making one call for some certificate information, which is returned in JSON, however i have another call which can return the certificate itself in a PDF format.

The tests I’m running daily are scheduled at the folder level for these calls and I’m wanting to know if i can write a test which includes an IF Statement, for example my current test features this:

pm.test(“Header to have Content Type ‘application/pdf’ OR ‘application/json’”, function () {

pm.expect(pm.response.headers.get('Content-Type')).to.be.oneOf(["application/json","application/pdf"]) });

i want to know if i can then take this further and say IF this returns “application/json” then i want it to see if “Example ID 1”, “Example ID 2” is returned.

Is this something that is possible?

You can wrap the test (pm.test) in an IF statement, or wrap the assertions (pm.expect) within the test in an IF statement (which sounds like is what you are asking).

Personally, I loathe IF statements in tests. (but on the same lines, I can’t say it breaks best practice either).

Tests should be singular and your test data should be a known state.
OR statements usually break the singular rule.
It should return the same data each time so there should be no need for IF statements.

It sounds like the test for the JSON response should be in that request, and the test for the PDF should be in that request. Not set at the folder level.

The re-usability of tests in Postman is a bit lacking, so I can understand sometimes why you want to put things in at the folder level. But I always refer back to the singularity of the test and the test data.


Hi thanks for responding, i’m very new with Postman and coding in general so running them at a folder level is what i’ve been doing, I get where you’re coming from with putting the respective tests in each request. I’m just kind of finding my feet right now and learning what is best practice.

I’m encountering some issues with the IF Statement

pm.test(“Header to have Content Type ‘application/pdf’ OR ‘application/json’”, function () {


if (pm.response.headers.get('Content-Type') === "application/json") {
    //if app/json then check the below

    pm.expect("Contain Example ID", function (){
        pm.expect(pm.response.text()).to.include("Example ID")

    pm.expect("Contain Example ID 2", function (){
        pm.expect(pm.response.text()).to.include("Example ID")

    pm.expect("Contain Example ID 3", function (){
        pm.expect(pm.response.text()).to.include("Example ID")

    } else {
        // if app/pdf then do only check for PDF

        (pm.response.headers.get('Content-Type') === "application/pdf"); 


You haven’t said what the problem is?

However, I would probably have two separate IF statements and not use ELSE.

Just to note, within a pm.test function, the first pm.expect assertion to fail will stop any more code within the function from executing. So it would not execute the if statement at all.

If the first pm.expect assertion fails then the rest of the code won’t execute, so you are sort of expecting if it hits the IF statements to be true for one of the header types so using ELSE is ok, but its just easier to read\troubleshoot with two IF statements.

As you haven’t said what your problem is, I would recommend ensuring you console log the elements you are working with.

Define a variable for contentType and use that in your if statement.

Something like.

let contentType = pm.response.headers.get('Content-Type');

pm.test("Header to have Content Type ‘application/pdf’ OR ‘application/json’", () => {

    pm.expect(contentType).is.not.oneOf([null, undefined]);

    if (contentType === "application/json") {
        console.log("application JSON")

    if (contentType === "application/pdf") {
        console.log("application PDF")

It is more lines of code, but easier to troubleshoot.

Is the contentType coming back correct (check the console). For a lot of my API’s it will be “application/json; charset=utf-8”. Which would fail the assertion.

One a side note, your response is JSON, so use pm.response.json() to parse it instead of text() and drill down to the elements in your assertions instead of just checking if the element exists somewhere in the text. Also, you are checking for the same “Example ID” in all three assertions. That doesn’t look right.

Apologies for the late response, and thanks for the code you provided to it aided me in my resolution.

For context my issue was i wanted my test to determine which content type was being returned but my code kept failing.

Either way thanks for your help as it’s working for me now!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.