Working with Pre-request & Test in Postman

Posted by: admin September 19, 2023 No Comments
API-Testing-Using-Postman

What is a postman?

Postman is a Client API tool that is used to create, share, test, and develop APIs[Application Programming Interface]. It caters to major functionalities, required for developing an API. Postman has Collections where you can store test cases in bulk and execute them sequentially or individually according to the requirements. In the same way for different types of test-cases Postman have different types of requests i.e. GET, POST, PUT, DELETE, PATCH, etc.

How does Postman work?

Postman has tabs to deal with different requests and test cases, similar to a web browser.

Then comes the Title of the test case and a dropdown under it. The dropdown contains types of Methods to perform and Requests. There is a Search Bar right side of the dropdown to enter the URL of the endpoint with the required data to execute that test case i.e. Parameters.
After filling in all the above details we can hit the API with the Send button and after successful execution of the test case, we can save that using the Save button given on the right side of the Send button.
We can see the response every time after hitting the test case also after the execution of the test case postman saves the request in the History.

What do different methods in Postman do?

Some of the common methods of postman perform the following activities:-

GET: This method just retrieves the information from the server using the URL.

POST: This method sends data to the server and creates new entries in the database.

PUT: This method is used to update or create entries in the database by replacing all the data sent in the specified format only.

PATCH: This method is used to update the specified entries in the database. We do not need to send all the data while using this method just send those which we need to update.

DELETE: This method is used to delete the specified entry.

How to perform API testing via Postman?

Many people don’t operate on test cases, they just change parameters manually and think the testing has been achieved but this is not the way postman testing is designed for. Postman is meant to achieve a lot bigger scope of testing. We can do this manually for a few scenarios but what if we have hundreds and thousands of such cases? Can we remember all of them? That’s why we should practice making test cases of each scenario and store them in a collection so that we can execute them in batch or manually accordingly.

Before performing any test we need to have test-cases and for making that we need a good understanding of the project and related scenarios like what we are going to test.
Obviously, we can not build our understanding via postman but we can make test cases.
Test cases are grouped under folders according to the endpoints. Test cases are often termed as Requests in postman.

Let’s say we have to test an online money transferring website then sign-up can be one endpoint and Log-in can be the next.
Inside the log-in folder, we can have test cases for different scenarios like Missing parameters, Validating parameters, etc. Here we can see how test cases are confined under a folder on a postman.

In a very similar manner group of folders are confined under Collection.

We can run all the folders in the collection or individual folder at a time. In order to run a collection, we must have at least 2 test cases in the folder.

How to create a Request:

In order to create a request first, we need to create a collection by Hitting the “+ Create new collection link”

Once the collection gets created, right-click on the collection and create a Request by selecting the Add Request button and filling up the name and description of the request.Now the ticket has been created we will write tests for that. We can add parameters according to the required scenarios. If the method of the request is GET it is ok to send parameters from the Params tab but if the method is POST we should send the parameter in the body.

Test script:

We can write tests in the test tab in Javascript language. The test part executes after the execution of the actual test-case and generation of a response. As we have to script the test according to the response, we should be aware of the response that will come after hitting the send button. We can write multiple assertions in the same test case and the same assertion for multiple test cases. All of them will execute simultaneously and we can see the result in PASS and FAIL format according to the applied assertion.

When we hit a single test case response will come while in case we execute collection there will be no response, the success of the test cases will be decided by the pass and failed cases.
In the postman, the test part runs only when the request is successful. If the response is not coming it means we have not designed our test correctly.

#To assign the value in a variable from the response and set value in a variable and make it dynamic to use anywhere.
var RequestedTransactionID;
pm.environment.set("RequestedTransactionID",jsonData.TransactionID);

Pre-Request script:
Pre-request is that part of the test case which runs before the actual test case. The purpose of writing the prerequisite script is to provide a dynamic nature to the request.

#To hit an API from the prerequisite of one API to another API and picking up elements from the response

var env_url = pm.environment.get("URL");
var account_id = pm.environment.get("AccountID");
var api_key = pm.environment.get("APIKey");
var signature = pm.environment.get("APISignature");

pm.sendRequest({
url: env_url + "/endpoint=" + account_id + " &Key=" + api_key +"&Signature=" + signature+ "&FirstName=George&LastName=Test&PhoneNumber=9876543210&Address1=Address1st&City=Noida&Province=UP&Country=India&PostalCode=201301&AccountNumber=12345&PANNumber=0013256&BranchTransitNumber=412&Amount=1&Currency=INR",
method: "GET",
headers: {},
body: {}
},
function (err, res) {
pm.environment.set("TransactionIDToCancel", res.json().TransactionID);
}
);

Using pre-request we can also modify values of parameters, set variables in the environment, can hit another API before the actual API in which the pre-request is written, declare and assign variables that can accept value from the response.

Dynamic dates:

var moment = require('moment');
pm.environment.set("currentDate", moment().format("YYYY-MM-DD"));

var StartDate = moment().add(1, 'day').format("YYYY-MM-DD");
pm.environment.set("StartDate", StartDate);

Collection Runner:

We can schedule the execution of test cases using the collection runner feature of the postman. Let’s say we need to execute a certain end-point or the whole collection a certain number of times then we can select that collection then the end-point or just select the collection and can run all the endpoints within that collection.

How to execute the test with collection Runner:

On the top right corner of Postman, we have three options- New, Import & Runner.

Select the Runner option from there. Now we have a drop-down to select the collection we want to execute. We can either select any particular endpoint inside the selected folder to execute or we can execute the entire collection.
We can also select work-space, environments, Iterations i.e. the number of times we want to execute the selected test folder, Delay i.e. time interval between the execution of test cases, etc for the same.

After selecting all the things accordingly we are good to hit the start button, Once the button is hit, execution starts in a sequential manner in which test cases are there and after all the execution of all the test cases we can see the counts of Passed and Failed cases and detailed analysis in graphical forms.

Leave a Reply