Challenges in Iterative API Testing:
Before the introduction of "pm.execution.skipRequest()" in Postman, managing request execution within iterations presented a challenge. Consider a scenario where you are testing an API that requires sending a request to check its status at the beginning of each iteration. Without the ability to skip subsequent iterations of this status check, the same request would be sent repeatedly, leading to unnecessary overhead and potentially impacting the efficiency of the testing process.
In the absence of a built-in mechanism to conditionally skip requests based on iteration count, testers often had to resort to workarounds such as duplicating collections and manually managing request execution. This not only increased the complexity of test setups but also introduced the risk of inconsistency and errors in test results.
Overall, the lack of a streamlined approach to managing request execution based on iteration conditions hindered the efficiency and effectiveness of API testing workflows in Postman.
Managing Iterations with Conditional Workflows:
However, with the introduction of pm.execution.skipRequest(), Postman users gained the ability to create conditional workflows that significantly improve the efficiency of API testing. By selectively skipping requests based on predefined conditions, such as iteration count or specific data parameters, testers can streamline their testing processes and ensure that each iteration processes correctly.
When running a Postman collection with multiple iterations, we need to manage iteration data and flow efficiently to ensure that each iteration processes correctly, especially when dealing with different sets of data or conditions.
Imagine a scenario where you are testing an API and you only want to send a request when a certain condition is met. For example, you might want to send a successful login request only once in the first iteration and skip this request in subsequent iterations, or send a logout request only once during the last iteration of the collection run. With “pm.execution.skipRequest()”, you can create conditional workflows that drastically improve the efficiency of your API testing.
For example, let’s take a public API for ordering a book by reading data from a CSV file.
Practical Examples:
1) Send Get Request to check the API status, we can run this request in the first iteration to ensure the API status is OK and then skip this request in subsequent iterations. To achieve this we need to add “pm.execution.skipRequest()” to the Pre-request Script tab of the “Check API Status” request.
Note: Iteration always starts from 0
As shown below Console logs snippet Get Status request sent only once in Iteration 0 and skipped from next iteration onwards.
Postman Collection Run Console Logs
2) Next Send Post Request for User Registration by reading data from CSV.
In above Snippet “Name” and “Email” data is required for Register API request and “Customer_Name” data is for next request (for ordering an book)
With above data set if we run collection Register API request is going to run in 4 iterations because total rows in the data set is 4.But Register API request supposed to run only till 3rd iteration as for 4th iteration there is no data to send request ,lets control this by adding “pm.execution.skipRequest()” to the Pre-request Script tab of the “Register API request”.
The condition “if ((!Name && !Email) || (Name.trim() === “” && Email.trim() === “”))” checks if both Name and Email are either not defined or empty strings, including after trimming any whitespace.
As shown in above collection console logs Registration API request ran only in first 3 iterations and from 4th iteration onwards skipped sending request just printed a message “Skipping the Register request” as added on pre-script
3) Next send an Delete request to delete created order. Suppose if we have an requirement like Delete request should run only once and only at last iteration we can handle by adding “pm.execution.skipRequest()” to the Pre-request Script tab of the “Delete API request”.
The condition “if (!(pm.info.iteration === (pm.info.iterationCount — 1)))” is used to check if the current iteration is not the last one in a Postman collection run.
As shown in above collection console logs Delete request sent only once in 4th iteration as it’s the last iteration.
4) Lets take a another scenario where you need to send a Get request only if prior Post Request is successful or Get request should only sent when Post Request Order is created successfully. To achieve this scenario follow the below steps.
Step 1: Create an flag in pre requisite script of Post Request “isOrderCreated” and initialize as False .
Step 2: In tests script of Post Request if request response is successful (if response code is 201) then make “isOrderCreated” as True and save the created order id to collection variable.
In above snippet I'm saving orederId along with iteration Number as i have multiple orderid’s gets create from my request. Below is my variables that added into my collection using above code .
Step 3: Write a conditional statement in Get Request pre requisite script if “isOrderCreated” value is true then only Get request should run else skip the request ,we can achieve this by adding “pm.execution.skipRequest()” to the Pre-request Script tab of the “Get Request”
In the above snippet first getting the "isOrderCreated" flag from collection variables and checking if the value of flag is not true and if condition met then skipping the request.
Conclusion:
In conclusion, the ability to use pre-request scripts in Postman has always been a powerful feature for customizing API requests. Now, with the introduction of pm.execution.skipRequest(), you can take this customization to the next level. This functionality allows you to make dynamic decisions about whether a request should be sent, based on conditions you define.
By integrating pm.execution.skipRequest() into your workflow, you can create more efficient and intelligent test scenarios, ensuring your API testing is both robust and streamlined. Whether you need to check API status only once, manage user registrations, or perform complex logic based on iteration data, this feature provides the flexibility to meet your testing needs.
Start experimenting with pm.execution.skipRequest() today and see how it can enhance your Postman collections, making your API testing smarter and more efficient than ever before.