
In the previous article, I wrote about test Response, now I will continue to write about Postman pre request script. Here are the steps when sending a request. The Pre-request part will be the part Postman will process before sending the request, and the test script to handle the returned response.

So what can Pre-request do? It handles only one part, which is creating data (variables) to pass to the param in the request.

There is only 1 function but extremely useful in different cases.
For example: I have an API that creates a ride like grab and uber, including the following params.
- _user_id
- _token
- Source
- Destination
- Departure_datetime
Add a little requirement:
- _user_id : obtained after login
- _token : obtained after login
- Source: Departure location
- Destination: Destination location Departure_datetime: The departure time of the trip is always greater than the current time.
When I first tested this API, I could only create 2 variables _user_id and _token after running the login API and then using the Test Script to save the response. You can read the previous post to learn more.

As for the 3 variables Source, Destination, Departure_datetime, I have to manually re-enter it every time I run the API —> it’s very laborious, if I don’t fix the above params, my API will be wrong.
Suppose today, October 9, 2017, I run the correct API, but tomorrow (October 10, 2017) I have to fix a bunch of APIs because of the wrong date. T_T I was like this for a while, until I learned how to use Pre-Request.
Handling Departure_datetime

I wrote a getToday function to ensure that every time the Testcase runs, it returns today’s date without having to maintain and modify the API.
You might think you’re good at inventing the function yourself :)))) No, you just need to google “Method getToday javascript”. After that, I just edited the code a bit to get the random hour I wanted. Finally, save that variable to Environment and you’re done.

Handling Source, Destination
In essence, both of these params fill in the value of the location, but I can’t test with only 2 locations, it will create a bunch of trips that are identical to the destination and departure point. I need a large number of locations and then I will randomize the Source, Destination part.
I create an array containing all the locations (I export from the DB) and then take random and save it as 2 variables in Environment.
Eg:
var myArray = [ {
"source" : "Tokyo, Japan"
},
{
"source" : "Gifu Prefecture, Japan"
},
{
"source" : "Aichi Prefecture, Japan"
},
{
"source" : "Tokyo Disney Resort, Urayasu, Chiba Prefecture, Japan"
}
];
var source = myArray[Math.floor(Math.random() * myArray.length)]['source'];
var destination = myArray[Math.floor(Math.random() * myArray.length)]['source'];
pm.environment.set("source", source);
pm.environment.set("destination", destination);

Once done, there will be an API with dynamic, flexible parameters, not APIs with fixed data.

That’s it, if you have any questions, please comment below.
To read more articles about API testing , please go this link