javascript 在 JMeter 和 BeanShell 中,如何使变量小写?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4755286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 14:46:59  来源:igfitidea点击:

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

javascriptjmeterlowercasebeanshell

提问by Walter Spicer

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

在 JMeter 的用户参数中,如何使变量小写?

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.

${my_lowercase_variable}就是 的小写${my_variable}。尝试使用报价,没有和转义等。没有运气。欢迎任何技巧或提示。

回答by Walter Spicer

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.

结果证明是 BeanShell Sampler 中的两行代码,而不是 __BeanShell 命令。不幸的是,并不完全在示例中。

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.

我在线程组下添加了BeanShell Sampler,然后做了一个变量。表单中没有参数只需要下面的两个 liner 脚本。只要我不更改变量,我就可以将数据复制到另一个变量,改为更改它,然后在任何需要的地方对其进行 Value 引用。

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)

在 User Preferences 或定义列表下添加一个 Bean Sampler(只是接下来,它不是子进程)

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.

现在使用名称/值对,我可以将 ${blah} 作为值映射到任何需要它的进程名称。

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.

请注意,调试响应仍会以大写字母显示初始值,但您还会看到 blah=its 大写字母,这是我想要使用的。

回答by Pacerier

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

${__javaScript('${foobar}'.toLowerCase())}确实有效。如果输出是${foobar}而不是desired value,则表示该变量尚未声明

Note that variables are defined only afterthe "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_variablein 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.:

该图像中的第二行将无法引用my_variable第一行中的变量。为了能够引用第一个变量,需要两个“用户定义变量”组件。第一个变量将在第一个组件中,第二个变量将在第二个组件中,例如:

enter image description here

在此处输入图片说明

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

这样,${my_lower_case_variable}将成功转换为some value.



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

${__BeanShell("${my_variable}".toLowerCase())}也有效。(请注意,Bean Shell需要双引号。您问题中的代码使用单引号。)

Another way is to use vars.get:

另一种方法是使用vars.get

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

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

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

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

回答by Doxa

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

嗯,你的 bean shell 代码对我不起作用。豆壳采样器返回:

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

回答by priyanka

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

注意:VAL 可以是相关值或参数化值(er VAL= TO LOWER 或 VAL= TO UPPER)。我们可以在 beanshell (pre-processor/post-processor/sampler) 中使用这个函数。Jmeter 版本使用 (2.6)。

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

可以在脚本中任何我们想要的地方使用它作为 ${VALUE}。

回答by BlackGaff

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

此使用中的 beanshell 和 JavaScript 函数将失败,因为它们不会导入您需要的包以使用.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.

如果您确实需要使用函数来转换大小写(而不是首先将它们声明为小写),则可能需要编写完整的 beanshell 后处理器脚本以导入所需的包。