简体   繁体   English

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

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

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} .这样${my_lowercase_variable}是小写的${my_variable} Tried with quote and without and escaping and such.尝试使用报价,没有和转义等。 No luck.没运气。 Any tricks or tips welcome.欢迎任何技巧或提示。

${__javaScript('${foobar}'.toLowerCase())} does work. ${__javaScript('${foobar}'.toLowerCase())}确实有效。 If the output is ${foobar} instead of desired value , it means that the variable has not been declared如果输出是${foobar}而不是desired value ,则表示该变量尚未声明

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 eg:变量不能在单个“用户定义变量”组件中重复使用,例如:

在此处输入图片说明

The second row in that image will not be able to refer to the variable my_variable in the first row.该图像中的第二行将无法引用第一行中的变量my_variable 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, eg:第一个变量将在第一个组件中,第二个变量将在第二个组件中,例如:

在此处输入图片说明

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. ${__BeanShell( " ${my_variable} " .toLowerCase())}也有效。 (Note that Bean Shell requires double quotes. The code in your question uses single quotes.) (请注意,Bean Shell需要双引号。您问题中的代码使用单引号。)

Another way is to use vars.get :另一种方法是使用vars.get

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

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

Note to self.注意自我。

It turns out to be a two liner in BeanShell Sampler rather than a __BeanShell command.结果证明是 BeanShell Sampler 中的两行代码,而不是 __BeanShell 命令。 Not exactly in the examples unfortunately.不幸的是,并不完全在示例中。

I added the BeanShell Sampler under the Thread Group, then made a variable.我在Thread Group下添加了BeanShell Sampler,然后做了一个变量。 No parameters in the form were required only the two liner script below.表单中没有参数只需要下面的两个 liner 脚本。 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.只要我不更改变量,我就可以将数据复制到另一个变量,改为更改该变量,然后在需要的地方对该变量进行 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 大写字母,这是我想要使用的。

Simply can add a function简单的可以添加一个功能

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

Note: VAL can be correlated or paramiterized value (er VAL= TO LOWER or VAL= TO UPPER).注意:VAL 可以是相关值或参数化值(er VAL= TO LOWER 或 VAL= TO UPPER)。 We can use this function in beanshell (pre-processor/post-processor/sampler).我们可以在 beanshell (pre-processor/post-processor/sampler) 中使用这个函数。 Jmeter version using (2.6). Jmeter 版本使用 (2.6)。

Can use it where ever we want in the script as ${VALUE}.可以在脚本中任何我们想要的地方使用它作为 ${VALUE}。

Hmmmm, your bean shell code didn't work for me.嗯,你的 bean shell 代码对我不起作用。 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 .此使用中的 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 后处理器脚本以导入所需的包。

var blah = "${my_initial_reference}"; var blah = "${my_initial_reference}";

blah.toLowerCase(); blah.toLowerCase();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM