Search code examples
performancejmeterperformance-testingjmeter-3.2jmeter-5.0

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}


Solution

  • 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