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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4266018/
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-08-23 11:39:13  来源:igfitidea点击:

JMeter - using substring on a user variable

javascriptsubstringjmeter

提问by Joe Watkins

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.

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

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

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

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.

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

How do I do this?

我该怎么做呢?

回答by ant

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

您可以通过内联调用 javascript 函数来做到这一点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);
    
  1. 创建用户定义的变量示例
  2. 创建名为 myTime 的变量(或任何你想要的)
  3. 创建 beanshell 采样器,选择 beanshell 作为其中的语言,然后:

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

use ${newTime}variable in your request

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

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

根据另一个答案编辑。逗号需要引用。

回答by Andrei Botalov

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

您可以在不使用JMeter 插件中的substring函数调用 Javascript 解释器(Rhino)的情况下执行此操作:

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

回答by Andrei Botalov

Make sure you escape commas in javascript functions:

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

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