Add an extra criteria to if statement in Postman test currently using indexOf

Hi,

I have a Postman test that uses .indexOf to determine if a customer is from a certain list of countries. I would like to expand this test to also check the currency the customer is using; for example, is the customer from the US and using USD? If yes, run test A, if customer is from the US, but not using USD, run test B.

What would be the best way to do this? I did try:

if ([“US”].indexOf(customerCountry) > -1 && [“USD”].indexOf(customerCurrency) > -1)

However, that didn’t work - I understand that it’s not ideal to use && with indexOf, in any case?

Hey @spaceflight-physici2 :wave:

Welcome to the Postman Community! :postman:

What’s the full script?

There might be a more efficient way of doing this without using .indexOf.

Thanks! :smiley:

This is the script:

pm.test("Are all required parameters returned", function () {
    pm.expect(pm.response.text()).to.include("recordOfInterest");
    pm.expect(pm.response.text()).to.include("memberId");
    pm.expect(pm.response.text()).to.include("customerCountry");
    pm.expect(pm.response.text()).to.include("customerCurrency");
    pm.expect(pm.response.text()).to.include("productId");

});


const xml2js = require('xml2js');

let jsonObject;
xml2js.parseString(pm.response.text(), {explicitArray: false}, (err, result)=>{jsonObject = result;
});
console.log(jsonObject)

let customerCountry = jsonObject["recordOfInterest"]["customerCountry"];
pm.test("The country code must follow the ISO 3166-1 alpha-2 format", function () {
    pm.expect(customerCountry).to.match(/^[A-Z][A-Z]$/)});

if (["US"].indexOf(customerCountry) > -1) {
    pm.test("Is product ID valid for the country", function () {
    let productId = jsonObject["recordOfInterest"]["productId"];
    pm.expect(pm.response.text()).to.include("productId");
    pm.expect(productId).to.be.oneOf(["25680","25681", "25682" ,"25683", "25684"])
    })}

This is what I’d like to do with the script:

if (["US"].indexOf(customerCountry) > -1 && ["USD"].indexOf(customerCurrency) > -1) {
    pm.test("Is product ID valid for the country", function () {
    let productId = jsonObject["recordOfInterest"]["productId"];
    pm.expect(pm.response.text()).to.include("productId");
    pm.expect(productId).to.be.oneOf(["25680","25681", "25682" ,"25683", "25684"])
    })};

else if (["US"].indexOf(customerCountry) > -1 && ["!USD"].indexOf(customerCurrency) >-1){
    pm.test("Is product ID valid for the country", function () {
    let productId = jsonObject["recordOfInterest"]["productId"];
    pm.expect(pm.response.text()).to.include("productId");
    pm.expect(productId).to.be.oneOf(["26680","26681", "26682" ,"26683", "26684"])
    })};

Is anyone able to help with this? Happy to use a method other than indexOf. :smiley:

Could you also provide an example of the response body, please?

Yes, please see below

<recordOfInterest>
  <memberId>105</memberId>
  <customerCountry>US</customerCountry>
  <customerCurrency>USD</customerCurrency>
  <productId>25682</productId>
  <contactMethod>email</contactMethod>
</recordOfInterest>

Have I ever mentioned how I dislike dynamic tests :slight_smile:

Ideally, you should be in control of your test data, and your request sent to this end point should return exactly the same data every time its run.

If you want to test for US and USD, then this should be a request specifically testing for that outcome.

If you want to test other countries and currencies, then you should have multiple requests with associated tests for those outcomes. (Or you could potentially use the collection runner with a data file or the Collection Runner with a setNextRequest loop) if you want to test multiple combinations.

I’m not really understanding your IF statement. You are already targetting and defining a variable for customerCountry, so the IF statement should just be

if (customerCountry === "US" && customerCurrency === "USD") {
    console.log("customerCountry and customerCurrency match");
}

Here is some code and options to consider.

// step 1 - parse XML\Soap response 
const xmlResponse = pm.response.text();

// step 2 - parse the XML response to JSON - so its easier to work with
let parseString = require('xml2js').parseString;
const stripPrefix = require('xml2js').processors.stripPrefix;

parseString(xmlResponse, {
    tagNameProcessors: [stripPrefix],
    ignoreAttrs: true,
    explicitArray: false,
}, function (err, jsonData) {
    // handle any errors in the XML
    if (err) {
        console.log(err);
    } else {

        // do something with the result - define the fields you want to test against
        let customerCountry = jsonData.recordOfInterest.customerCountry;
        let customerCurrency = jsonData.recordOfInterest.customerCurrency
        let productId = jsonData.recordOfInterest.productId

        // Option 1 using IF statements.    

        if (customerCountry === "US" && customerCurrency === "USD") {
            pm.test("Option 1: Is product ID valid for the country (US/USD)", function () {
                pm.expect(productId).to.be.oneOf(["25680", "25681", "25682", "25683", "25684"]);
            });

        } else {
            pm.test("Option 1: Is product ID valid for the country (Non US/USD)", function () {
                pm.expect(productId).to.be.oneOf(["26680", "26681", "26682", "26683", "26684"]);
            });

        }

        // Option 2 using switch

        let countryCurrency = customerCountry + " " + customerCurrency

        switch (countryCurrency) {
            case "US USD":
                pm.test("Option 2: Is product ID valid for the country (US/USD)", function () {
                    pm.expect(productId).to.be.oneOf(["25680", "25681", "25682", "25683", "25684"]);
                });
                break;
            default:
                pm.test("Option 2: Is product ID valid for the country (Non US/USD)", function () {
                    pm.expect(productId).to.be.oneOf(["26680", "26681", "26682", "26683", "26684"]);
                });
        }
    }
});
1 Like

:100: - I was just working on a response and I was actually going to mention this as it’s always a solid comment/advice

Thanks, I’ve got it working now. :slight_smile:

Thanks for the advice on the drawbacks on dynamic testing, I’ll take it under consideration.