Postman 中的 For 循环未按预期工作 [英] For Loop in Postman not Working as Expected
本文介绍了Postman 中的 For 循环未按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 Postman Runner 并运行 JSON 数据文件并从控制台中的数据文件中取回每条记录.
I am trying to use Postman Runner and run through a JSON data file and get every record back from the data file in the console.
目前我只取回第一行.
我的 JSON 数据文件如下所示:
My JSON data file looks like this:
[
{
"line1": "13579",
"line2": "2468",
"line3": "1234",
"line4": "5678",
},
{
"line1": "13468",
"line2": "2425",
"line3": "12578",
"line4": "567343",
}
]
我尝试过的代码是:
var res = JSON.parse(responseBody);
for (let i = 0; i < res.length; i++) {
console.log("actual data: " + i + " = " + data[i].line1);
// tests start
pm.test("iteration: " + i + " = " + "expected line1 = " + data.line1 + " | Actual line1 = " + res[i].line1 , function () {
pm.expect(res[i].line1).to.equal(data[i].line1);
});
}
但是,这会在控制台中打印出错误:
However, this prints out an error in the console:
"TypeError: Cannot read property 'line1' of undefined"
如果我将其更改为 console.log("actual data: " + i + " = " + data.line1);
,它会写出:
If I change this to console.log("actual data: " + i + " = " + data.line1);
, it then writes out:
actual data: 0 = 13579
actual data: 1 = 13579
actual data: 2 = 13579
[![邮差跑者][1]][1]
[![Postman runner][1]][1]
- 我是否正确使用了 for 循环?
- 我做错了什么?
编辑
我在发送 URL 时忘记添加 res
返回的内容:
[
{
"line1": "13579",
"line2": "2468",
"line3": "1234",
"line4": "5678"
},
{
"line1": "13468",
"line2": "2425",
"line3": "12578",
"line4": "567343"
},
{
"line1": "1test8",
"line2": "24te25",
"line3": "125st78",
"line4": "567test343"
}
]
推荐答案
我的同事在这方面帮助了我.
My colleague helped me on this.
解析是使用res[pm.info.iteration].line1
.
完整的测试是:
// start tests
pm.test("expected line1 = " + (data["line1"]) + " | Actual line1 = " + (res[pm.info.iteration].line1), function () {
pm.expect(res[pm.info.iteration].line1).to.equal(data["line1"]);
});
这篇关于Postman 中的 For 循环未按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文