【问题标题】:Postman environment variable returning nullPostman 环境变量返回 null
【发布时间】:2018-09-30 08:25:33
【问题描述】:

我想将有效负载模式添加到环境变量,以便我可以根据该模式验证响应有效负载。

我的环境变量定义如下:

responseSchema: 
{ "properties":
      "firstname":
         {"type":"string" },
       "lastname": 
          {"type":"string"},
       "phonenumber": 
          {"type":"integer","format":"int64"}
       },
   "required":["firstname", "lastname", "phonenumber"]}

但是,我无法在我的 Postman 测试代码中访问此环境变量。我尝试通过以下方式访问它:

environment.responseSchema

但是,这将返回 null。如何访问使用邮递员创建的环境变量。我实现这一点的方式与http://blog.getpostman.com/2017/07/28/api-testing-tips-from-a-postman-professional/ TIP #4:JSON Schema 验证一致

【问题讨论】:

  • 你是怎么设置环境变量的?
  • 好问题@AndrewLohr - 我使用了“EDIT COLLECTION”选项并通过其键和值添加了变量。

标签: javascript environment-variables postman web-api-testing


【解决方案1】:

所以要清楚,您添加的是集合变量,而不是环境变量。 More on Postman variables

要访问您的集合变量,您可以在测试脚本选项卡中执行 pm.variables.get("responseSchema")

为了更完整,你也应该解析它。

var mySchema = JSON.parse(pm.variables.get("responseSchema"));
console.log(mySchema.properties.firstname.type);

另外我相信你的对象是无效的,你可能打算这样做

{
    "properties": {
        "firstname": {
            "type": "string"
        },
        "lastname": {
            "type": "string"
        },
        "phonenumber": {
            "type": "integer",
            "format": "int64"
        }
    },
    "required": ["firstname", "lastname", "phonenumber"]
}

【讨论】:

    猜你喜欢
    • 2020-01-02
    • 2016-11-11
    • 2021-05-14
    • 2018-11-14
    • 2020-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-06
    相关资源
    最近更新 更多