You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@jmeter.apache.org by Lionel Blanco <ml...@gmail.com> on 2023/06/14 21:59:02 UTC

Help with forEach Controller

Hello everyone.

I need help with forEach Controller.

I made this arraylist code in the Beanshell Preprocessor and I need to
iterate through it in a post request.

[image: imagen.png]
[image: imagen.png]

[image: imagen.png]

I understand that this is done with the forEach Controller but I can't
figure out how to use it.

[image: imagen.png]

If my beanshell code is this, how do I use it in the forEachController?
Thank you

import java.util.ArrayList;

String region = vars.get("region");
String canales = vars.getObject("canales");

ArrayList myList = new ArrayList();

switch (region){
    case "GT":
          myList.add("10001001");
          myList.add("10001002");
          myList.add("10001003");
          String listString = String.join(", ", myList);
        vars.putObject("canales",listString);
     break;
}

Re: Help with forEach Controller

Posted by Lionel Blanco <ml...@gmail.com>.
 Dmitri, Dimitri thank you very much, I was able to solve it. Very kind.

El jue, 15 jun 2023 a las 6:52, Dmitri T (<gl...@live.com>) escribió:

> Lionel Blanco wrote:
> > Hello everyone.
> >
> > I need help with forEach Controller.
> >
> > I made this arraylist code in the Beanshell Preprocessor and I need to
> > iterate through it in a post request.
> >
> > imagen.png
> > imagen.png
> >
> > imagen.png
> >
> > I understand that this is done with the forEach Controller but I can't
> > figure out how to use it.
> >
> > imagen.png
> >
> > If my beanshell code is this, how do I use it in the
> > forEachController?  Thank you
> >
> > importjava.util.ArrayList;
> >
> > Stringregion=vars.get("region");
> > Stringcanales=vars.getObject("canales");
> >
> > ArrayListmyList=newArrayList();
> >
> > switch(region){
> > case"GT":
> > myList.add("10001001");
> > myList.add("10001002");
> > myList.add("10001003");
> > StringlistString=String.join(", ",myList);
> > vars.putObject("canales",listString);
> > break;
> > }
> >
> >
> >
> >
> In order to be able to run your request 3 times with different
> typeServiceID you need to amend your code to look like:
>
> String region =vars.get("region");
>
> switch (region){
>      case "GT":
>          vars.put("canales1","10001001");
>          vars.put("canales2","10001002");
>          vars.put("canales3","10001003");
>          break;
> }
>
>
>
> and in the ForEach Controller
> <
> https://jmeter.apache.org/usermanual/component_reference.html#ForEach_Controller>
>
> use *canales* as the "Input variable prefix" and "Output variable name"
>
> Also be aware that starting from JMeter 3.1 you should be using JSR223
> Test Elements and Groovy language for scripting
> <https://jmeter.apache.org/usermanual/best-practices.html#bsh_scripting>
> so consider migrating. More information:Apache Groovy: What Is Groovy
> Used For? <https://www.blazemeter.com/blog/apache-groovy>
>
>

Re: Help with forEach Controller

Posted by Dmitri T <gl...@live.com>.
Lionel Blanco wrote:
> Hello everyone.
>
> I need help with forEach Controller.
>
> I made this arraylist code in the Beanshell Preprocessor and I need to 
> iterate through it in a post request.
>
> imagen.png
> imagen.png
>
> imagen.png
>
> I understand that this is done with the forEach Controller but I can't 
> figure out how to use it.
>
> imagen.png
>
> If my beanshell code is this, how do I use it in the 
> forEachController?  Thank you
>
> importjava.util.ArrayList;
>
> Stringregion=vars.get("region");
> Stringcanales=vars.getObject("canales");
>
> ArrayListmyList=newArrayList();
>
> switch(region){
> case"GT":
> myList.add("10001001");
> myList.add("10001002");
> myList.add("10001003");
> StringlistString=String.join(", ",myList);
> vars.putObject("canales",listString);
> break;
> }
>
>
>
>
In order to be able to run your request 3 times with different 
typeServiceID you need to amend your code to look like:

String region =vars.get("region");

switch (region){
     case "GT":
         vars.put("canales1","10001001");
         vars.put("canales2","10001002");
         vars.put("canales3","10001003");
         break;
}



and in the ForEach Controller 
<https://jmeter.apache.org/usermanual/component_reference.html#ForEach_Controller> 
use *canales* as the "Input variable prefix" and "Output variable name"

Also be aware that starting from JMeter 3.1 you should be using JSR223 
Test Elements and Groovy language for scripting 
<https://jmeter.apache.org/usermanual/best-practices.html#bsh_scripting> 
so consider migrating. More information:Apache Groovy: What Is Groovy 
Used For? <https://www.blazemeter.com/blog/apache-groovy>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@jmeter.apache.org
For additional commands, e-mail: user-help@jmeter.apache.org