0

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"]);
                }
            });
        }
    });
});
Rohith R Nair
  • 43
  • 1
  • 10
  • Sample input: ```{ "Xyz":"blah blah", "Abc":"111", "IsFlip":"", "Asd":"4" }``` Note that my code is failing in case of empty inputs – Rohith R Nair Sep 23 '20 at 06:16

2 Answers2

1

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

Christian Baumann
  • 3,188
  • 3
  • 20
  • 37
  • I dont think thats the issue. Because im getting the correct test result for both 1 and 0 scenarios. The test is not working only when the value is empty. I tried with isFruit as well and its still showing an error. i believe its because of the if condition that we used when the input is empty. Im getting a ∅ as the value for isFruit in the console when its value is empty. – Rohith R Nair Sep 23 '20 at 07:23
  • FYI, this is the error that im getting Check whether Fruit values in Fruits returned is accurate based on fruit filter in the request | AssertionError: expected 'Y' to deeply equal 'N' . The test cases before this passed however probably because the FruitYN value was N for them. The test is failing whenever FruitYN is Y. Might it be because of the loop? – Rohith R Nair Sep 23 '20 at 07:36
  • Could you please share `request.data` and `responseBody` for a failing case? – Christian Baumann Sep 23 '20 at 07:39
  • 1
    request for fail: { "Name":"blah blah", "Color":"Red", "IsFlip":"", "Price":"4" } response for Fail: { "FruitYN": "Y", "Fruitprice": "658", "FruitType": "Full", "Taste": "Inside", "Origin": "Unknown", "Seeds": "Unknown", "Sweetness": "Other", "Calories": "Unknown", "Id": "55640777", "Weight": 0.10, "Dense": "false", "Count": 1, "ImageCount": 28 } The test cases are only failing when the value is Y – Rohith R Nair Sep 23 '20 at 07:51
  • 1
    You're request doesn't contain `IsFruit` – Christian Baumann Sep 23 '20 at 08:00
  • Sorry for that. request for fail: { "Name":"blah blah", "Color":"Red", "IsFruit":"", "Price":"4" } – Rohith R Nair Sep 23 '20 at 08:00
  • 1
    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"]);}` Thank you :) – Rohith R Nair Sep 23 '20 at 08:28
0

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"]); }

Rohith R Nair
  • 43
  • 1
  • 10