如何使用正则表达式/ beanshell post 处理器提取器从响应中提取数据
how to use regular expression/ beanshell post processor extractor for extracting data from response
我在视图树监听器中有这个响应
"__v":0,"language":"en","jobTitle":"Executive","firstName":"wer","lastName":"wre","email":"fde+98989@gmail.com","phone":"+1
213-321-3232","_companyId":"5723erera8b2196fdaer9502b86dc7","accountStatus":"UNVERIFIED","role":"ADMIN","emailActivationSecret":"XD1EererreQyVLFn1IjJg7QQNcDz9RLZ1xrKnl84XI5Nb5","_id":"5723a8b21er96fwereeda9ewree502b86dc8","products":[],"created":"2016-04-29T18:32:18.700Z"}
现在,我需要将 email
、email activation secret
和 id
提取到此 link:
webserver-wat:3001/activateAccount?email=fde%298989%40gmail.com&secret=XD1EererreQyVLFn1IjJg7QQNcDz9RLZ1xrKnl84XI5Nb5&companyId=5723a8b21er96fwereeda9ewree502b86dc8
并发送 post 请求以激活 link。有什么办法吗?真的很感激,因为这阻碍了我们的自动化过程
正则表达式:
/.*"email":"(.*?)".*emailActivationSecret":"(.*?)".*"_id":"(.*?)".*/
替换字符串:
"webserver-wat:3001/activateAccount?email=&secret=&companyId="
我相信您使用 JSON Path Extractor (available via JMeter Plugins) 会更容易,因为这样您将能够轻松解析多行数据,使用条件选择器和 JSON 路径表达式看起来更加友好和人类可读性高于正则表达式。
例如:
要提取单个值,即 email
使用以下 JSON 路径提取器配置:
- 目标变量名称:任何有意义的,即
email
- JSON 路径表达式:
$.email
- 在需要时将提取的值引用为
${email}
- 对其他动态变量重复上述步骤(使用单独的 JSON 路径提取器)
如果你想要一次性完成所有内容:
- 目标变量名:任何合适的变量名,即
param
- JSON 路径表达式:
$.[email, emailActivationSecret, _id]
将提取的值引用为:
${param_1}
- 对于 "email"
${param_2}
- 对于 "emailActivationSecret"
${param_3}
- 对于“_id”
参考文献:
- Using the XPath Extractor in JMeter(向下滚动到 "Parsing JSON")
- JSONPath - XPath for JSON
- JSONPath Online Evaluator
我在视图树监听器中有这个响应
"__v":0,"language":"en","jobTitle":"Executive","firstName":"wer","lastName":"wre","email":"fde+98989@gmail.com","phone":"+1 213-321-3232","_companyId":"5723erera8b2196fdaer9502b86dc7","accountStatus":"UNVERIFIED","role":"ADMIN","emailActivationSecret":"XD1EererreQyVLFn1IjJg7QQNcDz9RLZ1xrKnl84XI5Nb5","_id":"5723a8b21er96fwereeda9ewree502b86dc8","products":[],"created":"2016-04-29T18:32:18.700Z"}
现在,我需要将 email
、email activation secret
和 id
提取到此 link:
webserver-wat:3001/activateAccount?email=fde%298989%40gmail.com&secret=XD1EererreQyVLFn1IjJg7QQNcDz9RLZ1xrKnl84XI5Nb5&companyId=5723a8b21er96fwereeda9ewree502b86dc8
并发送 post 请求以激活 link。有什么办法吗?真的很感激,因为这阻碍了我们的自动化过程
正则表达式:
/.*"email":"(.*?)".*emailActivationSecret":"(.*?)".*"_id":"(.*?)".*/
替换字符串:
"webserver-wat:3001/activateAccount?email=&secret=&companyId="
我相信您使用 JSON Path Extractor (available via JMeter Plugins) 会更容易,因为这样您将能够轻松解析多行数据,使用条件选择器和 JSON 路径表达式看起来更加友好和人类可读性高于正则表达式。
例如:
要提取单个值,即
email
使用以下 JSON 路径提取器配置:- 目标变量名称:任何有意义的,即
email
- JSON 路径表达式:
$.email
- 在需要时将提取的值引用为
${email}
- 对其他动态变量重复上述步骤(使用单独的 JSON 路径提取器)
- 目标变量名称:任何有意义的,即
如果你想要一次性完成所有内容:
- 目标变量名:任何合适的变量名,即
param
- JSON 路径表达式:
$.[email, emailActivationSecret, _id]
将提取的值引用为:
${param_1}
- 对于 "email"${param_2}
- 对于 "emailActivationSecret"${param_3}
- 对于“_id”
- 目标变量名:任何合适的变量名,即
参考文献:
- Using the XPath Extractor in JMeter(向下滚动到 "Parsing JSON")
- JSONPath - XPath for JSON
- JSONPath Online Evaluator