Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postman 邮递员-验证响应中的值并在控制台上打印_Postman_Postman Testcase - Fatal编程技术网

Postman 邮递员-验证响应中的值并在控制台上打印

Postman 邮递员-验证响应中的值并在控制台上打印,postman,postman-testcase,Postman,Postman Testcase,从下面的Response中,我想获取“responseCode”的值并临时存储。若一个值是1,那个么在控制台上,我想写“测试通过”。任何人都可以共享此测试的代码吗 { "data":{ "transactionId":"$1" }, "responseMessage":"Transaction successfully done. Transaction Id : txn_15594028419901124218", "responseCode":1 } 我

从下面的Response中,我想获取“responseCode”的值并临时存储。若一个值是1,那个么在控制台上,我想写“测试通过”。任何人都可以共享此测试的代码吗

{
   "data":{
      "transactionId":"$1"
   },
   "responseMessage":"Transaction successfully done. Transaction Id : txn_15594028419901124218",
   "responseCode":1
}
我尝试使用以下代码设置变量:

var jsonData = JSON.parse(responseBody); 
pm.globals.set("responseCode",jsonData.data.responseCode); 

这个基本的
测试
将检查响应中的值,存储变量,并将
测试通过
写入控制台

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")
});
这并不能解释测试失败和将
testfail
写入控制台的原因,不管怎样,您都可以在Postman UI中看到这一点

如果您不想将其包装在
测试中
,您可以执行以下操作:

if(pm.response.json().responseCode === 1){
    pm.globals.set("responseCode", pm.response.json().responseCode)
    console.log("Test PASS")
}
else {
    console.log("Test FAIL")
}

这个基本的
测试
将检查响应中的值,存储变量,并将
测试通过
写入控制台

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")
});
这并不能解释测试失败和将
testfail
写入控制台的原因,不管怎样,您都可以在Postman UI中看到这一点

如果您不想将其包装在
测试中
,您可以执行以下操作:

if(pm.response.json().responseCode === 1){
    pm.globals.set("responseCode", pm.response.json().responseCode)
    console.log("Test PASS")
}
else {
    console.log("Test FAIL")
}

到目前为止,您尝试了什么?var jsonData=JSON.parse(responseBody);pm.globals.set(“responseCode”,jsonData.data.responseCode);到目前为止,您尝试了什么?var jsonData=JSON.parse(responseBody);pm.globals.set(“responseCode”,jsonData.data.responseCode);嗨,丹尼,非常感谢你提供的解决方案。这对我很有用。你能接受这个作为答案,结束这个问题吗。嗨,丹尼,非常感谢你提供的解决方案。这对我来说很有效。你能接受这个答案,结束这个问题吗。