最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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'`)
})
发布评论

评论列表(0)

  1. 暂无评论