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/03/08 22:52:31 UTC

Beanshell Array list.

Hello to the whole community. I need help with this "Beanshell
preprocessor". I want to make a list, and pass it in a while. Can
someone tell me why it doesn't work?


String region = vars.get("region");
String[] myList = vars.get("canales");

switch (region){
    case "GT":
          myList.add("30000007");
        myList.add("70000007");
        myList.add("90000007");
        vars.put("canales",myList);
   break;
}


[image: imagen.png]

Lionel Blanco | *Tester QA Automation*

Re: Beanshell Array list.

Posted by Owen Pahl <ow...@gmail.com>.
Hi Lionel,

As Dmitri has mentioned moving to groovy over beanshell will do you many
favours.

But the issue you are seeing now is that you are getting a string
representation of the ArrayList in your message.
I assume you've used a JMeter variable expansion in your sampler
"${canales}"?

It is unclear from your previous messages how you intend to use the values
in the ArrayList.
If you intend to loop through each value in the array and insert it into
your message I would suggest using a ForEach
<https://jmeter.apache.org/usermanual/component_reference.html#ForEach_Controller>
controller and formatting your input values into named variables rather
than an array eg vars.put("canales_1", <value 1>)
This can get a bit messy if there are lots of values and lots of different
variables in which case storing them as arrays and then flattening them
later might work too.
This is where groovy makes like quite easy:

"(vars.getObject as List).eachWithIndex{ value, index ->
vars.put("canales_${index}", value  }" or similar.


Cheers,
Owen


On Thu, 9 Mar 2023 at 14:34, Lionel Blanco <ml...@gmail.com> wrote:

>
> Owen, After reading the link of the variables, I understood how it works.
> I managed to get it to work, but it gives me the number with "brackets".
> [30000007] .
> Do you know how to make them not happen?
>
> My code:
>
> import java.util.ArrayList;
> String region = vars.get("region");
> String canales = vars.getObject("canales");
> ArrayList myList = new ArrayList();
>
> switch (region){
>     case "GT":
>         myList.add("30000007");
>         vars.putObject("canales",myList);
>    break;
> }
>
> Response:
>
> [image: imagen.png]
>
> Lionel Blanco | *Tester QA Automation*
>
> El mié, 8 mar 2023 a las 21:42, Lionel Blanco (<ml...@gmail.com>)
> escribió:
>
>> Thanks for your answer friend.
>> I can't understand well. Would you help me here in the example?
>>
>>
>> String region = vars.get("region");
>> String[] myList = vars.get("canales");
>>
>> switch (region){
>>     case "GT":
>>         myList.add("30000007");
>>         myList.add("70000007");
>>         myList.add("90000007");
>>         vars.put("canales",myList);
>>    break;
>> }
>>
>>
>>
>> Lionel Blanco | *Tester QA Automation*
>>
>>
>>
>> El mié, 8 mar 2023 a las 20:24, Owen Pahl (<ow...@gmail.com>)
>> escribió:
>>
>>> Hi Lionel,
>>>
>>> Jun Zhuang is correct. vars.put() and vars.get() work with strings.
>>> If you want to store/retrieve objects you need to use vars.getObject and
>>> vars.putObject. Don't forget you'll likely need to cast the return value of
>>> getObject.
>>>
>>> See the javadocs here
>>> https://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterVariables.html
>>>
>>>
>>> Cheers,
>>> Owen
>>>
>>>
>>> On Thu, 9 Mar 2023 at 12:08, Jun Zhuang <th...@yahoo.com.invalid>
>>> wrote:
>>>
>>>> I think vars.get() always returns a string - don't think it can return
>>>> an array.
>>>> On Wednesday, March 8, 2023 at 05:52:49 PM EST, Lionel Blanco <
>>>> mlionelblanco@gmail.com> wrote:
>>>>
>>>>
>>>> Hello to the whole community. I need help with this "Beanshell preprocessor". I want to make a list, and pass it in a while. Can someone tell me why it doesn't work?
>>>>
>>>>
>>>> String region = vars.get("region");
>>>> String[] myList = vars.get("canales");
>>>>
>>>> switch (region){
>>>>     case "GT":
>>>>           myList.add("30000007");
>>>>         myList.add("70000007");
>>>>         myList.add("90000007");
>>>>         vars.put("canales",myList);
>>>>    break;
>>>> }
>>>>
>>>>
>>>> [image: imagen.png]
>>>>
>>>> Lionel Blanco | *Tester QA Automation*
>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: user-unsubscribe@jmeter.apache.org
>>>> For additional commands, e-mail: user-help@jmeter.apache.org
>>>
>>>

Re: Beanshell Array list.

Posted by Jun Zhuang <th...@yahoo.com.INVALID>.
 I think concept is your main problem, code is not ;)
> The way your wrote your code will never give you an array. That part you will have to figure out yourself.. > If you do need to save a string array to a variable and loop through it later, here is a reference for how-to :  https://stackoverflow.com/questions/26381643/how-to-store-array-values-in-jmeter-variables> This post talked about how to loop through an array. The way you use while loop won't work. https://stackoverflow.com/questions/47122466/jmeter-need-to-loop-through-the-array> For debugging purpose, you can use log.info() in your code. Make sure you remove them for formal runs.






    On Thursday, March 9, 2023 at 12:03:02 PM EST, Lionel Blanco <ml...@gmail.com> wrote:  
 
 Hello everyone. 
I did it in the following way: 

This is my BeanShell PreProcessor: 

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;
}



This is my HTTP Request: 



This is my WHILE: 

This is my RESPONSE: 


What I can't achieve is that in each iteration, execute a new number.Example:
Execution 1: 10001001
Execution 2: 10001002
Execution 3: 10001003
Someone give me 15 minutes of their valuable time and write me the code to do that?Thank you for your patience.

Lionel

El jue, 9 mar 2023 a las 9:30, Jun Zhuang (<th...@yahoo.com.invalid>) escribió:

Since you are using a preprocessor, I suppose you only need to deal with 1 value every iteration, so the array list may be unnecessary. A string variable should be enough. In this specific example, if you want to remove the brackets, you can use a java substring function.
String sFinalValue = sCurrent.substring(1, sCurrent.length() - 1);
BTW, Beanshell preprocessor is more expensive than JSSR223.
   On Wednesday, March 8, 2023 at 08:34:50 PM EST, Lionel Blanco <ml...@gmail.com> wrote:  
 
 
Owen, After reading the link of the variables, I understood how it works. I managed to get it to work, but it gives me the number with "brackets". [30000007] .
Do you know how to make them not happen?
My code: 
import java.util.ArrayList;
String region = vars.get("region");
String canales = vars.getObject("canales");
ArrayList myList = new ArrayList();

switch (region){
    case "GT":
        myList.add("30000007");
        vars.putObject("canales",myList);
   break;
}Response: 



Lionel Blanco | Tester QA Automation

El mié, 8 mar 2023 a las 21:42, Lionel Blanco (<ml...@gmail.com>) escribió:

Thanks for your answer friend.
I can't understand well. Would you help me here in the example?
String region = vars.get("region");
String[] myList = vars.get("canales");

switch (region){
    case "GT":
        myList.add("30000007");
        myList.add("70000007");
        myList.add("90000007");
        vars.put("canales",myList);
   break;
}

Lionel Blanco | Tester QA Automation



El mié, 8 mar 2023 a las 20:24, Owen Pahl (<ow...@gmail.com>) escribió:

Hi Lionel,
Jun Zhuang is correct. vars.put() and vars.get() work with strings.
If you want to store/retrieve objects you need to use vars.getObject and vars.putObject. Don't forget you'll likely need to cast the return value of getObject.

See the javadocs here https://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterVariables.html

Cheers,Owen

On Thu, 9 Mar 2023 at 12:08, Jun Zhuang <th...@yahoo.com.invalid> wrote:

I think vars.get() always returns a string - don't think it can return an array.
   On Wednesday, March 8, 2023 at 05:52:49 PM EST, Lionel Blanco <ml...@gmail.com> wrote:  
 
 Hello to the whole community. I need help with this "Beanshell preprocessor". I want to make a list, and pass it in a while. Can someone tell me why it doesn't work?
String region = vars.get("region");
String[] myList = vars.get("canales");

switch (region){
    case "GT":
          myList.add("30000007");
        myList.add("70000007");
        myList.add("90000007");
        vars.put("canales",myList);
   break;
}


Lionel Blanco | Tester QA Automation

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


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

Re: Beanshell Array list.

Posted by Lionel Blanco <ml...@gmail.com>.
Dear Dimitri.
I didn't want to create more confusion, so I didn't use groovy.
Apologies for that.
I'll investigate.
Thanks for taking the time to reply.

Lionel Blanco | *Tester QA Automation*


El jue, 9 mar 2023 a las 15:34, Dmitri T (<gl...@live.com>) escribió:

> Lionel Blanco wrote:
> > Hello everyone.
> > I did it in the following way:
> >
> > This is my BeanShell PreProcessor:
> >
> > 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;
> > }
> > imagen.png
> >
> >
> > *This is my HTTP Request: *
> >
> > imagen.png
> >
> > *This is my WHILE: *
> > imagen.png
> > *This is my RESPONSE: *
> >
> > imagen.png
> > What I can't achieve is that in each iteration, execute a new
> > number.Example:
> > Execution 1: 10001001
> > Execution 2: 10001002
> > Execution 3: 10001003
> >
> > Someone give me 15 minutes of their valuable time and write me the
> > code to do that?
> > Thank you for your patience.
> >
> > Lionel
> >
> > El jue, 9 mar 2023 a las 9:30, Jun Zhuang
> > (<th...@yahoo.com.invalid>) escribió:
> >
> >     Since you are using a preprocessor, I suppose you only need to
> >     deal with 1 value every iteration, so the array list may be
> >     unnecessary. A string variable should be enough. In this specific
> >     example, if you want to remove the brackets, you can use a java
> >     substring function.
> >
> >     String sFinalValue = sCurrent.substring(1, sCurrent.length() - 1);
> >
> >     BTW, Beanshell preprocessor is more expensive than JSSR223.
> >     On Wednesday, March 8, 2023 at 08:34:50 PM EST, Lionel Blanco
> >     <mlionelblanco@gmail.com <ma...@gmail.com>> wrote:
> >
> >
> >
> >     Owen, After reading the link of the variables, I understood how it
> >     works. I managed to get it to work, but it gives me the number
> >     with "brackets". [30000007] .
> >     Do you know how to make them not happen?
> >
> >     My code:
> >
> >     importjava.util.ArrayList;
> >     Stringregion=vars.get("region");
> >     Stringcanales=vars.getObject("canales");
> >     ArrayListmyList=newArrayList();
> >
> >     switch(region){
> >     case"GT":
> >     myList.add("30000007");
> >     vars.putObject("canales",myList);
> >     break;
> >     }
> >
> >     Response:
> >
> >     imagen.png
> >
> >     Lionel Blanco | *Tester QA Automation*
> >
> >     El mié, 8 mar 2023 a las 21:42, Lionel Blanco
> >     (<mlionelblanco@gmail.com <ma...@gmail.com>>)
> escribió:
> >
> >         Thanks for your answer friend. I can't understand well. Would
> >         you help me here in the example?
> >
> >
> >         Stringregion=vars.get("region");
> >         String[]myList=vars.get("canales");
> >
> >         switch(region){
> >         case"GT":
> >         myList.add("30000007");
> >         myList.add("70000007");
> >         myList.add("90000007");
> >         vars.put("canales",myList);
> >         break;
> >         }
> >
> >
> >
> >         Lionel Blanco | *Tester QA Automation*
> >
> >
> >
> >         El mié, 8 mar 2023 a las 20:24, Owen Pahl
> >         (<owen.pahl@gmail.com <ma...@gmail.com>>) escribió:
> >
> >             Hi Lionel,
> >
> >             Jun Zhuang is correct. vars.put() and vars.get() work with
> >             strings.
> >             If you want to store/retrieve objects you need to use
> >             vars.getObject and vars.putObject. Don't forget you'll
> >             likely need to cast the return value of getObject.
> >
> >             See the javadocs here
> >
> https://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterVariables.html
> >
> >
> >             Cheers,
> >             Owen
> >
> >
> >             On Thu, 9 Mar 2023 at 12:08, Jun Zhuang
> >             <th...@yahoo.com.invalid> wrote:
> >
> >                 I think vars.get() always returns a string - don't
> >                 think it can return an array.
> >                 On Wednesday, March 8, 2023 at 05:52:49 PM EST, Lionel
> >                 Blanco <mlionelblanco@gmail.com
> >                 <ma...@gmail.com>> wrote:
> >
> >
> >                 Hello to the whole community. I need help with this
> >                 "Beanshell preprocessor". I want to make a list, and
> >                 pass it in a while. Can someone tell me why it doesn't
> >                 work?
> >
> >                 Stringregion=vars.get("region");
> >                 String[]myList=vars.get("canales");
> >
> >                 switch(region){
> >                 case"GT":
> >                 myList.add("30000007");
> >                 myList.add("70000007");
> >                 myList.add("90000007");
> >                 vars.put("canales",myList);
> >                 break;
> >                 }
> >
> >
> >                 imagen.png
> >
> >                 Lionel Blanco | *Tester QA Automation*
> >
> >
> >
>  ---------------------------------------------------------------------
> >                 To unsubscribe, e-mail:
> >                 user-unsubscribe@jmeter.apache.org
> >                 <ma...@jmeter.apache.org>
> >                 For additional commands, e-mail:
> >                 user-help@jmeter.apache.org
> >                 <ma...@jmeter.apache.org>
> >
> >
> >     ---------------------------------------------------------------------
> >     To unsubscribe, e-mail: user-unsubscribe@jmeter.apache.org
> >     <ma...@jmeter.apache.org>
> >     For additional commands, e-mail: user-help@jmeter.apache.org
> >     <ma...@jmeter.apache.org>
> >
> If you want to keep using your beautiful Beanshell code, although you
> have been told to use Groovy 3 times already, which does nothing by the
> way you can achieve "is that in each iteration, execute a new number" by
> using the following __ Beanshell() function
> <https://jmeter.apache.org/usermanual/functions.html#__BeanShell> (yes,
> we remember, you love Beanshell)
>
> ${__BeanShell(vars.get("canales").split("\,")[Integer.parseInt(vars.get("__jm__While
>
> Provisioning Product FIja__idx"))].trim(),)}
>
> put it instead of ${canales} in your HTTP Request sampler.
>
> P.S.  You can see what JMeter Variables are defined along with their
> respective values using Debug Sampler and View Results Tree listener
> combination <https://www.blazemeter.com/blog/debug-jmeter>
> P.P.S. You might want to use ForEach Controller
> <
> https://jmeter.apache.org/usermanual/component_reference.html#ForEach_Controller>
>
> instead of the While Controller
>

Re: Beanshell Array list.

Posted by Dmitri T <gl...@live.com>.
Lionel Blanco wrote:
> Hello everyone.
> I did it in the following way:
>
> This is my BeanShell PreProcessor:
>
> 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;
> }
> imagen.png
>
>
> *This is my HTTP Request: *
>
> imagen.png
>
> *This is my WHILE: *
> imagen.png
> *This is my RESPONSE: *
>
> imagen.png
> What I can't achieve is that in each iteration, execute a new 
> number.Example:
> Execution 1: 10001001
> Execution 2: 10001002
> Execution 3: 10001003
>
> Someone give me 15 minutes of their valuable time and write me the 
> code to do that?
> Thank you for your patience.
>
> Lionel
>
> El jue, 9 mar 2023 a las 9:30, Jun Zhuang 
> (<th...@yahoo.com.invalid>) escribió:
>
>     Since you are using a preprocessor, I suppose you only need to
>     deal with 1 value every iteration, so the array list may be
>     unnecessary. A string variable should be enough. In this specific
>     example, if you want to remove the brackets, you can use a java
>     substring function.
>
>     String sFinalValue = sCurrent.substring(1, sCurrent.length() - 1);
>
>     BTW, Beanshell preprocessor is more expensive than JSSR223.
>     On Wednesday, March 8, 2023 at 08:34:50 PM EST, Lionel Blanco
>     <mlionelblanco@gmail.com <ma...@gmail.com>> wrote:
>
>
>
>     Owen, After reading the link of the variables, I understood how it
>     works. I managed to get it to work, but it gives me the number
>     with "brackets". [30000007] .
>     Do you know how to make them not happen?
>
>     My code:
>
>     importjava.util.ArrayList;
>     Stringregion=vars.get("region");
>     Stringcanales=vars.getObject("canales");
>     ArrayListmyList=newArrayList();
>
>     switch(region){
>     case"GT":
>     myList.add("30000007");
>     vars.putObject("canales",myList);
>     break;
>     }
>
>     Response:
>
>     imagen.png
>
>     Lionel Blanco | *Tester QA Automation*
>
>     El mié, 8 mar 2023 a las 21:42, Lionel Blanco
>     (<mlionelblanco@gmail.com <ma...@gmail.com>>) escribió:
>
>         Thanks for your answer friend. I can't understand well. Would
>         you help me here in the example?
>
>
>         Stringregion=vars.get("region");
>         String[]myList=vars.get("canales");
>
>         switch(region){
>         case"GT":
>         myList.add("30000007");
>         myList.add("70000007");
>         myList.add("90000007");
>         vars.put("canales",myList);
>         break;
>         }
>
>
>
>         Lionel Blanco | *Tester QA Automation*
>
>
>
>         El mié, 8 mar 2023 a las 20:24, Owen Pahl
>         (<owen.pahl@gmail.com <ma...@gmail.com>>) escribió:
>
>             Hi Lionel,
>
>             Jun Zhuang is correct. vars.put() and vars.get() work with
>             strings.
>             If you want to store/retrieve objects you need to use
>             vars.getObject and vars.putObject. Don't forget you'll
>             likely need to cast the return value of getObject.
>
>             See the javadocs here
>             https://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterVariables.html
>
>
>             Cheers,
>             Owen
>
>
>             On Thu, 9 Mar 2023 at 12:08, Jun Zhuang
>             <th...@yahoo.com.invalid> wrote:
>
>                 I think vars.get() always returns a string - don't
>                 think it can return an array.
>                 On Wednesday, March 8, 2023 at 05:52:49 PM EST, Lionel
>                 Blanco <mlionelblanco@gmail.com
>                 <ma...@gmail.com>> wrote:
>
>
>                 Hello to the whole community. I need help with this
>                 "Beanshell preprocessor". I want to make a list, and
>                 pass it in a while. Can someone tell me why it doesn't
>                 work?
>
>                 Stringregion=vars.get("region");
>                 String[]myList=vars.get("canales");
>
>                 switch(region){
>                 case"GT":
>                 myList.add("30000007");
>                 myList.add("70000007");
>                 myList.add("90000007");
>                 vars.put("canales",myList);
>                 break;
>                 }
>
>
>                 imagen.png
>
>                 Lionel Blanco | *Tester QA Automation*
>
>
>                 ---------------------------------------------------------------------
>                 To unsubscribe, e-mail:
>                 user-unsubscribe@jmeter.apache.org
>                 <ma...@jmeter.apache.org>
>                 For additional commands, e-mail:
>                 user-help@jmeter.apache.org
>                 <ma...@jmeter.apache.org>
>
>
>     ---------------------------------------------------------------------
>     To unsubscribe, e-mail: user-unsubscribe@jmeter.apache.org
>     <ma...@jmeter.apache.org>
>     For additional commands, e-mail: user-help@jmeter.apache.org
>     <ma...@jmeter.apache.org>
>
If you want to keep using your beautiful Beanshell code, although you 
have been told to use Groovy 3 times already, which does nothing by the 
way you can achieve "is that in each iteration, execute a new number" by 
using the following __ Beanshell() function 
<https://jmeter.apache.org/usermanual/functions.html#__BeanShell> (yes, 
we remember, you love Beanshell)

${__BeanShell(vars.get("canales").split("\,")[Integer.parseInt(vars.get("__jm__While 
Provisioning Product FIja__idx"))].trim(),)}

put it instead of ${canales} in your HTTP Request sampler.

P.S.  You can see what JMeter Variables are defined along with their 
respective values using Debug Sampler and View Results Tree listener 
combination <https://www.blazemeter.com/blog/debug-jmeter>
P.P.S. You might want to use ForEach Controller 
<https://jmeter.apache.org/usermanual/component_reference.html#ForEach_Controller> 
instead of the While Controller

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


Re: Beanshell Array list.

Posted by Lionel Blanco <ml...@gmail.com>.
 Hello everyone.
I did it in the following way:

This is my BeanShell PreProcessor:

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;
}

[image: imagen.png]


*This is my HTTP Request: *

[image: imagen.png]

*This is my WHILE: *
[image: imagen.png]
*This is my RESPONSE: *

[image: imagen.png]
What I can't achieve is that in each iteration, execute a new number.
Example:
Execution 1: 10001001
Execution 2: 10001002
Execution 3: 10001003

Someone give me 15 minutes of their valuable time and write me the code to
do that?
Thank you for your patience.

Lionel

El jue, 9 mar 2023 a las 9:30, Jun Zhuang (<th...@yahoo.com.invalid>)
escribió:

> Since you are using a preprocessor, I suppose you only need to deal with 1
> value every iteration, so the array list may be unnecessary. A string
> variable should be enough. In this specific example, if you want to remove
> the brackets, you can use a java substring function.
>
> String sFinalValue = sCurrent.substring(1, sCurrent.length() - 1);
>
> BTW, Beanshell preprocessor is more expensive than JSSR223.
> On Wednesday, March 8, 2023 at 08:34:50 PM EST, Lionel Blanco <
> mlionelblanco@gmail.com> wrote:
>
>
>
> Owen, After reading the link of the variables, I understood how it works.
> I managed to get it to work, but it gives me the number with "brackets".
> [30000007] .
> Do you know how to make them not happen?
>
> My code:
>
> import java.util.ArrayList;
> String region = vars.get("region");
> String canales = vars.getObject("canales");
> ArrayList myList = new ArrayList();
>
> switch (region){
>     case "GT":
>         myList.add("30000007");
>         vars.putObject("canales",myList);
>    break;
> }
>
> Response:
>
> [image: imagen.png]
>
> Lionel Blanco | *Tester QA Automation*
>
> El mié, 8 mar 2023 a las 21:42, Lionel Blanco (<ml...@gmail.com>)
> escribió:
>
> Thanks for your answer friend.
> I can't understand well. Would you help me here in the example?
>
>
> String region = vars.get("region");
> String[] myList = vars.get("canales");
>
> switch (region){
>     case "GT":
>         myList.add("30000007");
>         myList.add("70000007");
>         myList.add("90000007");
>         vars.put("canales",myList);
>    break;
> }
>
>
>
> Lionel Blanco | *Tester QA Automation*
>
>
>
> El mié, 8 mar 2023 a las 20:24, Owen Pahl (<ow...@gmail.com>)
> escribió:
>
> Hi Lionel,
>
> Jun Zhuang is correct. vars.put() and vars.get() work with strings.
> If you want to store/retrieve objects you need to use vars.getObject and
> vars.putObject. Don't forget you'll likely need to cast the return value of
> getObject.
>
> See the javadocs here
> https://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterVariables.html
>
>
> Cheers,
> Owen
>
>
> On Thu, 9 Mar 2023 at 12:08, Jun Zhuang <th...@yahoo.com.invalid>
> wrote:
>
> I think vars.get() always returns a string - don't think it can return an
> array.
> On Wednesday, March 8, 2023 at 05:52:49 PM EST, Lionel Blanco <
> mlionelblanco@gmail.com> wrote:
>
>
> Hello to the whole community. I need help with this "Beanshell preprocessor". I want to make a list, and pass it in a while. Can someone tell me why it doesn't work?
>
>
> String region = vars.get("region");
> String[] myList = vars.get("canales");
>
> switch (region){
>     case "GT":
>           myList.add("30000007");
>         myList.add("70000007");
>         myList.add("90000007");
>         vars.put("canales",myList);
>    break;
> }
>
>
> [image: imagen.png]
>
> Lionel Blanco | *Tester QA Automation*
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@jmeter.apache.org
> For additional commands, e-mail: user-help@jmeter.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@jmeter.apache.org
> For additional commands, e-mail: user-help@jmeter.apache.org

Re: Beanshell Array list.

Posted by Jun Zhuang <th...@yahoo.com.INVALID>.
Since you are using a preprocessor, I suppose you only need to deal with 1 value every iteration, so the array list may be unnecessary. A string variable should be enough. In this specific example, if you want to remove the brackets, you can use a java substring function.
String sFinalValue = sCurrent.substring(1, sCurrent.length() - 1);
BTW, Beanshell preprocessor is more expensive than JSSR223.
   On Wednesday, March 8, 2023 at 08:34:50 PM EST, Lionel Blanco <ml...@gmail.com> wrote:  
 
 
Owen, After reading the link of the variables, I understood how it works. I managed to get it to work, but it gives me the number with "brackets". [30000007] .
Do you know how to make them not happen?
My code: 
import java.util.ArrayList;
String region = vars.get("region");
String canales = vars.getObject("canales");
ArrayList myList = new ArrayList();

switch (region){
    case "GT":
        myList.add("30000007");
        vars.putObject("canales",myList);
   break;
}Response: 



Lionel Blanco | Tester QA Automation

El mié, 8 mar 2023 a las 21:42, Lionel Blanco (<ml...@gmail.com>) escribió:

Thanks for your answer friend.
I can't understand well. Would you help me here in the example?
String region = vars.get("region");
String[] myList = vars.get("canales");

switch (region){
    case "GT":
        myList.add("30000007");
        myList.add("70000007");
        myList.add("90000007");
        vars.put("canales",myList);
   break;
}

Lionel Blanco | Tester QA Automation



El mié, 8 mar 2023 a las 20:24, Owen Pahl (<ow...@gmail.com>) escribió:

Hi Lionel,
Jun Zhuang is correct. vars.put() and vars.get() work with strings.
If you want to store/retrieve objects you need to use vars.getObject and vars.putObject. Don't forget you'll likely need to cast the return value of getObject.

See the javadocs here https://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterVariables.html

Cheers,Owen

On Thu, 9 Mar 2023 at 12:08, Jun Zhuang <th...@yahoo.com.invalid> wrote:

I think vars.get() always returns a string - don't think it can return an array.
   On Wednesday, March 8, 2023 at 05:52:49 PM EST, Lionel Blanco <ml...@gmail.com> wrote:  
 
 Hello to the whole community. I need help with this "Beanshell preprocessor". I want to make a list, and pass it in a while. Can someone tell me why it doesn't work?
String region = vars.get("region");
String[] myList = vars.get("canales");

switch (region){
    case "GT":
          myList.add("30000007");
        myList.add("70000007");
        myList.add("90000007");
        vars.put("canales",myList);
   break;
}


Lionel Blanco | Tester QA Automation

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


  

Re: Beanshell Array list.

Posted by Lionel Blanco <ml...@gmail.com>.
Owen, After reading the link of the variables, I understood how it works. I
managed to get it to work, but it gives me the number with "brackets".
[30000007] .
Do you know how to make them not happen?

My code:

import java.util.ArrayList;
String region = vars.get("region");
String canales = vars.getObject("canales");
ArrayList myList = new ArrayList();

switch (region){
    case "GT":
        myList.add("30000007");
        vars.putObject("canales",myList);
   break;
}

Response:

[image: imagen.png]

Lionel Blanco | *Tester QA Automation*

El mié, 8 mar 2023 a las 21:42, Lionel Blanco (<ml...@gmail.com>)
escribió:

> Thanks for your answer friend.
> I can't understand well. Would you help me here in the example?
>
>
> String region = vars.get("region");
> String[] myList = vars.get("canales");
>
> switch (region){
>     case "GT":
>         myList.add("30000007");
>         myList.add("70000007");
>         myList.add("90000007");
>         vars.put("canales",myList);
>    break;
> }
>
>
>
> Lionel Blanco | *Tester QA Automation*
>
>
>
> El mié, 8 mar 2023 a las 20:24, Owen Pahl (<ow...@gmail.com>)
> escribió:
>
>> Hi Lionel,
>>
>> Jun Zhuang is correct. vars.put() and vars.get() work with strings.
>> If you want to store/retrieve objects you need to use vars.getObject and
>> vars.putObject. Don't forget you'll likely need to cast the return value of
>> getObject.
>>
>> See the javadocs here
>> https://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterVariables.html
>>
>>
>> Cheers,
>> Owen
>>
>>
>> On Thu, 9 Mar 2023 at 12:08, Jun Zhuang <th...@yahoo.com.invalid>
>> wrote:
>>
>>> I think vars.get() always returns a string - don't think it can return
>>> an array.
>>> On Wednesday, March 8, 2023 at 05:52:49 PM EST, Lionel Blanco <
>>> mlionelblanco@gmail.com> wrote:
>>>
>>>
>>> Hello to the whole community. I need help with this "Beanshell preprocessor". I want to make a list, and pass it in a while. Can someone tell me why it doesn't work?
>>>
>>>
>>> String region = vars.get("region");
>>> String[] myList = vars.get("canales");
>>>
>>> switch (region){
>>>     case "GT":
>>>           myList.add("30000007");
>>>         myList.add("70000007");
>>>         myList.add("90000007");
>>>         vars.put("canales",myList);
>>>    break;
>>> }
>>>
>>>
>>> [image: imagen.png]
>>>
>>> Lionel Blanco | *Tester QA Automation*
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: user-unsubscribe@jmeter.apache.org
>>> For additional commands, e-mail: user-help@jmeter.apache.org
>>
>>

Re: Beanshell Array list.

Posted by Lionel Blanco <ml...@gmail.com>.
Thanks for your answer friend.
I can't understand well. Would you help me here in the example?


String region = vars.get("region");
String[] myList = vars.get("canales");

switch (region){
    case "GT":
        myList.add("30000007");
        myList.add("70000007");
        myList.add("90000007");
        vars.put("canales",myList);
   break;
}



Lionel Blanco | *Tester QA Automation*



El mié, 8 mar 2023 a las 20:24, Owen Pahl (<ow...@gmail.com>) escribió:

> Hi Lionel,
>
> Jun Zhuang is correct. vars.put() and vars.get() work with strings.
> If you want to store/retrieve objects you need to use vars.getObject and
> vars.putObject. Don't forget you'll likely need to cast the return value of
> getObject.
>
> See the javadocs here
> https://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterVariables.html
>
>
> Cheers,
> Owen
>
>
> On Thu, 9 Mar 2023 at 12:08, Jun Zhuang <th...@yahoo.com.invalid>
> wrote:
>
>> I think vars.get() always returns a string - don't think it can return an
>> array.
>> On Wednesday, March 8, 2023 at 05:52:49 PM EST, Lionel Blanco <
>> mlionelblanco@gmail.com> wrote:
>>
>>
>> Hello to the whole community. I need help with this "Beanshell preprocessor". I want to make a list, and pass it in a while. Can someone tell me why it doesn't work?
>>
>>
>> String region = vars.get("region");
>> String[] myList = vars.get("canales");
>>
>> switch (region){
>>     case "GT":
>>           myList.add("30000007");
>>         myList.add("70000007");
>>         myList.add("90000007");
>>         vars.put("canales",myList);
>>    break;
>> }
>>
>>
>> [image: imagen.png]
>>
>> Lionel Blanco | *Tester QA Automation*
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@jmeter.apache.org
>> For additional commands, e-mail: user-help@jmeter.apache.org
>
>

Re: Beanshell Array list.

Posted by Owen Pahl <ow...@gmail.com>.
Hi Lionel,

Jun Zhuang is correct. vars.put() and vars.get() work with strings.
If you want to store/retrieve objects you need to use vars.getObject and
vars.putObject. Don't forget you'll likely need to cast the return value of
getObject.

See the javadocs here
https://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterVariables.html


Cheers,
Owen


On Thu, 9 Mar 2023 at 12:08, Jun Zhuang <th...@yahoo.com.invalid>
wrote:

> I think vars.get() always returns a string - don't think it can return an
> array.
> On Wednesday, March 8, 2023 at 05:52:49 PM EST, Lionel Blanco <
> mlionelblanco@gmail.com> wrote:
>
>
> Hello to the whole community. I need help with this "Beanshell preprocessor". I want to make a list, and pass it in a while. Can someone tell me why it doesn't work?
>
>
> String region = vars.get("region");
> String[] myList = vars.get("canales");
>
> switch (region){
>     case "GT":
>           myList.add("30000007");
>         myList.add("70000007");
>         myList.add("90000007");
>         vars.put("canales",myList);
>    break;
> }
>
>
> [image: imagen.png]
>
> Lionel Blanco | *Tester QA Automation*
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@jmeter.apache.org
> For additional commands, e-mail: user-help@jmeter.apache.org

Re: Beanshell Array list.

Posted by Jun Zhuang <th...@yahoo.com.INVALID>.
I think vars.get() always returns a string - don't think it can return an array.
   On Wednesday, March 8, 2023 at 05:52:49 PM EST, Lionel Blanco <ml...@gmail.com> wrote:  
 
 Hello to the whole community. I need help with this "Beanshell preprocessor". I want to make a list, and pass it in a while. Can someone tell me why it doesn't work?
String region = vars.get("region");
String[] myList = vars.get("canales");

switch (region){
    case "GT":
          myList.add("30000007");
        myList.add("70000007");
        myList.add("90000007");
        vars.put("canales",myList);
   break;
}


Lionel Blanco | Tester QA Automation

  

Re: Beanshell Array list.

Posted by Dmitri T <gl...@live.com>.
Lionel Blanco wrote:
> Hello to the whole community. I need help with this "Beanshell 
> preprocessor". I want to make a list, and pass it in a while. Can 
> someone tell me why it doesn't work?
> Stringregion=vars.get("region");
> String[]myList=vars.get("canales");
>
> switch(region){
> case"GT":
> myList.add("30000007");
> myList.add("70000007");
> myList.add("90000007");
> vars.put("canales",myList);
> break;
> }
>
> imagen.png
>
> Lionel Blanco | *Tester QA Automation*
>
*vars* stands for JMeterVariables 
<https://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterVariables.html> 
class instance, if you take a look at documentation on vars.put() 
<https://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterVariables.html#put(java.lang.String,java.lang.String)> 
function you will see that both arguments are Strings 
<https://docs.oracle.com/javase/7/docs/api/java/lang/String.html>

If you want to store anything else - you will need to use 
vars.putObject() 
<https://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterVariables.html#putObject(java.lang.String,java.lang.Object)> 
for storing the  value and vars.getObject() 
<https://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterVariables.html#getObject(java.lang.String)> 
for reading the value

In general since JMeter 3.1 it's recommended to use 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