邮差测试需要随机获取标题包含

问题描述 投票:0回答:1

我有一个想要修改的测试,但我只是不知道该怎么做。对于此测试,我从响应数组中获取一个随机项目并设置 CategoryID 集合变量。问题是我将 CategoryID 用于 PUT 请求,并且可能会更改不应更改的项目。我想从标题包含“测试”的随机项目中获取 CategoryID(如果可能的话),我只是不知道该怎么做,非常感谢您的帮助。我与 Postman 的唯一经历是参加 Udemy 课程,因此我们将不胜感激。

这是我需要更改的测试:

pm.test('Ensure item is there', () => {
    pm.expect(response).to.be.an('array');
    pm.expect(response.length).to.be.above(0);

    const index = Math.trunc(Math.random() * response.length);
    const item = response[index];
    pm.expect(item).to.be.an('object');
    pm.expect(item).to.haveOwnProperty('id');
    pm.collectionVariables.set('CategoryID', item.id);
});

postman
1个回答
0
投票

我思考了一下后稍微改变了我的测试并想出了这个解决方案:

pm.test('Ensure item is there', () => {
pm.expect(response).to.be.an('array');
pm.expect(response.length).to.be.above(0);

const index = Math.trunc(Math.random() * response.length);
const item = response[index];
pm.expect(item).to.be.an('object');
pm.expect(item).to.haveOwnProperty('id');
if (item.title.includes('TEST')) {
    pm.collectionVariables.set('CategoryID', item.id);
 }   
});
© www.soinside.com 2019 - 2024. All rights reserved.