# How to verify if response contains specific text

**URL:** <https://community.postman.com/t/how-to-verify-if-response-contains-specific-text/50113>\
**Category:** Help Hub\
**Tags:** tests, json, loop\
**Created:** [August 1, 2023, 5:05am UTC](https://community.postman.com/t/how-to-verify-if-response-contains-specific-text/50113 "2023-08-01T05:05:11Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![skyroamofghostviper](https://avatars.discourse-cdn.com/v4/letter/s/71e660/32.png) [@skyroamofghostviper](https://community.postman.com/u/skyroamofghostviper)\
**Post date:** [August 1, 2023, 5:05am UTC](https://community.postman.com/t/how-to-verify-if-response-contains-specific-text/50113/1 "2023-08-01T05:05:11Z")

</div>

As a response, I have an array returned with lots of email data and I want to verify that the emails returned in the response, are only from specific domains.

Not to verify the full emails themselves but only the domains are matching an “allowed domains list” (in the example given they will be **somedomain** and **somedomain2** only).  
Please, any ideas are welcome.

Example structure of response:

```auto
[
  {
    "id": "xxxx111122223333",
        "firstName": "firstName",
        "lastName": "",
        "email": "someuser@somedomain.com",
        "replyToAddress": null,
        "phoneNumber": null,
        "internalUser": true,
        "status": "Active",
        "userStatus": {
            "id": "statusId",
            "name": "Active",
    },
    {
    "id": "xxxx4444445555555",
        "firstName": "firstName2",
        "lastName": "",
        "email": "someuser2@somedomain2.com",
        "replyToAddress": null,
        "phoneNumber": null,
        "internalUser": false,
        "status": "Active",
        "userStatus": {
            "id": "statusId",
            "name": "Active",
    },
    {
    "id": "xxxx66667777777",
        "firstName": "firstName3",
        "lastName": "",
        "email": "someuser3@somedomain3.com",
        "replyToAddress": null,
        "phoneNumber": null,
        "internalUser": false,
        "status": "Active",
        "userStatus": {
            "id": "statusId",
            "name": "Active",
      },
]

```

I’ve already tried using **oneOf** but got some errors that I should use strings, which I am actually using.

Tried also **to.include** but then test will always pass while I am actually trying to confirm that there are no other domains different from the allowed list.

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [August 1, 2023, 12:39pm UTC](https://community.postman.com/t/how-to-verify-if-response-contains-specific-text/50113/2 "2023-08-01T12:39:26Z")

</div>

You’ll need to loop through your response.

Something like…

```auto
const response = pm.response.json();

let allowedDomains = ["somedomain.com","somedomain2.com"]

response.forEach(record => {
    const emailDomain = record.email.split('@')[1];
    console.log(emailDomain);
    pm.test(`${emailDomain} is in allowed list`, () => { 
        pm.expect(emailDomain).to.be.oneOf(allowedDomains)
    })
});

```

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/f/7/f70b1dce9112a0f64a9c465d25d72565e36c4e69.png)

---

<div class="post-metadata">

**Author:** ![skyroamofghostviper](https://avatars.discourse-cdn.com/v4/letter/s/71e660/32.png) [@skyroamofghostviper](https://community.postman.com/u/skyroamofghostviper)\
**Post date:** [August 2, 2023, 9:35am UTC](https://community.postman.com/t/how-to-verify-if-response-contains-specific-text/50113/3 "2023-08-02T09:35:53Z")

</div>

Thank you very much! That worked out brilliantly.  
Now my task is to rework the script a bit so it can only display/log the domains that are not in the allowed list.

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [August 2, 2023, 10:08am UTC](https://community.postman.com/t/how-to-verify-if-response-contains-specific-text/50113/4 "2023-08-02T10:08:33Z")

</div>

@skyroamofghostviper

You will need to include an IF statement and can use the old way of writing to the tests tab (before Postman included the pm.expect library).

```auto
const response = pm.response.json();

let allowedDomains = ["somedomain.com","somedomain2.com"]

response.forEach(record => {
    const emailDomain = record.email.split('@')[1];
    console.log(emailDomain);
    if (!allowedDomains.includes(emailDomain)) {
        tests[`${emailDomain} is not in allowed list`] = false;
    }
});

```

![image](https://us1.discourse-cdn.com/getpostman/original/3X/6/e/6e6f65eecc04ebc032b3f075f3917a06ebe30e24.png)

---

<div class="post-metadata">

**Author:** ![skyroamofghostviper](https://avatars.discourse-cdn.com/v4/letter/s/71e660/32.png) [@skyroamofghostviper](https://community.postman.com/u/skyroamofghostviper)\
**Post date:** [August 3, 2023, 4:43am UTC](https://community.postman.com/t/how-to-verify-if-response-contains-specific-text/50113/5 "2023-08-03T04:43:59Z")

</div>

Thank you again.  
Is .expect already deprecated? Have not followed the latest changes since I recently started with Postman.  
I still can use it but is there something newer and more powerful in a way?

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [August 3, 2023, 9:21am UTC](https://community.postman.com/t/how-to-verify-if-response-contains-specific-text/50113/6 "2023-08-03T09:21:04Z")

</div>

No. pm.test is the current test library and pm.expect is the current assertion library built on ChaiJS.

[Expect / Should - Chai (chaijs.com)](https://www.chaijs.com/api/bdd/)

The tests function I’ve provided is the old way of writing tests before these libraries were introduced to make things easier.

However, it does have the ability to write directly to the tests tab, so its worth knowing about.

If you’ve already done the evaluation using an IF statement, there is no real point in then creating another test and assertion for the same evaluation (as you already know the outcome). So its easier to just write directly to the tests tab.

---

<div class="post-metadata">

**Author:** ![skyroamofghostviper](https://avatars.discourse-cdn.com/v4/letter/s/71e660/32.png) [@skyroamofghostviper](https://community.postman.com/u/skyroamofghostviper)\
**Post date:** [August 4, 2023, 10:37am UTC](https://community.postman.com/t/how-to-verify-if-response-contains-specific-text/50113/7 "2023-08-04T10:37:28Z")

</div>

Thank you for all these details.  
I will have to definitely dig a bit deeper!

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [August 11, 2023, 10:37am UTC](https://community.postman.com/t/how-to-verify-if-response-contains-specific-text/50113/8 "2023-08-11T10:37:52Z")

</div>

Closed on August 11, 2023, 10:37am UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
