Kamal
Kamal

Reputation: 131

How to check if responseBody does not contain string in postman tests

To check for a string in the responseBody, we do the search as follows

tests["Body matches string"] = responseBody.has("string_you_want_to_search");

How to check if responseBody does not contain string in postman tests ?

Upvotes: 13

Views: 35271

Answers (3)

Mathieu VIALES
Mathieu VIALES

Reputation: 4772

The most "natural" and readable syntax is as follow, using a "fluent" style API

pm.test("Body matches string", function () 
{
    pm.expect(pm.response.text()).to.not.include("string_you_want_to_search");
});

As Stefan Iancu pointed out, this only seems to work in the standalone version of Postman.

Upvotes: 34

Midas
Midas

Reputation: 471

var data = JSON.parse(responseBody);
tests["Body does not contain string_you_want_to_search"] = data.search("string_you_want_to_search") < 0;

Upvotes: 0

Dinesh Kumar
Dinesh Kumar

Reputation: 1742

You can try this:

tests["Body does not have supplied string"] = !(responseBody.has("string_you_want_to_search"));

Upvotes: 10

Related Questions