dev_in_training
dev_in_training

Reputation: 433

Is there a way to input multiple environment variables into a url for testing on Postman?

I am testing an endpoint in Postman using a url like this, {{api_url}}/stackoverflow/help/{{customer_id}}/{{client_id}}. I have the api_url, customer_id, and client_id stored in my environment variables. I would like to test multiple customer_id and client_id without having to change the environment variables manually each time. I created a csv to store a list of customer_id and one to store client_id. When I go to run collection, it will only allow me to add one file. Is there another way to do this if I want to iterate through my tests to automate them?

Upvotes: 0

Views: 850

Answers (2)

PDHide
PDHide

Reputation: 19929

you can use postman.setNextRequest to control the flow. The below code runs the request with different values in the arr variable

url:

{{api_url}}/stackoverflow/help/{{customer_id}}/{{client_id}}

now add pre-request:

// add values for the variable in an array
const tempArraycustomer_id = pm.variables.get("tempArraycustomer_id")
const tempArrayclient_id = pm.variables.get("tempArrayclient_id")

//modify the array to the values you want
const arrcustomer_id = tempArraycustomer_id ? tempArraycustomer_id : ["value1", "value2", "value3"]
const arrclient_id = tempArrayclient_id ? tempArrayclient_id : ["value1", "value2", "value3"]


// testing variable to each value of the array and sending the request until all values are used

pm.variables.set("customer_id", arrcustomer_id.pop())
pm.variables.set("client_id", arrclient_id.pop())

pm.variables.set("tempArraycustomer_id", arrcustomer_id)
pm.variables.set("tempArrayclient_id", arrclient_id)


//end iteration when no more elements are there
if (arrcustomer_id.length !== 0) {
    postman.setNextRequest(pm.info.requestName)
}

Upvotes: 1

lucas-nguyen-17
lucas-nguyen-17

Reputation: 5917

You can add both customer_id & client_id in one csv file. Postman will iterate n times (n = number of csv lines, except header)

enter image description here

Upvotes: 1

Related Questions