shaga85
(Danny Silva)
October 30, 2022, 12:23am
1
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?
w4dd325
(w4dd325)
October 30, 2022, 9:57am
2
Hi @shaga85
Youāre looking for ādata driven testingā.
Here are some resources:
https://learning.postman.com/docs/running-collections/working-with-data-files/
Hi Everyone!!! Letās check out some details about Data-Driven Testing with Postman using CSV and JSON filesš
Data-driven testing can be a very effective approach in testing an API against different data-sets. Postman supports CSV and JSON files to get data for the test scripts. The data-driven approach is useful when we need to execute a test with multiple sets of Data. Also, modifying or adding any data fields will just need updating the data files which is easier than going to test script andā¦
shaga85
(Danny Silva)
October 30, 2022, 10:21am
3
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?
w4dd325
(w4dd325)
October 30, 2022, 10:46am
4
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
shaga85
(Danny Silva)
October 30, 2022, 11:04am
5
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?
w4dd325
(w4dd325)
October 30, 2022, 10:04pm
6
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