【问题标题】:Postman - Selective run of API Requests within the same IterationPostman - 在同一迭代中选择性地运行 API 请求
【发布时间】:2019-12-11 11:17:09
【问题描述】:

我有一组 API 请求(保存在同一文件夹下)。我需要根据 Runner 中指定的迭代次数执行这些多次。 但是有一个(第一个)请求在整个运行过程中只需要执行一次。这个请求就是收集auth-token的认证请求。

即我有 Req1Req2Req3Req4,保存在同一个集合/文件夹下。我需要运行这组 100 次迭代。但是 Req1 应该只运行一次,而 Req2Req3Req4 应该全部执行 100 次。

有没有办法告诉 Postman(或以其他方式设置)在 整个运行开始时只执行 Req1 一次 >?

【问题讨论】:

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


    【解决方案1】:

    Postman 具有构建工作流功能,您可以在其中指定接下来要调用的请求。

    到达 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(Post Request),在集合运行器中拥有 only 1 iteration。我们使用全局/环境变量进行迭代。

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

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

    【讨论】:

      猜你喜欢
      • 2022-01-02
      • 2019-11-12
      • 2020-05-02
      • 1970-01-01
      • 1970-01-01
      • 2022-07-04
      • 2019-04-02
      • 2019-07-29
      • 2017-05-02
      相关资源
      最近更新 更多