JMeter - 在用户变量上使用子字符串

发布于 2024-10-04 08:01:30 字数 247 浏览 0 评论 0原文

使用 jmeter,我有一个从 CSV 文件传递​​的变量(使用 CSV 数据集配置),我想在 http 请求中使用它的子字符串。

IE 变量TIME=23:40,请求参数是小时和分钟,所以我想在HTTP请求中提取适当的部分。

我读到你可以使用 javascript,所以在这个小时里,我尝试了 ${TIME}.substring(0,2) ,它看起来好像不起作用,而且果然不起作用。 t。

我该怎么做?

Using jmeter, I have a variable passed from CSV file (using CSV Data Set Config), and I'd like to use a substring of it in an http request.

i.e.
variable TIME=23:40, request paramaters are hour and minute, so I want to extract appropriate parts, in the HTTP Request.

I read you could use javascript, so for the hour, I tried ${TIME}.substring(0,2) , which didn't look as though it would work, and sure enough it didn't.

How do I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

何以笙箫默 2024-10-11 08:01:30

您可以通过内联调用 javascript 函数 http://jmeter.apache.org/usermanual/functions 来做到这一点。例如

${__javaScript('${TIME}'.substring(0\,2))}

或者

  1. 创建用户定义的变量示例
  2. 创建名为 myTime 的变量(或任何您想要的)
  3. 创建 beanshell 采样器,然后选择 beanshell 作为其中的语言:

    String tempTime = vars.get("myTime");
    String newTime = tempTime.substring(0,2);     
    vars.put("newTime", newTime);
    

在请求中使用 ${newTime} 变量

根据其他答案进行编辑。逗号需要加引号。

You can do that by calling javascript function inline http://jmeter.apache.org/usermanual/functions.html

Ex :

${__javaScript('${TIME}'.substring(0\,2))}

Or

  1. create user defined variables sample
  2. create variable called myTime(or anything you want)
  3. create beanshell sampler, choose beanshell as language in it then:

    String tempTime = vars.get("myTime");
    String newTime = tempTime.substring(0,2);     
    vars.put("newTime", newTime);
    

use ${newTime} variable in your request

Edited according to the other answer. Comma needs to be quoted.

黄昏下泛黄的笔记 2024-10-11 08:01:30

您无需使用 substring 函数:

${__substring(${TIME}, 0, 2)}

You can do it without calling Javascript interpreter (Rhino) with substring function from JMeter plugins:

${__substring(${TIME}, 0, 2)}
凡间太子 2024-10-11 08:01:30

确保在 javascript 函数中转义逗号:

${__javaScript('${TIME}'.substring(0\,2))}

Make sure you escape commas in javascript functions:

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