[Postman] How to send a request multiple times with different values automatically using Postman Scripts

[Postman] How to send a request multiple times with different values automatically using Postman Scripts

Lets say we have 5 APIs, Each API validate on one of the following: Username, Password, PhoneNumber, Country and City

So now we have a postman collection that has 5 APIs as we stated above.

Lets say we want to test different username combinations (LowerCases,UpperCases,1 char, Maximum number of char, spaces between name... etc) and you we want all of these to be done automatically without the need of modifying the values and sending the request manually for each value.

We can solve this using Postman Pre-request Script and Post-request Tests

Lets Start

I'll be using this website as a test https://reqres.in/ in my example.

The request used is "Create User", the request takes "name" and "Job" variables, We will automate different names in this request body to see how the system respond for this.

1) Create our request in a postman collection

No alt text provided for this image

2) Change the name from "Ahmed" to "{{CurrentUsername}}" to start fetching it from the collection variables

No alt text provided for this image

3) Writing our Pre-request Script

//Step1, Get the userLists from the array

let ListOfNames= pm.collectionVariables.get("ListOfNames");


//write all your data here
if(!ListOfNames || ListOfNames.length==0){
    ListOfNames = ["Ahmed","Ramzy","Ahmeeeeeeeeeeeeeeeeeeeeed","A1223","a@b"]
}


//Step2, Get the current value to be executed.

let CurrentUserName  = ListOfNames.shift();  //Get the first element and remove this value from the array.


//Step 3 put the current and the total list after shifting in the collection variables
pm.collectionVariables.set("CurrentUserName",CurrentUserName);
pm.collectionVariables.set("ListOfNames",ListOfNames)        
No alt text provided for this image

4) Writing our post-request test

let currentUserName  = pm.collectionVariables.get("CurrentUserName")
let ListOfNames = pm.collectionVariables.get("ListOfNames")


// If the array still have values, repeat the request and get the new value 
if(ListOfNames && ListOfNames.length > 0 ){
    postman.setNextRequest("Create User")
}


//Create a test to check that the username is valid
pm.test("Status code is 201", function () {
    pm.response.to.have.status(201);
});)        
No alt text provided for this image

5) Run the collection

5 requests passed.

No alt text provided for this image

That's it.

Just imagine, Having 20 APIs with 20 value each can be automated easily by writing your values just in the array list and run the collection.

I hope that everything was clear, feel free to ask me any questions regarding this.

Note:

This script was taken form this workspace: https://www.postman.com/postman/workspace/postman-answers/

This workspace contains a-lot of scripts that may help you in your work, Highly recommended to check it.

Suprakash Mandal

Software Engineer @ Boomi

1 年

if i have to add a variable as xml attribute in xml body instead of json then how to use that varible

回复

Thanks a lot for it! How can we do the same with Soapui, do Zoe have an idea?

Ahmed Hamada

Test Lead at Pixelogic Media

2 年

Good work Ramzy! but why you don’t just use a data file? I think same results can be achieved ?? let me know what I am missing here. Example: https://learning.postman.com/docs/running-collections/working-with-data-files/

??? ????

Senior Quality Control Engineer|| ISTQB?(CTFL fl-AT-MAT-PT)

2 年
回复

要查看或添加评论,请登录

Ahmed Ramzy的更多文章

社区洞察

其他会员也浏览了