在JMeter和BeanShell中,如何将变量变为小写?

发布于 2024-10-13 00:27:56 字数 388 浏览 10 评论 0原文

在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

感性 2024-10-20 00:27:56

自我提醒一下。

事实证明,它是 BeanShell Sampler 中的两行命令,而不是 __BeanShell 命令。不幸的是,并不完全在示例中。

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

首先在一些用户参数等中定义一个变量
即:

Name: my_initial_reference 
Value: ITS IN CAPS

在用户首选项或定义列表下添加一个 Bean Sampler(接下来,它不是子进程)

放入:

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

现在在名称/值对下,我可以将 ${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:

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.

分开我的手 2024-10-20 00:27:56

只需添加一个函数即可。

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

注意:VAL 可以是相关值或参数化值(er VAL= TO LOWER 或 VAL= TO UPPER)。我们可以在beanshell(预处理器/后处理器/采样器)中使用这个函数。 Jmeter版本使用(2.6)。

可以在脚本中的任意位置使用它作为 ${VALUE}。

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

诠释孤独 2024-10-20 00:27:56

${__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 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())}

一梦浮鱼 2024-10-20 00:27:56

嗯,你的 bean shell 代码对我不起作用。 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

我添加了两个双引号来解决:

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

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
执手闯天涯 2024-10-20 00:27:56

此用途中的 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.

一抹苦笑 2024-10-20 00:27:56

var blah = "${my_initial_reference}";

blah.toLowerCase();

var blah = "${my_initial_reference}";

blah.toLowerCase();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文