30 Jan 2022

Automation Test & Automate workflow with Postman Collection Runner

Automation Test & Automate workflow with Postman Collection Runner

Postman Automation: Tests and Workflows with Postman Collection Runner

Follow @JohnOnSoftware

Postman collection is used pretty frequently to quick check with Restful API, and we also provide some Postman collections for you to do a quick check, please see the following links for these available postman collection that you can use:

 

Postman Collection Runner Automation

We can do more with Postman, it provides Collection Runner tool to help run these collection together as a series of requests, against a corresponding environment. You can use scripts to build integration test suites, pass data between API requests, and build workflows that mirror your actual use case of APIs. Details please check here.

 

How to Automate in Postman

Firstly, I have my BIM 360 Cost Management Step-by-Step tutorial Postman collection here, you can manually use the collection to simulate the workflow, you can check the details in Github repo, or watch the following video.

Second, I just added some script in Tests tab as follow, and run the collection together with the environment using the Postman Collection Runner tool, the tool will run these Request in sequence, and you can see the test result in the following video.  

pm.test("Status code is 200", function () { 
    pm.response.to.have.status(200); 
});

Until now, you can create only one type of change order which is PCO, and execute one action on it, how about I want to create all types of change orders, and perform all the action on them? You can achieve this by using the method postman.setNextRequest(''). Let me explain it here:

1. In "Step 11: Execute an action on change order", always go to Step 10 to find more actions.

Tests:

postman.setNextRequest('Step 10: Get all avialable actions for the change order');

2. In "Step 10: Get all available actions for the change order", check if current available actions are all done for this change order, if done, go to "Step 7: Find change order types" to create another type of change order, if not, set the "action_name" and go to next step to perform the action.

Tests:

var responseBody = JSON.parse(responseBody);

// execute the action on the change order, if the action is done for this change order, go back to step 7 to start a new change order.
if(responseBody.length === 0 || responseBody[0].name == "reject" || responseBody[0].name == "acceptedToSubmit" || responseBody[0].name == "forceResubmit"  ){
    postman.setNextRequest('Step 7: Find change order types');
}else{
    postman.setEnvironmentVariable("action_name", responseBody[0].name);
}

3. In "Step 7: Find change order types", in Pre-request Script, check "changeOrder_index", set it as 0 if not exist, otherwise always increase it as index+1, in Tests script, set "changeOrder_type" in sequence by the "changeOrder_index", and if "changeOrder_index" is out of range, stop the process:

Pre-request Script:

var index = pm.environment.get("changeOrder_index");
if( index != null ){
    console.log("current change order index is: " + (index+1));
    pm.environment.set("changeOrder_index", index+1);
}else{
    console.log("current change order index is not set");
    pm.environment.set("changeOrder_index", 0);
}

Tests:

var responseBody = JSON.parse(responseBody);

var index = pm.environment.get("changeOrder_index");
// stop postman collection runner while index is out of range
if( index >= responseBody.length ){
    postman.setNextRequest('Stop the collection');
}else{
    postman.setEnvironmentVariable("changeOrder_type", responseBody[index].type);
}

Now you can see all the different change order types are created, and all the available actions are executed on that. Check the video:

Download all the resource here to play with it yourself, hope you like it;)

Related Article