邮递员 - 验证响应中的值并在控制台上打印
Postman - Validate value from Response and print on Console
从响应下面,我想获取 "responseCode" 的值并暂时存储。如果值是 1 那么在控制台上,我想写 "Test PASS"。任何人都可以分享此测试的代码吗?
{
"data":{
"transactionId":""
},
"responseMessage":"Transaction successfully done. Transaction Id : txn_15594028419901124218",
"responseCode":1
}
我尝试使用以下代码设置变量:
var jsonData = JSON.parse(responseBody);
pm.globals.set("responseCode",jsonData.data.responseCode);
这个基本的 test
将检查响应中的值,存储变量并将 Test PASS
写入控制台
pm.test("Check the Response Code is 1", () => {
pm.expect(pm.response.json().responseCode).to.eql(1);
pm.globals.set("responseCode", pm.response.json().responseCode)
console.log("Test PASS")
});
这不考虑测试失败和将 Test FAIL
写入控制台的原因,无论如何您都会在 Postman UI.
中得到它
如果您不想将其包装在 test
中,您可以这样做:
if(pm.response.json().responseCode === 1){
pm.globals.set("responseCode", pm.response.json().responseCode)
console.log("Test PASS")
}
else {
console.log("Test FAIL")
}
从响应下面,我想获取 "responseCode" 的值并暂时存储。如果值是 1 那么在控制台上,我想写 "Test PASS"。任何人都可以分享此测试的代码吗?
{
"data":{
"transactionId":""
},
"responseMessage":"Transaction successfully done. Transaction Id : txn_15594028419901124218",
"responseCode":1
}
我尝试使用以下代码设置变量:
var jsonData = JSON.parse(responseBody);
pm.globals.set("responseCode",jsonData.data.responseCode);
这个基本的 test
将检查响应中的值,存储变量并将 Test PASS
写入控制台
pm.test("Check the Response Code is 1", () => {
pm.expect(pm.response.json().responseCode).to.eql(1);
pm.globals.set("responseCode", pm.response.json().responseCode)
console.log("Test PASS")
});
这不考虑测试失败和将 Test FAIL
写入控制台的原因,无论如何您都会在 Postman UI.
如果您不想将其包装在 test
中,您可以这样做:
if(pm.response.json().responseCode === 1){
pm.globals.set("responseCode", pm.response.json().responseCode)
console.log("Test PASS")
}
else {
console.log("Test FAIL")
}