Reputation: 131
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
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
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
Reputation: 1742
You can try this:
tests["Body does not have supplied string"] = !(responseBody.has("string_you_want_to_search"));
Upvotes: 10