如何使用 bean-shell 预处理器将从正则表达式提取器获得的多个提取值用于我的下一个 http 请求?
How to use bean-shell pre-processor to use multiple extracted values obtained from regex extractor into my next http request ?
我试图访问 Myntra 的主页并使用 JMeter 在搜索框中搜索 puma。使用正则表达式提取器,我从响应中提取了几个值,如下所示:
JMeterVariables:
JMeterThread.last_sample_ok=true
JMeterThread.pack=org.apache.jmeter.threads.SamplePackage@1589f854
START.HMS=122825
START.MS=1532069905949
START.YMD=20180720
TESTSTART.MS=1532073140645
__jmeter.USER_TOKEN__=Thread Group 1-1
outValue=puma?f=gender:men::categories:Tshirts
value_1=puma?f=gender:men::categories:Tshirts
value_1_g=1
value_1_g0="value":"puma?f=gender:men::categories:Tshirts"
value_1_g1=puma?f=gender:men::categories:Tshirts
value_2=puma?f=gender:men::categories:Casual Shoes
value_2_g=1
value_2_g0="value":"puma?f=gender:men::categories:Casual Shoes"
value_2_g1=puma?f=gender:men::categories:Casual Shoes
value_3=puma?f=gender:men::categories:Sports Shoes
value_3_g=1
value_3_g0="value":"puma?f=gender:men::categories:Sports Shoes"
value_3_g1=puma?f=gender:men::categories:Sports Shoes
value_matchNr=3
现在使用 For Each Controller,我可以将这些值传递到我的下一个 HTTP 请求并迭代它们一次,如下所示:
但是我想使用 BeanShell 预处理器做同样的事情并且我是脚本编写的新手,所以我需要帮助了解如何使用 BeanShell 预处理器做同样的事情并将值传递给我的下一个 HTTP 请求。
欢迎提出建议。
使用value_matchNr
找出您有多少个变量实例。然后循环:构建一个合适的变量名,并使用 vars.get(name)
:
获取它的值
// First, use the value of 'value_matchNr' to identify how many variables of type 'value_...' we have
int count = 0;
try {
count = Integer.parseInt(vars.get("value_matchNr"));
} catch(NumberFormatException e) { log.error("Variable 'value_matchNr' was not found. There won't be any looping"); }
// Next, loop through variables (if there's at least 1 to loop through)
for(int i = 1; i <= count; i++) {
String name = "value_" + i; // build variable name, e.g. value_1, value_2, etc
String value = vars.get(name); // get variable value
// at this point you can do whatever you want with the value. For example print it out:
log.info("Variable '" + name + "' has value '" + value + "'");
}
我试图访问 Myntra 的主页并使用 JMeter 在搜索框中搜索 puma。使用正则表达式提取器,我从响应中提取了几个值,如下所示:
JMeterVariables:
JMeterThread.last_sample_ok=true
JMeterThread.pack=org.apache.jmeter.threads.SamplePackage@1589f854
START.HMS=122825
START.MS=1532069905949
START.YMD=20180720
TESTSTART.MS=1532073140645
__jmeter.USER_TOKEN__=Thread Group 1-1
outValue=puma?f=gender:men::categories:Tshirts
value_1=puma?f=gender:men::categories:Tshirts
value_1_g=1
value_1_g0="value":"puma?f=gender:men::categories:Tshirts"
value_1_g1=puma?f=gender:men::categories:Tshirts
value_2=puma?f=gender:men::categories:Casual Shoes
value_2_g=1
value_2_g0="value":"puma?f=gender:men::categories:Casual Shoes"
value_2_g1=puma?f=gender:men::categories:Casual Shoes
value_3=puma?f=gender:men::categories:Sports Shoes
value_3_g=1
value_3_g0="value":"puma?f=gender:men::categories:Sports Shoes"
value_3_g1=puma?f=gender:men::categories:Sports Shoes
value_matchNr=3
现在使用 For Each Controller,我可以将这些值传递到我的下一个 HTTP 请求并迭代它们一次,如下所示:
但是我想使用 BeanShell 预处理器做同样的事情并且我是脚本编写的新手,所以我需要帮助了解如何使用 BeanShell 预处理器做同样的事情并将值传递给我的下一个 HTTP 请求。
欢迎提出建议。
使用value_matchNr
找出您有多少个变量实例。然后循环:构建一个合适的变量名,并使用 vars.get(name)
:
// First, use the value of 'value_matchNr' to identify how many variables of type 'value_...' we have
int count = 0;
try {
count = Integer.parseInt(vars.get("value_matchNr"));
} catch(NumberFormatException e) { log.error("Variable 'value_matchNr' was not found. There won't be any looping"); }
// Next, loop through variables (if there's at least 1 to loop through)
for(int i = 1; i <= count; i++) {
String name = "value_" + i; // build variable name, e.g. value_1, value_2, etc
String value = vars.get(name); // get variable value
// at this point you can do whatever you want with the value. For example print it out:
log.info("Variable '" + name + "' has value '" + value + "'");
}