Rohith R Nair
Rohith R Nair

Reputation: 45

In postman tests, how can i assert the response values with one of multiple values when the input parameter can be empty?

I have an input parameter called IsFruit which can be either 0 or 1. If the value is 0, the response should return fruits with FruitsYN value as N. Similarly if value is 1, FruitsYN should be Y. If this parameter has no values, response can have FruitsYN Y or N. This is the code that i wrote but while some cases are a pass, others are failing. I printed IsFruit when the value is empty in the input. It looks like ∅

var requestData = JSON.parse(request.data);
var responseData = JSON.parse(responseBody);
var IsFruit=requestData.IsFruit;// IsFruit can be either 0,1 or empty
pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
    pm.test("Check if Fruits found with this search criteria", function() {
        pm.expect(responseData.Message).to.not.eql("No Fruits found with this search criteria");

        var list = (responseData.Fruits).length;
        //console.log(list);
        var a = [];
        
        for (var i = 0; i < list; i++) {
            var counter = responseData.Fruits[i];
            FruitsYN = counter.FruitsYN
            //console.log(FruitsYN);
            a.push(FruitsYN)
            pm.test("Check whether Fruit values in the Fruits returned is accurate based on fruit filter in the request", function() {

                if (IsFruit == 0) {

                    pm.expect(FruitsYN).to.eql("N")

                }
                if (IsFruit == 1) {
                    pm.expect(FruitsYN).to.eql("Y")
                }

                if (IsFruit =="") {
                    pm.expect(FruitsYN).to.be.oneOf(["Y", "N"]);
                }
            });
        }
    });
});

Upvotes: 0

Views: 2293

Answers (2)

Rohith R Nair
Rohith R Nair

Reputation: 45

I fixed it. :) . i just added a new if condition. Apparently the issue was because of the null value. So i handled it like this if (isFruit == "") { isFruit = "empty" Then i just used this later in the condition check if (isFruit == "empty") { pm.expect(FruitYN).to.be.oneOf(["Y", "N"]); }

Upvotes: 0

Christian Baumann
Christian Baumann

Reputation: 3434

Looks like variable names starting with upper case characters are not working in Postman.

Below code is working for me:

let requestData = {"IsFruit":""};
let isFruit=requestData.IsFruit;// IsFruit can be either 0,1 or empty

if (isFruit =="") {
    console.log("Empty string");
}

Whereas it is not working if I use IsFruit

Upvotes: 1

Related Questions