In JMeter and BeanShell, how can I make a variable lowercase?

16,670

Solution 1

Note to self.

It turns out to be a two liner in BeanShell Sampler rather than a __BeanShell command. Not exactly in the examples unfortunately.

I added the BeanShell Sampler under the Thread Group, then made a variable. No parameters in the form were required only the two liner script below. As long as I don't change the variable I can copy the data to another variable, change that instead, and then make a Value reference to that wherever needed.

First define a variable in some User Parameters or such ie:

Name: my_initial_reference 
Value: ITS IN CAPS

Add a Bean Sampler under the User Preferences or definition list (just next, it's not a child process)

Put in:

String blah = "${my_initial_reference}"; // 
vars.put("blah", blah.toLowerCase());  //${blah} = "its in caps" now available

Now under that with Name/Value pairs I can map ${blah} as the value to whatever process name requires it.

Note that the Debug response will still show the initial value in caps but you'll also see blah=its in caps which is what I wanted to use.

Solution 2

${__javaScript('${foobar}'.toLowerCase())} does work. If the output is ${foobar} instead of desired value, it means that the variable has not been declared

Note that variables are defined only after the "User Defined Variable" component has been parsed. Variables cannot be reused within a single "User Defined Variable" component e.g.:

enter image description here

The second row in that image will not be able to refer to the variable my_variable in the first row. To be able to refer to the first variable, two "User Defined Variable" components is needed. The first variable will be in the first component and the second variable in the second one, e.g.:

enter image description here

With that, ${my_lower_case_variable} will successfully be converted into some value.


${__BeanShell("${my_variable}".toLowerCase())} works too. (Note that Bean Shell requires double quotes. The code in your question uses single quotes.)

Another way is to use vars.get:

  • ${__javaScript(vars.get('my_variable').toLowerCase())}

  • ${__BeanShell(vars.get("my_variable").toLowerCase())}

Solution 3

Simply can add a function

${__lowercase(${VAL},VALUE)}
${__uppercase(${VAL},VALUE)}

Note: VAL can be correlated or paramiterized value (e.r VAL= TO LOWER or VAL= TO UPPER). We can use this function in beanshell (pre-processor/post-processor/sampler). Jmeter version using (2.6).

Can use it where ever we want in the script as ${VALUE}.

Solution 4

Hmmmm, your bean shell code didn't work for me. The bean shell sampler returned:

Response code: 500
Response message: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval  Sourced file: inline evaluation of: ``String blah = AAP;  vars.put("blah", blah.toLowerCase());  //${blah} now availab . . . '' : Typed variable declaration : Void initializer

I added two double quotes to solve it:

String blah = "${my_initial_reference}";
vars.put("blah", blah.toLowerCase());  //${blah} now available
Share:
16,670
sf2k
Author by

sf2k

Updated on June 08, 2022

Comments

  • sf2k
    sf2k almost 2 years

    In JMeter's User Parameters, how can I make a variable lowercase?

    Left column

    my_lowercase_variable
    

    Right column

    ${__BeanShell('${my_variable}'.toLowerCase())}  //fails
    

    or

    ${__javaScript('${my_variable}'.toLowerCase())}  //fails
    

    Such that ${my_lowercase_variable} is lowercase of ${my_variable}. Tried with quote and without and escaping and such. No luck. Any tricks or tips welcome.

  • sf2k
    sf2k over 13 years
    actually I was close, the BeanShell Sampler can make a variable. As long as I don't change it I can copy the data to another variable, the changed one, and then reference that.
  • sf2k
    sf2k almost 13 years
    Sorry for the inconvenience, I had typed it in and not copy/pasted so I had indeed forgotten the quotes as you have described.
  • Pacerier
    Pacerier over 8 years
    Is downloading the plugin a prerequisite to use this solution?
  • Pacerier
    Pacerier over 8 years
    @BlackGaff, What do you mean by "don't import the packages you need in order to use .toLowerCase"? (Something like "str".toLowerCase() works in both Bean Shell and Javascript without importing anything.)