Search code examples
javascriptpostmanpostman-testcase

How to verify a particular text in response body using postman


Response Body

{
    "message": "Hi I am 'lakshmi' from 'India'"
}

The text lakshmi is provided in pre-request script and I need to verify the same in the response. I don't want to verify like this below

Var message = "Hi I am 'lakshmi' from 'India'"

Since I mentioned lakshmi as global variable how do I verify in the test like

Hi I am "{{name}}" from 'India'

Solution

  • You can use Test scripts from Postman. Check also those examples

    This code should work

    pm.test("Status test", function () {
        pm.response.to.have.status(200);
    });
    
    var expectedValue  = pm.environment.get("lakshmi");
    pm.test("Body contains variable", function () {
        pm.expect(pm.response.text()).to.include(expectedValue);
    });
    
    // IF YOU WANT TO CHECK THE WHOLE SENTENCE
    var expectedValue  = "Hi I am '" + pm.environment.get("lakshmi") + "' from 'India";
    pm.test("Body contains variable", function () {
        pm.response.to.have.body(expectedValue);
    });