【问题标题】:Postman Get Current Collection Variable ValuePostman 获取当前集合变量值
【发布时间】:2020-11-18 11:42:42
【问题描述】:

我需要获取当前确切的集合变量值。

在邮递员请求的预请求脚本中,我正在设置 2 个集合变量,如下所示

pm.collectionVariables.set("firstCollectionVariable", "string_{{$guid}}");
pm.collectionVariables.set("secondCollectionVariable", "second_variable_{{firstCollectionVariable}}");

然后我在 post 请求正文中使用这 2 个集合变量来设置特定数据,如下所示

 { 
    "firstKey": {{firstCollectionVariable}},
    "secondKey" : {{secondCollectionVariable}},
 }

firstKey 和 secondKey 按预期设置

firstKey => "string_c6631d2c-2427-4903-b604-8120662a5e0e"

secondKey => "second_variable_string_c6631d2c-2427-4903-b604-8120662a5e0e"

问题是当我尝试使用检查响应时

pm.expect(pm.response.secondKey).to.eql(pm.collectionVariables.get("secondCollectionVariable"));

我得到了断言错误

AssertionError:预期 'second_variable_string_c6631d2c-2427-4903-b604-8120662a5e0e' 到 深度相等'second_variable_{{firstCollectionVariable}}'

如何获取集合变量的当前准确值?

【问题讨论】:

标签: postman postman-native-app


【解决方案1】:

也许使用to.be.deep 可以解决您的问题,尝试类似:

pm.expect(pm.response.secondKey).to.be.deep.equals(pm.collectionVariables.get("secondCollectionVariable"));

pm.expect(pm.response.secondKey).to.be.deep.equal(pm.collectionVariables.get("secondCollectionVariable"));

另一个不错的方法是使用单引号来获取您的 collectionVariable,例如 'secondCollectionVariable'

我希望这对你有用,最好的问候!

【讨论】:

    【解决方案2】:

    将变量转换为字符串也可能解决您的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-14
      • 2019-01-27
      • 2020-02-07
      • 2018-05-20
      • 1970-01-01
      相关资源
      最近更新 更多