简体   繁体   中英

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'

You can useTest 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);
});

You could use:

let name = pm.globals.get("name"),
    jsonData = pm.response.json();

pm.test("Name is correct in the response", () => {
    pm.expect(jsonData.message).to.equal(`Hi I am ${name} from 'India'`)
})

Or

let jsonData = pm.response.json()

pm.test("Name is correct in the response", () => {
    pm.expect(jsonData.message).to.equal(`Hi I am ${pm.variables.replaceIn('{{name}}')} from 'India'`)
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM