在JMeter和BeanShell中,如何将变量变为小写?
在JMeter的用户参数中,如何使变量小写?
左列
my_lowercase_variable
右列
${__BeanShell('${my_variable}'.toLowerCase())} //fails
或
${__javaScript('${my_variable}'.toLowerCase())} //fails
使 ${my_lowercase_variable}
为 ${my_variable}
的小写形式。尝试使用引号和不使用转义等。运气不好。欢迎任何技巧或提示。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
自我提醒一下。
事实证明,它是 BeanShell Sampler 中的两行命令,而不是 __BeanShell 命令。不幸的是,并不完全在示例中。
我在线程组下添加了BeanShell Sampler,然后创建了一个变量。表单中不需要任何参数,仅需要下面的两个衬垫脚本。只要我不更改变量,我就可以将数据复制到另一个变量,更改它,然后在需要的地方对其进行值引用。
首先在一些用户参数等中定义一个变量
即:
在用户首选项或定义列表下添加一个 Bean Sampler(接下来,它不是子进程)
放入:
现在在名称/值对下,我可以将 ${blah} 作为值映射到任何进程名称需要的值。
请注意,调试响应仍将显示大写字母中的初始值,但您还会在大写字母中看到 blah=its,这就是我想要使用的。
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:
Add a Bean Sampler under the User Preferences or definition list (just next, it's not a child process)
Put in:
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.
只需添加一个函数即可。
注意:VAL 可以是相关值或参数化值(er VAL= TO LOWER 或 VAL= TO UPPER)。我们可以在beanshell(预处理器/后处理器/采样器)中使用这个函数。 Jmeter版本使用(2.6)。
可以在脚本中的任意位置使用它作为 ${VALUE}。
Simply can add a function
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())}
确实有效。如果输出是${foobar}
而不是desired value
,则表示该变量尚未声明请注意,变量仅在之后定义“用户定义变量”组件已解析。变量不能在单个“用户定义变量”组件中重复使用,例如:
该图像中的第二行将无法引用第一行中的变量
my_variable
。为了能够引用第一个变量,需要两个“用户定义变量”组件。第一个变量将位于第一个组件中,第二个变量将位于第二个组件中,例如:这样,
${my_lower_case_variable}
将成功转换为一些值。
${__BeanShell("${my_variable}".toLowerCase())}
也有效。 (请注意,Bean Shell 需要双引号。您问题中的代码使用单引号。)另一种方法是使用
vars.get
:${__javaScript(vars.get('my_variable').toLowerCase())}
${__BeanShell(vars.get('my_variable') ).toLowerCase())}
${__javaScript('${foobar}'.toLowerCase())}
does work. If the output is${foobar}
instead ofdesired value
, it means that the variable has not been declaredNote 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.:
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.:With that,
${my_lower_case_variable}
will successfully be converted intosome 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())}
嗯,你的 bean shell 代码对我不起作用。 bean shell 采样器返回:
我添加了两个双引号来解决:
Hmmmm, your bean shell code didn't work for me. The bean shell sampler returned:
I added two double quotes to solve it:
此用途中的 beanshell 和 JavaScript 函数将会失败,因为它们没有导入使用
.toLowerCase
所需的包。如果您确实需要使用函数来转换大小写(而不是首先将它们声明为小写),您可能需要编写完整的 beanshell 后处理器脚本才能导入所需的包。
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.
var blah = "${my_initial_reference}";
blah.toLowerCase();
var blah = "${my_initial_reference}";
blah.toLowerCase();