Change body between predefined values

Hello there!

Is there a way to change a body between predefined values everytime i run a POST request?

For example:

{
“id”: 11,
“name”: “Tutorialspoint”,
“username”: “Test1”,
“email”: “abc@gmail.com”,
}

i want the username to change between ´Test1, Test2, Test3ˋ everytime i run the request.
Is it possible?

Hi @shaga85

You’re looking for ‘data driven testing’.
Here are some resources:

Nice, this will come in handy! Altough its not exactly what i need. I only want to have the request with one of the usernames (Test1 or Test2 or Test3), and not run alll 3 names like on the collection runner. Also i would like to only run this test without the need of the collection runner.
Is this possible?

If its only 1 request you could set it as a variable and pass it in using the {{variable}} syntax.

You could add a pre-req script to set the variable before the request is sent.

How do you decide which user you want to send?

1 Like

It can be random between those 3 usernames (Test1, Test2, Test3). I don’t need to have a specific order. How do I do that on the pre-request test?

You could do something like this;

//Create an array with all options
let myList = ["User 1", "User 2", "User 3"];
//Generate a random number between 1 and array length
let i =  Math.floor(Math.random() * myList.length);
//Output the value by using the random number as the array index
console.log("Output variable = " + myList[i]);

The Lodash library is built-in to the Postman sandbox by default so you can use _.sample.

var randomString = _.sample(['Test1', 'Test2', 'Test3']);
console.log(randomString);
1 Like