[英]Postman - Use part of the response data from one test in another test
I require help to execute a postman test which requires a response output from another test. 我需要帮助来执行邮递员测试,该测试需要来自另一个测试的响应输出。 I have checked with various forums but a solution is not available for this particular scenario.
我已经检查了各种论坛,但是没有针对此特定方案的解决方案。
Example 例
Test 1 response: 测试1响应:
{
"items": [
{
"email": "archer+qa01@gmail.com",
"DocumentName": "tc",
"type": "URL",
"url": "https://localhost:8443/user/terms?statusno=a5f2-eq2wd3ee45rrr"
}
]
}
Test 2: 测试2:
I need to use only the a5f2-eq2wd3ee45rrr
part of the response data from Test 1
, this can be seen in the url
value above. 我只需要使用
Test 1
的响应数据的a5f2-eq2wd3ee45rrr
部分,这可以在上面的url
值中看到。 I need to use this value within Test 2
我需要在
Test 2
使用此值
How can I make this work with Postman? 我怎样才能与Postman合作?
Not completely sure what the response data format is from the question but if it's a simple object with just the url
property, you could use something simple like this: 不完全确定问题的响应数据格式是什么,但如果它是一个只有
url
属性的简单对象,你可以使用这样简单的东西:
var str = pm.response.json().url
pm.environment.set('value', str.split('=', 2)[1])
This will then set the value you need to a variable, for you to use in the next request using with the {{value}}
syntax in a POST request body or by using pm.environment.get('value')
in one of the test scripts. 然后,这将为变量设置所需的值,以便在下一个请求中使用POST请求正文中的
{{value}}
语法或在其中一个中使用pm.environment.get('value')
测试脚本。
Edit: 编辑:
If the url
property is in an array, you could loop through these and extract the value that way. 如果
url
属性在数组中,您可以遍历这些并以这种方式提取值。 This would set the variable but if you have more than 1 url
property in the array it would set the last one it found. 这将设置变量,但如果您在数组中有多个
url
属性,它将设置它找到的最后一个属性。
_.each(pm.response.json(), (arrItem) => {
pm.environment.set('value', arrItem[0].url.split('=', 2)[1])
})
If you get JSON response and then send JSON in body, I would do the following: 如果你得到JSON响应,然后在体内发送JSON,我会做以下事情:
1) In test script(javascript): 1)在测试脚本(javascript)中:
var JsonBody = pm.response.json();
var strToParse = JsonBody.url;
var value = strToParse.slice(indexOf("?status=")+"?status=".length);//parse string
//manually but you can google for a better solutions.
pm.environment.get("varName" , value)
2) You can use it! 2)你可以使用它! In scripts like:
pm.environment.get("varName")
, and everywhere else using {{varName}}
在诸如:
pm.environment.get("varName")
类的脚本中,以及使用{{varName}}
其他任何地方
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.