Reputation: 17
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'
Upvotes: 1
Views: 7638
Reputation: 25931
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'`)
})
Upvotes: 2
Reputation: 1788
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);
});
Upvotes: 2