skip to Main Content

I have this test that I would like to modify and I just don’t know how to do it. For this test, I am getting a random item from the response array and setting the CategoryID collection variable. The problem is that I use the CategoryID for my PUT requests and may be altering items that should not be altered. I would like to get the CategoryID from a random item where the title contains "Test" if that is possible, I just don’t know how to do it and would appreciate the help. My only experience with Postman has been with a Udemy course so any help would be appreciated.

Here is the test I need altered:

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);
});

Here is an example of the response:

[
{
    "title": "Testprimary",
    "color": "salmon",
    "id": "273bda70-2dbc-429f-b143-045dccbffea7",
    "lastModified": "2024-03-06T22:12:43.161"
},
{
    "title": "Relationships",
    "color": "FF63C7",
    "id": "b46c40c2-37e1-46d7-a8e1-0ea235f071ec",
    "lastModified": "2024-01-25T22:18:22.6177142"
}]

Here is the test again with what I have come up with. In the if block I would like to grab another item from the array, how would I do that without duplicating the code I have already?

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')) {
    console.log(item.id);
    console.log(item.title);
    
} 
else {
    pm.collectionVariables.set('CategoryID', item.id);
} 

});

2

Answers


  1. Chosen as BEST ANSWER

    I figured out a way to do this without grabbing a random number or anything like that.

    pm.test('Ensure item is there', () => {
      pm.expect(response).to.be.an('array');
      pm.expect(response.length).to.be.above(0);
    
      var element = response.filter(function(item) { return item.title.includes ('TEST'); });
      if (element.length > 0) {
        pm.collectionVariables.set('CategoryID', element[0].id);
      } 
    });
    

    This is working much better. Basically, I filter the array to get all of the elements with TEST in their name and then use the if block to set the category ID.


  2. To set dynamically a variable in your test name with a randomNumber, try this :

    pm.test(["TEST - " + pm.variables.replaceIn('{{$randomInt}}')], function () {
        pm.response.to.have.status(200);
    });

    You will get random value in the the name on the TSF report.

    Take a look here :
    official documentation of Postman about random variables generation
    You will find more details about this topic and all the ramdom value that you can use.

    Hope it’s help !

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search