邮递员-在同一迭代中选择性运行API请求

时间:2019-08-02 20:27:53

标签: postman postman-collection-runner postman-pre-request-script

我有一组API请求(保存在同一文件夹下)。我需要根据Runner中指定的迭代次数执行多次。 但是有一个(第一个)请求,整个运行只需要执行一次。此请求是收集身份验证令牌的身份验证请求。

即我有 Req1 Req2 Req3 Req4 ,它们保存在同一收集/文件夹下。我将需要运行此集合100次迭代。但是 Req1 应该只运行一次,而 Req2 Req3 Req4 应该全部执行100次。

是否有一种方法可以告诉邮递员(或以其他方式设置)在全面运行开始时仅一次执行 Req1 >?

1 个答案:

答案 0 :(得分:2)

邮递员具有构建工作流程功能,您可以在其中指定下一个要呼叫的请求。

到达Req4后,调用Req2,它基于计数器在Req1之后。 这可以在邮递员请求窗口的Tests标签中实现。

Pseudo code - 
set 2 global/environment variables , iteration = <some number you need>, iteration_ref = 0
<In Req1 window>
if(pm.globals.get("iteration_ref") < pm.globals.get("iteration")-1)
     postman.setNextRequest('Req2')

<In Req2 window>
if(pm.globals.get("iteration_ref") < pm.globals.get("iteration")-1)
     postman.setNextRequest('Req3')

<In Req3 window>
if(pm.globals.get("iteration_ref") < pm.globals.get("iteration")-1)
     postman.setNextRequest('Req4')

<In Req4 window>
if(pm.globals.get("iteration_ref") < pm.globals.get("iteration")-1)
{
     postman.setGlobalVariable("iteration_ref", 
     Number(postman.getGlobalVariable("iteration_ref"))+1);
     postman.setNextRequest('Req2')
}

或者仅在最后一个请求中,如果您对集合中设置的请求顺序很有信心。

<In Req4 window>
if(pm.globals.get("iteration_ref") < pm.globals.get("iteration")-1)
{
     postman.setGlobalVariable("iteration_ref", 
     Number(postman.getGlobalVariable("iteration_ref"))+1);
     postman.setNextRequest('Req2')
}

请确保您首先在集合中拥有Req1(发布请求),在集合运行者中拥有only 1 iteration。我们正在使用全局变量/环境变量进行迭代。

PS:我强烈建议您使用请求库来调用API的简单python或js脚本,以上流程是一个机智的技巧。

参考-https://learning.getpostman.com/docs/postman/collection_runs/building_workflows/