Collections 如何在postman collection runner中设置环境变量?

Collections 如何在postman collection runner中设置环境变量?,collections,automated-tests,postman,web-api-testing,postman-collection-runner,Collections,Automated Tests,Postman,Web Api Testing,Postman Collection Runner,我有一个名为getcampaignlist的API。它返回给我所有的活动列表,根据相关人员的描述。像在图像中一样,我有多个带有id和描述的活动。我想使用 postman.setEnvironmentVariable(“cmid”,jsonData.id) 或 “postman.setEnvironmentVariable(“cmid”,jsonData.id) 哪里 jsonData.campaigname==“在线游戏” 我的意思是,我想通过collection runner在循环中使用所有

我有一个名为getcampaignlist的API。它返回给我所有的活动列表,根据相关人员的描述。像在图像中一样,我有多个带有id和描述的活动。我想使用

postman.setEnvironmentVariable(“cmid”,jsonData.id)

postman.setEnvironmentVariable(“cmid”,jsonData.id)
哪里
jsonData.campaigname==“在线游戏”


我的意思是,我想通过collection runner在循环中使用所有这些id。如何在环境变量中设置值。因为当我设置jsonData.id时,它无法决定应该在环境变量“cmid”中设置哪个id值并返回false

对于收集运行程序,您通常(csv或json)使用
数据访问变量,例如
data.id

此代码可以添加到
测试
选项卡,以迭代响应数据(类似于您的示例)并将与
在线游戏
匹配的ID指定为环境变量。这可以通过使用URL中的
{{cmid}}
在另一个请求中引用

const result = pm.response.json()

for (var i = 0; i < result.length; ++i)
     if (result[i].campaignName === "Online Games") {
         pm.environment.set('cmid', result[i].id)
     }
const result=pm.response.json()
对于(变量i=0;i
使用邮递员的示例:

您所说的“返回错误”是什么意思?在测试选项卡中,您是否有
postman.setEnvironmentVariable(“cmid”,jsonData.id)
代码的
var jsonData=pm.response.json()
?此外,由于它是一个数组,因此不会得到任何结果,因此
jsonData[0].id
将获得列表中的第一个
id