How to verify if response contains specific text

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:

[
  {
    "id": "xxxx111122223333",
        "firstName": "firstName",
        "lastName": "",
        "email": "[email protected]",
        "replyToAddress": null,
        "phoneNumber": null,
        "internalUser": true,
        "status": "Active",
        "userStatus": {
            "id": "statusId",
            "name": "Active",
    },
    {
    "id": "xxxx4444445555555",
        "firstName": "firstName2",
        "lastName": "",
        "email": "[email protected]",
        "replyToAddress": null,
        "phoneNumber": null,
        "internalUser": false,
        "status": "Active",
        "userStatus": {
            "id": "statusId",
            "name": "Active",
    },
    {
    "id": "xxxx66667777777",
        "firstName": "firstName3",
        "lastName": "",
        "email": "[email protected]",
        "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.

You’ll need to loop through your response.

Something like…

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

2 Likes

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.

@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).

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

1 Like

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?

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

Expect / Should - Chai (chaijs.com)

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.

1 Like

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

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