Check if array contains 'true' or 'false' flag in Response

Hi team,

I have an array which contains the flag as : [false, true, false] and so on. I wrote code to fetch out using contains, include, have.property and others as an ‘if’ condition and unable to trigger the condition within ‘if’ statement given the array contains a true flag but unable to trigger that as test actually compares the entire array as true.

I know I might be using the incorrect assertion (pm.expect) here but would like to know how do I resolve this.

Code is as follows:

var flagArray = [false, true, false];

pm.test("Response: Check for Action Result", () => {
    if (pm.expect(flagArray).to.contain("true")) {
    console.log("Action A is triggered");
    }
    else {
        console.log("Action B is triggered.");
    }
})

Would it be possible to check if I can check whether the array contains true or false (even a single instance) which can trigger ‘if’ block and then in case not then the loop counter can switch to ‘else’ block.

Your elements in the array are Booleans, not strings.

The assertion needs to reflect this.

var flagArray = [false, true, false];

pm.test("Response: Check for Action Result", () => {
    if (pm.expect(flagArray).to.contain(true)) {
    console.log("Action A is triggered");
    }
    else {
        console.log("Action B is triggered.");
    }
})

I was doing that but the array is dynamically generated and can have either True or False. So using true as boolean in the contain tag results in it comparing the entire array against that boolean value. Thus I was trying put the condition within a ‘if’ block, the solution you suggested is something I tried but it results in this:

“AssertionError: expected [ false, false, false ] to include true”

What does the response actually return?

Is it a Boolean or a String in the array. As that is what data type you need to match.

Both work using your original code\concept.

var booleans = [false, true, false];

pm.test("Response: Check for Boolean", () => {
    if (pm.expect(booleans).to.contain(true)) {
    console.log("Action A is triggered");
    }
    else {
        console.log("Action B is triggered.");
    }
})

var strings = ["false", "true", "false"];

pm.test("Response: Check for string", () => {
    if (pm.expect(strings).to.contain("true")) {
    console.log("Action A is triggered");
    }
    else {
        console.log("Action B is triggered.");
    }
})

var booleans2 = [false, false, false];

pm.test("Negative Test: Check for Boolean", () => {
    if (pm.expect(booleans2).to.contain(true)) {
    console.log("Action A is triggered");
    }
    else {
        console.log("Action B is triggered.");
    }
})

var strings2 = ["false", "false", "false"];

pm.test("Negative Test: Check for string", () => {
    if (pm.expect(strings2).to.contain("true")) {
    console.log("Action A is triggered");
    }
    else {
        console.log("Action B is triggered.");
    }
})

image

So currently the test is passing for me because the array has a ‘true’ Boolean value in it. And yes the array returns only Boolean value not, Strings.

But the time when it fails for me is your example #3 - Boolean2 for Negative test. In that the same happens for me where the counters compares entire array against ‘true’ where the array only has ‘false’ in it. My question is, in that instance wouldn’t the counter move to ‘else’ block and still have the test pass?

Ah, I see the issue.

Within the test block (pm.test), as soon as an assertion fails (pm.test) then it stops all other execution and fails the test. Postman uses Chai for it’s test library, and it’s a hard fail. Chai doesn’t have soft assertions.

Therefore, you will need to have the IF statement first.

var booleans = [false, true, false];

console.log(['joe', 'jane', 'mary'].includes('jane')); // true

if (booleans.includes(true)) {
    console.log("Action A is triggered");
}
else {
    console.log("Action B is triggered.");
}

var strings = ["false", "true", "false"];

if (strings.includes("true")) {
    console.log("Action A is triggered");
}
else {
    console.log("Action B is triggered.");
}

var booleans2 = [false, false, false];

if (booleans2.includes(true)) {
    console.log("Action A is triggered");
}
else {
    console.log("Action B is triggered.");
}

var strings2 = ["false", "false", "false"];

if (strings2.includes("true")) {
    console.log("Action A is triggered");
}
else {
    console.log("Action B is triggered.");
}
1 Like

Bingpot GIFs - Get the best GIF on GIPHY

That’s it!! Worked. I had missed out that Chai does not soft assertion and I just removed the ‘pm.test’ and instead put in good’ol if-else and it works like a charm. Thanks MJ!!