Skip specific tests if empty array in response

Skipping specific tests within a single endpoint if there is an array missing from the response json

Hi All!

I have a set of tests for a single endpoint and i want to know if there is a way that i am able to skip certain tests if there is an empty array in the response json. I was hoping there was a way to do this in the pre-request script, setting something up that looks through the response json to see if there is a specific array is populated, if it isnt then it will skip certain tests. If not then is there a way to wrap the tests in an IF statement?

Below is one of the tests that i want to skip if and ‘offers’ array is not present in the response

pm.test("Offers - offers array is correct", function () {
    const responseJson = pm.response.json();
    let stock_quantity = responseJson.widget.data.offers[0].offer.stock_quantity
    let percentage_saving_label = responseJson.widget.data.offers[0].offer.percentage_saving_label
    let product_note = responseJson.widget.data.offers[0].offer.product_note;
    let display_name = responseJson.widget.data.offers[0].offer.display_name


    pm.expect(responseJson.widget.data.offers[0].offer.name).to.be.a("string");
    pm.expect(responseJson.widget.data.offers[0].offer.price).to.be.a("string");
    pm.expect(responseJson.widget.data.offers[0].offer.in_stock).to.be.oneOf([true, false]);
    pm.expect(Object.prototype.toString.call(stock_quantity)).to.be.oneOf(["[object String]","[object Null]"])
    pm.expect(responseJson.widget.data.offers[0].offer.currency_iso).to.be.a("string");
    pm.expect(responseJson.widget.data.offers[0].offer.currency_symbol).to.be.a("string");
    pm.expect(responseJson.widget.data.offers[0].offer.link).to.be.a("string");
    pm.expect(responseJson.widget.data.offers[0].offer.link_text).to.be.a("string");
    pm.expect(responseJson.widget.data.offers[0].offer.merchant_link_text).to.be.a("string");
    pm.expect(responseJson.widget.data.offers[0].offer.label).to.be.a("string");
    pm.expect(Object.prototype.toString.call(percentage_saving_label)).to.be.oneOf(["[object Number]","[object Null]"])
    pm.expect(Object.prototype.toString.call(product_note)).to.be.oneOf(["[object String]","[object Null]"])
    pm.expect(Object.prototype.toString.call(display_name)).to.be.oneOf(["[object String]","[object Null]"])
    pm.expect(responseJson.widget.data.offers[0].offer.display_labels).to.be.a("string");
    pm.expect(responseJson.widget.data.offers[0].offer.display_primary_label).to.be.a("string");

})

Example of the array in the response

"offers": [
                {
                    "id": 2694807,
                    "match_id": 2694807,
                    "product_key": "25996-648193497",
                    "score": 23894,
                    "mobile_match_score": 23894,
                    "percentage": 100,
                    "click_count": 0,
                    "product_type": 1000,
                    "bundle_models": [],
                    "model_matched": [
                        "Sennheiser Sennheiser E 935"
                    ],
                    "model": "Sennheiser Sennheiser E 935",
                    "model_id": 833312,
                    "an": "Generic",
                    "offer": {
                        "name": "Sennheiser E 935",
                        "price": "95.00",
                        "in_stock": true,
                        "stock_quantity": null,
                        "currency_iso": "GBP",
                        "currency_symbol": "£",
                        "link": "https://www.thomann.de/gb/sennheiser_e935_dynamisches_mikrofon.htm?offid=1&affid=195&subid=hawk-custom-tracking",
                        "link_text": "View Deal",
                        "merchant_link_text": "View Deal at Thomann",
                        "label": "",
                        "percentage_saving": null,
                        "percentage_saving_label": null,
                        "money_saving": null,
                        "money_saving_label": null,
                        "product_note": null,
                        "display_name": "Sennheiser Sennheiser E 935",
                        "display_labels": "",
                        "display_primary_label": ""
                    },

The purpose of this is that sometimes there arent any offers for the product, so the ‘offers’ array returned can be empty, which causes my tests to fail.

Thanks!

Hey @joe-hicks :wave:

Welcome to the Postman Community! :postman:

You could wrap the tests in a conditional statement that checks if the array is empty of not or check that the array’s length is not zero.

    let responseJson = pm.response.json();
    if (responseJson.widget.data.offers.length === 0) {
        // do this
    } else {
        // do that
    }

You could even do that in a separate test rather than trying to test for everything in a single test.

Thanks Danny!

Nice and simple and it begs the question why i didnt think of structuring it like this in the first place! :man_facepalming:

Thanks again

1 Like

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