简体   繁体   English

使用正则表达式提取器循环样本导致JMeter

[英]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: 实例如下所示:

  1. Test Plan looks like this: Test Plan如下所示:

在此输入图像描述

  1. Extract User ID using Regular Expression Extractor 使用Regular Expression Extractor提取用户ID

在此输入图像描述

  1. Use For Each Controller to iterate over each value 使用For Each Controller迭代每个值

在此输入图像描述

  1. Use value from For Each Controller in your Request as you want 根据需要使用Request中For Each Controller的值

在此输入图像描述

  1. Proof of its working 它的工作证明

在此输入图像描述

  1. It's better to use XPath Extractor which allows using XPath expressions for fetching data from XML/XHTML/HTML response types 最好使用XPath Extractor ,允许使用XPath表达式从XML / XHTML / HTML响应类型中获取数据
  2. You can loop the variables using ForEach Controller 您可以使用ForEach Controller循环变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JMeter:如何为此请求URL编写正则表达式提取器 - JMeter: How to write regular expression extractor for this request URL 无法在JMeter中使用正则表达式提取器捕获字母数字数据 - Unable to capture alpha numeric data using regular expression extractor in JMeter 使用正则表达式提取器从 jmeter 中的 url 中提取值 - Use Regular Expression extractor to extract values from url in jmeter 提取器表达式JMeter - Extractor expression JMeter jmeter如何使用正则表达式提取器在从jmeter响应中提取的日期加上+4? - jmeter how to add +4 to the date extracted from the jmeter response using regular expression extractor? Jmeter输出的正则表达式 - Jmeter Regular expression for an output Jmeter中动态字符串的正则表达式是什么? - What is the regular expression in Jmeter for a dynamic string? 除了使用正则表达式提取器之外,如何通过一种简单的方法有效地从响应中提取多个“视图状态”(将近50个) - How to Extract Multiple “View State” (almost 50) from the Response effectively and in a simple way other than using the regular expression extractor 使用Java / Javascript的正则表达式/正则表达式:性能下降或无限循环 - Regular expression/Regex with Java/Javascript: performance drop or infinite loop JMeter结果分析的方法 - The way of JMeter Result Analysis
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM