Postman 邮递员获取当前集合变量值
我需要得到当前确切的集合变量值 在邮递员请求的请求前脚本中,我设置了两个收集变量,如下所示Postman 邮递员获取当前集合变量值,postman,postman-native-app,Postman,Postman Native App,我需要得到当前确切的集合变量值 在邮递员请求的请求前脚本中,我设置了两个收集变量,如下所示 pm.collectionVariables.set("firstCollectionVariable", "string_{{$guid}}"); pm.collectionVariables.set("secondCollectionVariable", "second_variable_{{firstCollectionVaria
pm.collectionVariables.set("firstCollectionVariable", "string_{{$guid}}");
pm.collectionVariables.set("secondCollectionVariable", "second_variable_{{firstCollectionVariable}}");
{
"firstKey": {{firstCollectionVariable}},
"secondKey" : {{secondCollectionVariable}},
}
然后我在post请求主体中使用这两个集合变量来设置特定数据,如下所示
pm.collectionVariables.set("firstCollectionVariable", "string_{{$guid}}");
pm.collectionVariables.set("secondCollectionVariable", "second_variable_{{firstCollectionVariable}}");
{
"firstKey": {{firstCollectionVariable}},
"secondKey" : {{secondCollectionVariable}},
}
firstKey和secondKey按预期设置
firstKey=>“字符串_c6631d2c-2427-4903-b604-8120662a5e0e”
secondKey=>“第二个变量字符串”c6631d2c-2427-4903-b604-8120662a5e0e
问题是当我试图使用
pm.expect(pm.response.secondKey).to.eql(pm.collectionVariables.get("secondCollectionVariable"));
我得到了断言错误
AssertionError:应为
“第二个变量字符串”c6631d2c-2427-4903-b604-8120662a5e0e”至
深度相等的“第二个变量”{{firstCollectionVariable}
如何获取集合变量的当前精确值?可能使用
to.be.deep
可以解决您的问题,请尝试以下方法:
pm.expect(pm.response.secondKey).to.be.deep.equals(pm.collectionVariables.get("secondCollectionVariable"));
或
另一个好方法是使用单引号获取collectionVariable,如'secondCollectionVariable'
我希望这对你有用,祝你好运 将变量转换为字符串也可能解决您的问题。这是否回答了您的问题?