API Testing – Postman pre request script

API testing

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

Leave a Reply

Your email address will not be published. Required fields are marked *

API testing

Test runner – API testing

Test runner – According to the previous post, you already know how to create simple tests for each API, but a project has too many APIs and too many different tasks, each task is a collection of several APIs how must be solved. Along with that is the management method that you think is applicable […]

API testing

How to test API

After reading “test API with Postman” series, you should be able to grasp the basic knowledge of the API and the functions Postman provides. But how to arrange tests and write Testcases for API still seems not very clear, so today I will write a post about how to test API properly. Reminder of knowledge […]

API testing

API testing – API documentation

Postman, in addition to providing an API testing tool, also helps us to make API documentation extremely professional and easy. This API document can be shared by both the team and the client. Usually, the API is usually written by Dev on google sheets, but at a certain stage of development, the dev will be […]