开发者

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

开发者 https://www.devze.com 2023-02-06 11:11 出处:网络
In JMeter\'s User Parameters, how can I make a variable lowercase? Left column my_lowercase_var开发者_StackOverflowiable

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

Left column

my_lowercase_var开发者_StackOverflowiable

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.


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.


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


${__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.:

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

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

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

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())}


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


The beanshell and JavaScript functions in this use will fail, because they don't import the packages you need in order to use .toLowerCase.

If you really need to use a function to convert case (rather then declaring them as lowercase in the first place), you may need to write a full beanshell post-processor script in order to import the needed packages.

  • http://jmeter.apache.org/usermanual/functions.html#__BeanShell
  • http://jmeter.apache.org/usermanual/functions.html#__javaScript
  • http://www.javadocexamples.com/java_examples/org/apache/jmeter/


var blah = "${my_initial_reference}";

blah.toLowerCase();

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号