# How to find the element in the array and give a failed test if the element is present

**URL:** <https://community.postman.com/t/how-to-find-the-element-in-the-array-and-give-a-failed-test-if-the-element-is-present/23972>\
**Category:** Help Hub\
**Tags:** tests\
**Created:** [May 21, 2021, 1:22pm UTC](https://community.postman.com/t/how-to-find-the-element-in-the-array-and-give-a-failed-test-if-the-element-is-present/23972 "2021-05-21T13:22:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![material-operator-29](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@material-operator-29](https://community.postman.com/u/material-operator-29)\
**Post date:** [May 21, 2021, 1:22pm UTC](https://community.postman.com/t/how-to-find-the-element-in-the-array-and-give-a-failed-test-if-the-element-is-present/23972/1 "2021-05-21T13:22:50Z")

</div>

_My question_: I have an array with some element and I want to pass the test case if the element is not present in the array. And I also want to pass the test case if the array is not present in the output.

For example: The response body is as below:

{  
“Fruits”: [“Mango”]  
}

I want the test to be pass if anything other than “Mango” is present in the array or it should also pass if the array “Fruits” don’t exists.

I hope you understand the question.

---

<div class="post-metadata">

**Author:** ![neilstudd](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/neilstudd/32/8213_2.png) [@neilstudd](https://community.postman.com/u/neilstudd)\
**Post date:** [May 21, 2021, 3:26pm UTC](https://community.postman.com/t/how-to-find-the-element-in-the-array-and-give-a-failed-test-if-the-element-is-present/23972/2 "2021-05-21T15:26:07Z")

</div>

Hi @material-operator-29 and welcome to the forum! 🙂

Here’s how I would do it:

```
pm.test("Check fruits", function () {
    var jsonData = pm.response.json();
    if(jsonData.hasOwnProperty("Fruits")) {
        pm.expect(jsonData.Fruits).to.not.include("Mango");
    }
});

```

To explain what is happening here:

- The `hasOwnProperty` check is looking to see whether Fruits is present in the input. (If it is not present, it will not attempt the assertion, so the test will automatically pass.)
- The assertion uses a [chai assertion](https://www.chaijs.com/api/assert/) to check that “Mango” is not present in the array. It will pass if there are 0 array elements, or if none of the elements are called “Mango”.

I hope this helps, please ask if you have any further questions!

---

<div class="post-metadata">

**Author:** ![material-operator-29](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@material-operator-29](https://community.postman.com/u/material-operator-29)\
**Post date:** [May 21, 2021, 3:43pm UTC](https://community.postman.com/t/how-to-find-the-element-in-the-array-and-give-a-failed-test-if-the-element-is-present/23972/3 "2021-05-21T15:43:03Z")

</div>

Thank you for your response.

Still I was not able to pass the test case if the response does not have “Fruits”? Suppose it return some other array. And in this case how will I pass the above test?

Basically, I want the above functionality to check the errors in the response. If one error shows up in form of array (that particular test should fail) and, I want other test cases to be pass which have other errors in the tests.

I hope you understand the question. Once again thank you for helping:)

---

<div class="post-metadata">

**Author:** ![neilstudd](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/neilstudd/32/8213_2.png) [@neilstudd](https://community.postman.com/u/neilstudd)\
**Post date:** [May 21, 2021, 6:06pm UTC](https://community.postman.com/t/how-to-find-the-element-in-the-array-and-give-a-failed-test-if-the-element-is-present/23972/4 "2021-05-21T18:06:59Z")

</div>

Hi @material-operator-29,

I’m not sure I understand in which scenario this is not working for you. Here are a few examples that you could try in the Tests tab (I have included inline json rather than reading from response); could you possibly add another data sample which is not working for you?

```
pm.test("Should pass if nothing in the response", function () {
    var response = {};
    if(response.hasOwnProperty("Fruits")) {
        pm.expect(response.Fruits).to.not.include("Mango");
    }
});

pm.test("Should pass if Fruits present but not Mango", function () {
    var response = {"Fruits": ["Banana", "Apple"]};
    if(response.hasOwnProperty("Fruits")) {
        pm.expect(response.Fruits).to.not.include("Mango");
    }
});

pm.test("Should pass if Fruits not in the response", function () {
    var response = {"Vegetables": ["Carrot"]};
    if(response.hasOwnProperty("Fruits")) {
        pm.expect(response.Fruits).to.not.include("Mango");
    }
});

pm.test("Fails if Fruits contains Mango", function () {
    var response = {"Fruits": ["Apple", "Mango"]};
    if(response.hasOwnProperty("Fruits")) {
        pm.expect(response.Fruits).to.not.include("Mango");
    }
});

```

 ![image](https://us1.discourse-cdn.com/getpostman/original/2X/5/546f3a20aafeaff9843a46b5622c3417e6f874d4.png)

---

<div class="post-metadata">

**Author:** ![material-operator-29](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@material-operator-29](https://community.postman.com/u/material-operator-29)\
**Post date:** [May 21, 2021, 7:20pm UTC](https://community.postman.com/t/how-to-find-the-element-in-the-array-and-give-a-failed-test-if-the-element-is-present/23972/5 "2021-05-21T19:20:31Z")

</div>

Thanks a lot Neil, this is exactly what I wanted.
