4

I have a test in postman where I can validate a string value in an array but only if I know the index in which it is returned. The problem I run into is the index in which the value is returned can be random.

This is what the json looks like that I'm validating.

{
    "cart": [{
        "offeringId": "1234",
        "offeringName": "Test1",
        "totalOfferingAmount": -15,
        "offeringTypeQualifier": "Test",
        "productTypeQualifier": "Product",
        "quantity": -1,
        "messages": [],
        "autoAdd": false,
        "autoAction": "removed",
        "addedByProcessor": true,
        "qualificationLevel": "FQ",
        "qualificationDetails": []
    }, {
        "offeringId": "5678",
        "offeringName": "Test2",
        "totalOfferingAmount": -15,
        "offeringTypeQualifier": "Test",
        "productTypeQualifier": "Product",
        "quantity": -1,
        "messages": [],
        "autoAdd": false,
        "autoAction": "removed",
        "addedByProcessor": true,
        "qualificationLevel": "FQ",
        "qualificationDetails": []
    }],
    "isCartValidForCheckout": true,
    "_status": []
}

Here is my postman test

var data = JSON.parse(responseBody);
tests ["Verify offeringId"] = data.cart[0].offeringId === "1234"
1
  • You probably need to iterate over the array to see if you find an object that passes your test, assuming your intention is to say "array has an object with offeringId === 1234". Also please copy and paste your code directly, instead of taking a screenshot. Commented Nov 30, 2016 at 17:44

4 Answers 4

2

In Postman a good shorthand with a regular expression for testing known values.

cart.forEach(function(x, i) {
    tests['Test known offeringName ' + cart[i].offeringName ] = true === /^Test1$|^Test2$/.test(cart[i].offeringName);  } 
)
Sign up to request clarification or add additional context in comments.

Comments

2

This is the answer tested in the current release of Postman v6.1.2 (native app, not in browser):

pm.test("Test for offeringId=1234", function () {

 var jsonData= JSON.parse('{"cart":[{"offeringId":"1234","offeringName":"Test1","totalOfferingAmount":-15,"offeringTypeQualifier":"Test","productTypeQualifier":"Product","quantity":-1,"messages":[],"autoAdd":false,"autoAction":"removed","addedByProcessor":true,"qualificationLevel":"FQ","qualificationDetails":[]},{"offeringId":"5678","offeringName":"Test2","totalOfferingAmount":-15,"offeringTypeQualifier":"Test","productTypeQualifier":"Product","quantity":-1,"messages":[],"autoAdd":false,"autoAction":"removed","addedByProcessor":true,"qualificationLevel":"FQ","qualificationDetails":[]}],"isCartValidForCheckout":true,"_status":[]}');

//var jsonData = pm.response.json(); // This is the production response. Uncomment this.
var size = jsonData.cart.length; // cart object is array. Get its size.

var flag = false;
console.log("Test for offeringId. size=[" + size + "]");

for(var i = 0; i < size; i++) {
  console.log("jsonData.cart[" + i + "].offeringId=[" + jsonData.cart[i].offeringId + "]");   // Debug 
  console.log(jsonData.cart[i].offeringId);
  if (jsonData.cart[i].offeringId == "1234") {
    console.log("1234 is found........");
    flag = true;
  }
}
pm.expect(flag).to.eql(true);
});

Comments

0

I think you should loop through the entire array and check if the string value is contained in any index.

var array = JSON.parse(responseBody)
let testResult = array.find((each)=>{
    return each.offeringID === '1234'
 });  

Comments

0

i dont know what you plan to do once the validation is true , but if is just to check if that specific id is inside the response , you can always do

var size = data.cart.length;
var flag = false;
for(var i = 0; i < size; i++) {
  if(data.cart[i].offeringId === "1234") {
    flag = true;
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.