javascript - How to verify a particular text in response body using postman - Stack Overflow

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

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'

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'
Share Improve this question edited Jun 22, 2020 at 7:28 D. Schreier 1,7861 gold badge24 silver badges35 bronze badges asked Jun 15, 2020 at 16:16 LakshmiLakshmi 171 silver badge6 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

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);
});

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'`)
})

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745120137a4612365.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信