Postman 中的 For 循环未按预期工作

编程入门 行业动态 更新时间:2024-10-27 21:20:41
本文介绍了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 循环未按预期工作

    本文发布于:2023-07-27 13:48:59,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1222711.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:未按   工作   Postman

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!