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.

2 Likes

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 () {

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

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');
console.log(contentType);

pm.test("Header to have Content Type ā€˜application/pdfā€™ OR ā€˜application/jsonā€™", () => {

    pm.expect(contentType).is.not.oneOf([null, undefined]);
    pm.expect(contentType).to.be.oneOf(["application/json","application/pdf"]);

    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.