degath
degath

Reputation: 1621

Loop sample with Regular Expression Extractor result in JMeter

I have 4 samples:

First one has a response similar to the 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.

Regular Expression Extractor

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.

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).

(I'm using JMeter 3.2, I'm able to bump it to 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}

Upvotes: 1

Views: 1376

Answers (3)

Ori Marko
Ori Marko

Reputation: 58862

EDIT

Your new requirement ${myVariable} += "<User><userId>${userId}</userId></User>"

Can be achieved by using JSR223 Sampler with the following line

vars.put("myVariable", vars.get("myVariable") + "<User><userId>"+ vars.get("userId") + "</userId></User>");

You should use the ForEach controller.example:

following variables should have been defined:

inputVar_1 = wendy

inputVar_2 = charles

inputVar_3 = peter

inputVar_4 = john

When the return variable is given as "returnVar", the collection of samplers and controllers under the ForEach controller will be executed 4 consecutive times

Upvotes: 1

Dmitri T
Dmitri T

Reputation: 168157

  1. It's better to use XPath Extractor which allows using XPath expressions for fetching data from XML/XHTML/HTML response types
  2. You can loop the variables using ForEach Controller

Upvotes: 1

SAIR
SAIR

Reputation: 499

Use ForEach_Controller to achieve this.

Practical example shown below:

  1. Test Plan looks like this:

enter image description here

  1. Extract User ID using Regular Expression Extractor

enter image description here

  1. Use For Each Controller to iterate over each value

enter image description here

  1. Use value from For Each Controller in your Request as you want

enter image description here

  1. Proof of its working

enter image description here

Upvotes: 1

Related Questions