[英]Loop sample with Regular Expression Extractor result in JMeter
I have 4 samples: 我有4个样本:
First one has a response similar to the XML: 第一个响应类似于XML:
<userId>9709262083</userId>
<name>Tom</name>
<relatedTo>9709262080</relatedTo>
<userId>9709262084</userId>
<name>John</name>
<relatedTo>9709262080</relatedTo>
<userId>9709262085</userId>
<name>Michael</name>
<relatedTo>9709262080</relatedTo>
I created special Regular expression extractor to fetch all userIds (-1 as a Match no.) and fetching work correctly. 我创建了特殊的正则表达式提取器来获取所有userIds(-1作为匹配号)并正确地获取工作。
And I have 3 exact same samples with only one difference in one parameter ( ${USER_ID_1}
, ${USER_ID_2}
and ${USER_ID_3}
), but I would like to make my solution more generic and work even if I'll don't know the number of users will be in response. 我有3个完全相同的样本,一个参数只有一个差异(
${USER_ID_1}
, ${USER_ID_2}
和${USER_ID_3}
),但我想让我的解决方案更通用,即使我不会做知道用户的数量将作出回应。
How can I replace these 3 samples with 1 sample in some kind of loop for each element found in Regular Expression Extractor (without knowing an exact number of users). 如何在正则表达式提取器中找到的每个元素的某种循环中用1个样本替换这3个样本(不知道确切的用户数)。
(I'm using JMeter 3.2, I'm able to bump it to 5.1) (我正在使用JMeter 3.2,我可以将它提升到5.1)
Update: 更新:
How about another way, instead of splitting them I would like to use them as a single variable inside sampler. 如何以另一种方式,而不是分裂它们我想将它们用作采样器内的单个变量。
//e.g. 3 users
"<User><userId>9709262083</userId></User> <User><userId>9709262084</userId></User> <User><userId>97092620835</userId></User>"
//e.g. 2 users
"<User><userId>9709262083</userId></User> <User><userId>9709262084</userId></User>"
The ideal solution would be to create a temp variable and in each step of the for each, I would just add to that variable pattern with my value. 理想的解决方案是创建一个临时变量,并在每个步骤的每个步骤中,我只是添加我的值的变量模式。
${myVariable} += "<User><userId>${userId}</userId></User>"
and then I can jsut use ${myVariable}
然后我可以
${myVariable}
使用${myVariable}
EDIT 编辑
Your new requirement ${myVariable} += "<User><userId>${userId}</userId></User>"
您的新要求
${myVariable} += "<User><userId>${userId}</userId></User>"
Can be achieved by using JSR223 Sampler with the following line 可以通过使用JSR223 Sampler和以下行来实现
vars.put("myVariable", vars.get("myVariable") + "<User><userId>"+ vars.get("userId") + "</userId></User>");
You should use the ForEach controller .example: 您应该使用ForEach控制器 .example:
following variables should have been defined:
应该定义以下变量:
inputVar_1 = wendy
inputVar_1 = wendy
inputVar_2 = charles
inputVar_2 =查尔斯
inputVar_3 = peter
inputVar_3 =彼得
inputVar_4 = john
inputVar_4 =约翰
When the return variable is given as "returnVar", the collection of samplers and controllers under the ForEach controller will be executed 4 consecutive times
当返回变量为“returnVar”时,ForEach控制器下的采样器和控制器的集合将连续执行4次
Use ForEach_Controller to achieve this. 使用ForEach_Controller实现此目的。
Practical example shown below: 实例如下所示:
Test Plan
looks like this: Test Plan
如下所示: Regular Expression Extractor
Regular Expression Extractor
提取用户ID For Each Controller
to iterate over each value For Each Controller
迭代每个值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.