As a debugging tool interfaces, iterative update Postman speed quickly, constantly adding a lot of new features. Api make the design, testing, monitoring, Mock, and team collaboration easier.
Modify the execution order
In the case of encounters with an interface dependent, we often need an interface in order to perform, before can be achieved by using pm.sendRequest sent depends Pre-request Scripts request in a request, a new version of Postman we can in Run Collection the execution order of the modification request, as follows:
where all the interfaces into a collection request set, the "Get Baidu token" Tests script this request, the access_token stored as a global variable, for "Baidu ORC" use .
we can directly drag, change the default Collection of execution order, you can choose which requests are not run.
Workflow Workflow
In addition to adjusting the order when Run Collection, we can also postman.setNextRequest("同Collection下保存的请求名");
jump to manually control request.
Sequence Structure
Below, a new Collection, add A, B, C, D, E, F 5 requests, added in Tests A script requests
postman.setNextRequest("D");
In the lower D A executes the complete execution request
from the figure we can see that in fact the request is skipped B, C, D jump directly to the request.
Note: If the request is unchecked D at run time, only run A request
Branch structure
Normally, we can use to control the flow goes Analyzing if, for example, by determining the success of the request A to D, respectively jump request or a request B.
var jsonData = pm.response.json()
if(jsonData.args.a == 'a'){
postman.setNextRequest("D");
}
else{
postman.setNextRequest("B");
}
Loop structure
We can use the last request postman.setNextRequest (), prior to a request to complete a cycle, such as a request using the F postman.setNextRequest ( "A"), will execute the infinite loop continues, as shown in FIG. :
In general, the loop must be set abort condition, typically by determining whether if postman.setNextRequest () controls whether to continue the cycle.
Example: Asynchronous Interface Poll
We can use a postman.setNextRequest request to itself to complete polling of the asynchronous interface.
- Create a Collection, a new "create order Interface", the request is:
http://115.28.108.130:5000/api/order/create/?
user_id=123&goods_id=123&num=2&amount=20.0
This is an asynchronous interface, the interface will return a query for orders order_id our results in Tests, the acquired order_id saved as global variables for the next interface, as shown below:
var jsonData = pm.response.json()
pm.globals.set("order_id", jsonData.order_id);
- Create a new "get results Order" interface to request to:
http://115.28.108.130:5000/api/order/get_result/?order_id={{order_id}}
This is an interface to obtain orders, orders not sure how long you've created, if the order is returned in the creation process "{}", if you create a successful return:
{
"amount": "20.0",
"goods_id": "123",
"num": "2",
"user_id": "123"
}
We set the polling conditions Tests script, the next request to itself as follows:
if(pm.response.text() == "{}\n"){
postman.setNextRequest("获取订单结果");
}
Run Collection we found that the "get results Order" request Quick request until it stops, in fact, we also need to set a polling interval, we will postman.setNextRequest into a kind of function, then setTimeout to delay some time to run.
function loop(){
postman.setNextRequest("获取订单结果")
}
if(pm.response.text() == "{}\n"){
setTimeout(loop, 5000); // 设置间隔5s
}
Below, "Getting results Order" request 5 will perform a wonderful, knowing that the order is processed.
note:
- Generally, we have to set the maximum number of polls or maximum polling time, to prevent the interface always returns "{} \ n", leading to polling can not be aborted (increment can be accomplished by using an integer variable).
- In polling done can also add assert (writes else in)