Through this post we are going to explore one of the easiest ways to extend Postman functionalities by making your API testing easier and fun. The idea is running a basic Express server locally, that responds to Postman test scripts requests.
This idea allows you to:
- Work around limitations, or missing features of Postman.
- Avoid some repetitive tasks, thus saving time.
- Have fun while testing APIs.
Some of the prerequisites to implement this, are :
- Create a folder with any name you want. Navigate into it in the terminal, and run
npm init, hit Enter each step of the way, to keep the default choices. A
package.jsonshould be created in your repository.
- In the same directory run :
npm install express openthis will install express , and another package we will use afterwards.
To briefly provide the context of this example. While testing an API’s endpoint, we generate loan contracts PDF files encoded in Base64. While the number of use cases increases, so does these repetitive tasks :
- Copy the long Base64 text from postman
- Paste it in a text editor
- Use a plugin to decode it from Base64
- Save it in PDF format
- Open the file and verify the data
Assuming your API returns a response similar to :
- Create a script.js file with the following content :
2. Run the command
node script.js, which starts a local server listening on port:3000 with a
POST /saveContractroute. This decodes the file from Base64 and saves it in PDF format on your local disk.
3. Go back to Postman, in your request’s test script. Send a request to the running local server using the
pm.sendRequest()method like shown in the code snippet below :
This project’s entry point is a session initialized from an REST API endpoint. Basically the API returns a url embedding a JWT token. So each time I had to copy/paste the url and open it in the browser, which was a tedious repetitive task I had to get rid off. Plus, the numerous requests from colleagues, and the client-side asking me to generate those recurring urls for different scenarios.
The idea is simple, use the open package to open the url in the default browser. Supposing your API returns a payload that contains a
redirectionUrlfield among other data, same as below :
In the script.js created before, add a GET route that fetches the query parameter and opens the link in the default browser. Below a code snippet demonstrating how to :
Run the command
node script.js in the Terminal to start the server 💡
Similarly to the first example, in the request’s test script call the created GET route :
As an agile tester, it is important to continuously improve practices for an efficient delivery. The demonstrated method saves me a lot of time and effort while testing APIs for critical projects.
Please note that the demonstrated examples are simple, it can be considered as a first pillar to automate much more tasks and help you in the future. Especially with a simple and intuitive framework like Express and Postman.