Part 2: How to write a script / automate APIs in Postman
Here is the second part of API testing using postman series. In this, we will see how to write a script / automate APIs in Postman.
We will use Pre-request Script & test options to automate API.
Pre-request Script: — Code snippet to execute before invoking API.
Tests: — Code snippet to execute after invoking API like JSON success code and APIs speed etc.
The postman has in-built code snippets, select snippets, and click on Send to execute APIs and check the test cases result.
As you can see in above screenshot, the test result is shown in ‘Test Result’ section.
Let’s automate a collection
Click on the arrow next to collection name it will display list all APIs from a collection, click on the ‘Run’ button as shown below
It will open Collection Runner window
Let’s see options available in the Runner.
Environment: it will help target testing on the desired environment, we have already seen how to set environment and variables in the earlier post.
Iteration: to select iteration, specify the number of times you want to run the test.
Delay: delay in execution.
Log response: to log the response of a test.
Data: It allows to test APIs behavior with different values dynamically, for example, let’s say you want to test authentication flow for different users, instead of executing collection separately for each user we can pass dynamic data using this option, It will take data JSON/CSV file.
Here I want to test the user flow for two user having credentials as username“prachi”, password “1234” & username “PPrachi” and password “5678”.
My CSV file looks like this
for iteration 1 username has value “prachi” & password has value “1234”, for iteration 2 key username has value “PPrachi” & password is “5678”.
Click on Run, You can see the all the APIs response, test cases failed / success in the Runner window, along with the time takes to execute each APIs, to check more details just hover on the API.