如何使用邮递员验证响应正文中的特定文本
How to verify a particular text in response body using postman
响应正文
{
"message": "Hi I am 'lakshmi' from 'India'"
}
预请求脚本中提供了文本 lakshmi
,我需要在响应中验证相同内容。
我不想像下面这样验证
Var message = "Hi I am 'lakshmi' from 'India'"
因为我提到 lakshmi
作为全局变量,我如何在测试中验证
Hi I am "{{name}}" from 'India'
您可以使用Test scripts from Postman. Check also those examples
此代码应该有效
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);
});
您可以使用:
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'`)
})
或者
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'`)
})
响应正文
{
"message": "Hi I am 'lakshmi' from 'India'"
}
预请求脚本中提供了文本 lakshmi
,我需要在响应中验证相同内容。
我不想像下面这样验证
Var message = "Hi I am 'lakshmi' from 'India'"
因为我提到 lakshmi
作为全局变量,我如何在测试中验证
Hi I am "{{name}}" from 'India'
您可以使用Test scripts from Postman. Check also those examples
此代码应该有效
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);
});
您可以使用:
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'`)
})
或者
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'`)
})