As Postman is one of the most used API development environment, we use it in many projects to create, test and document our API’s but there is something called Workflow.
When we create a bunch of requests to represent an API, we group them up in folders depending on the functionality (login,…) so if we want to launch a test, out of the box we can only run a single request, all the requests in a folder or all the request in your collection, but this is not always the way we wanted to be.
For example, if I have to test the new login module, the user must try register first, validate it and then log in, but these force me to use all the requests from different folders and tests may end with a logout request and tests may necessarily fail.
Workflow - Can be used when we need to Execute Requests in a way as needed! To accomplish this Postman offers a feature i.e., to code in Javascript for your test, also they even offer you an API with some useful functionalities.
Here is the code you should add to all your requests in Tests
postman.setNextRequest(Request_Name)
When added, we can set the next request to be executed, and in case we don’t send anything, it would ignore it and go to the next request (in the order they were written).
Ensure the last request has - postman.setNextRequest(null) where you want to stop your test to avoid iterations with an infinite loop and done! Now, we can have a custom workflow, where we can version it and share it so anyone can use it.
Here, we've our API Request Collection in below order - GET,POST,DELETE,UPDATE,TESTUser,CreateStatus
But, when written with setNextRequest(), the Collection executes in the below format - Note: We can also add JSCode in the Pre-request Script.
Troubleshooting APIs - Debug and Logging in Postman
We may have many possibilities for our APIs requests NOT behaving as expected and showing up a message
Could not send the request. Error: connect ECONNREFUSED
By navigating to View in Console, we can see the request in detail and find out what went wrong! Generally, these errors may encounter due to - Invalid Request URL or Protocol or Responses
We can also track the logs by adding below in Tests or Pre-Request Script tab.
console.log()
console.info()
console.warn()
console.error()
Let's write some Scripts in Postman
Postman is purely based on Node.js by which we can add our JavaScript code to execute under Pre-Request Script and Tests Tab
In Pre-request Script tab, we write scripts which are sent to the server before a request.
In Tests tab, we write scripts to perform assertions, etc validations for the response received.
Order of the scripts -
We can write below JS snippets in the Pre-Request Scripts tab while sending any requests
pm.environment.get("variable_key"); -- Extract Environment Variable
pm.globals.get("variable_key"); -- Extract Global Variable
pm.variables.get("variable_key"); -- Extract Variable
pm.environment.set("variable_key", "variable_value"); -- Set Environment Variable
pm.globals.set("variable_key", "variable_value"); -- Set Environment Variable
pm.environment.unset("variable_key"); -- Unassign Environment Variable
pm.globals.unset("variable_key"); -- Unassign Global Variable
pm.sendRequest("https://postman-echo.com/get", function (err, response) {
console.log(response.json());
}); -- Sending a request
We can write below JS snippets in the Tests tab to perform assertions on Status Code or the JSON response
pm.environment.unset("variable_key"); -- Unassign Environment Variable
pm.globals.unset("variable_key"); -- Unassign Global Variable
pm.sendRequest("https://postman-echo.com/get", function (err, response) {
console.log(response.json());
}); -- Sending a Request
pm.test("Status code is 200", function ()
{
pm.response.to.have.status(200);
}); -- Assertion with respective to a Status code
pm.test("Body matches string", function () {
pm.expect(pm.response.text()).to.include("string_you_want_to_search");
}); -- Assertion with respective to a JSON String response
pm.test("Your test name", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.value).to.eql(100);
}); -- Assertion with respective to a JSON response
pm.test("Body is correct", function () {
pm.response.to.have.body("response_body_string");
}); -- Assertion with respective to a String response
pm.test("Content-Type is present", function () {
pm.response.to.have.header("Content-Type");
}); -- Assertion with respective to a Content-Type
pm.test("Response time is less than 200ms", function () {
pm.expect(pm.response.responseTime).to.be.below(200);
}); -- Assertion with respective to a response time
pm.test("Successful POST request", function () {
pm.expect(pm.response.code).to.be.oneOf([201, 202]);
}); -- Assertion for a POST request
Note : We can have Pre-Request Scripts and Tests for a request, folder or a collection level
References: learning.postman.com/docs/writing-scripts/s..
In Part - 3 we will be checking on the Importing Data Files and usage of Newman with Postman for CI/CD and Reporting!
Thank You, for reading. :) Happy Learning!