【问题标题】:Iteration through RestAPI POST calls遍历 Rest API POST 调用
【发布时间】:2019-06-16 22:41:52
【问题描述】:

我正在使用用于创建和测试虚拟机的私有云平台。他们有丰富的 API,可以让我创建虚拟机:

{
  "name": "WIN2016-01",
  "description": "This is a new VM",
  "vcpus": 4,
  "memory": 2147483648,
  "templateUuid": "sdsdd66-368c-4663-82b5-dhsg7739smm",
...
}

我需要通过简单地迭代 -01 部分来自动化这个创建机器的过程,所以它变成:

  • “名称”:“WIN2016-01”,
  • “名称”:“WIN2016-02”,
  • “名称”:“WIN2016-03”

我尝试使用 Postman Runner 并构建工作流 https://learning.getpostman.com/docs/postman/collection_runs/building_workflows/ 但没有运气 - 不确定我需要在“测试”选项卡中使用什么语法。

【问题讨论】:

  • 您可以在“预请求脚本”选项卡中设置变量,然后在请求本身上使用它们以使它们动态化。因此,您可以在每次请求后增加一个数字,然后将其转换为字符串(例如 2 将变为“02”),并将其附加到您的请求正文中。
  • 你认为我可以提供一些关于这件事的文档吗?我知道如何使用预请求脚本,但不确定如何实现这个特定的解决方案。

标签: rest api automation postman


【解决方案1】:

这是一种方法。

创建一个集合和您的 POST 请求。

在您的 pre-request 中,添加以下内容:

/* As this will be run through the Collection Runner, this extracts 
the number of the current iteration. We're adding +1, as the iteration starts from 0.*/ 

let count = Number(pm.info.iteration) + 1;

//Convert the current iteration number, to a '00' number format (will be a string)

let countString = ((count) < 10) ? '0' + count.toString() : 
count.toString();

//Set an environment variable, which can be used anywhere

pm.environment.set("countString", countString)

在您的 POST 请求正文中,执行以下操作:

{
   "name": "WIN2016-{{countString}}",
   ...
}

现在,通过“Collection Runner”运行您的集合,并输入 Iterations 的次数(例如,您希望您的集合运行多少次)。如果您的 API 施加了速率限制,您还可以添加 延迟

最后,点击运行

【讨论】:

    猜你喜欢
    • 2020-12-02
    • 1970-01-01
    • 1970-01-01
    • 2016-01-08
    • 1970-01-01
    • 1970-01-01
    • 2020-11-06
    • 2018-08-29
    • 2017-04-25
    相关资源
    最近更新 更多