0
votes

I want to make a Postman collection of requests hitting the same API endpoints as different users. For example:

  1. login "user_1"
  2. set data
  3. login "user_2"
  4. get data & check correct

I could make the login request a separate request, so the example above would be four separate requests. But for more complex situations that's a lot of login requests which is messy.

So I thought I would use a pre-request script to do the login. I could copy and paste the same login script into each pre-request script section. This is obviously not ideal as if I need to change something I'll have to change it for each request individually.

The other option is a collection level pre-request script. However there is no way to set the "user" variable to choose which user I want before running the collection pre-request script. The request level pre-request script runs after the collection level pre-request script. The only way I can see to set it before is to do it in the Test of the previous request, but that would be extremely fragile and require multiple changes should I wish to change the order of the tests.

Is there some other option I'm missing?

1
Adding the data to a JSON or CSV file and only making a single reference?Danny Dainton
By which you mean have "user_1,user_2" in a csv and have request no.1 use value no.1? I'd have to ensure that the order of the csv matches the order of requests at all times. I guess it could work but it seems like a lot of overhead...Tom H.
You'd have a single request that would use the all data from the file and check against that. I don't know what you're checking for, in each user, so it's difficult to give a better solution.Danny Dainton

1 Answers

0
votes

I ended up writing the login function into the environment variables and calling it in the pre-request script as described here: How to Write Global Functions in Postman