【问题标题】:Jmeter HTTP setPath - How do i set dynamic path in http samplerJmeter HTTP setPath - 我如何在 http 采样器中设置动态路径
【发布时间】:2020-05-04 23:43:27
【问题描述】:

我是 jmeter 的新手,我陷入了困境。我正在使用 csv 文件运行 jmeter http 示例,在该文件中我正在使用预处理器脚本,其中执行以下操作-

  1. 根据请求设置 http.method,如 POST 、 GET 、 PUT 、PATCH
  2. 将 http.path 设置为“/restpath”
  3. 设置请求body.json

对于路径,我使用“http.path”并在 http 采样器中传递相同的内容,如 ${http.path}。在我有如下用例的依赖 HTTP 请求之前,以上所有内容都可以正常工作 -

第 1 步 - 在路径 "/restpath" 上点击 HTTP 请求。成功提交后,我得到一个 serverId 作为响应并将其保存在 json 提取器变量“serverID”中。

第 2 步 - 现在我必须在路径 "/rest/${serverID}" 上点击 http 请求。当我尝试从 csv 文件传递​​相同的内容时,相同的内容不会在步骤 1 中替换为捕获的变量。我在 http.path 中的 csv 文件中传递的任何内容都已传递并提交。

期待 - 在工作表中 --> http.path == "/restpath/${serverID}" 在 HTTP 请求提交中 --> "/restpath/4894rh89r"

实际 - 在工作表中 --> http.path == "/restpath/${serverID}" 在 HTTP 请求提交中 --> "/restpath/${serverID}"

import org.apache.jmeter.engine.util.CompoundVariable;
import org.apache.jmeter.protocol.http.util.HTTPArgument;

//test input parameters
def httpMethod = vars.get("http.method");

//change the http method as per the data sheet
sampler.setMethod(httpMethod);

//set the HTTP Path
//URL url = new URL(vars.get('http.path'));
//vars.put('http.path',url.getPath());

//set the HTTP request body
if(!vars.get("input.json").equals("")){
    def dataToBePosted = new CompoundVariable(new File(vars.get("jmeter.test.home") + vars.get("input.json")).text).execute();
    def arg= new HTTPArgument("", dataToBePosted, null, true);
    arg.setAlwaysEncoded(false);
    sampler.getArguments().addArgument(arg);

    //HTTP Body is set to
    log.info(dataToBePosted);
}

使用 __V() 函数尝试了几次 hit n 试验,但没有运气:(

【问题讨论】:

    标签: groovy jmeter


    【解决方案1】:

    我认为您需要更改此行:

    def dataToBePosted = new CompoundVariable(new File(vars.get("jmeter.test.home") + vars.get("input.json")).text).execute();
    

    到这个:

    def dataToBePosted = ((org.apache.jmeter.engine.util.CompoundVariable)([new File(vars.get("jmeter.test.home") + vars.get("input.json")).text])).execute()
    

    演示:

    更多信息:

    【讨论】:

    • 感谢您查看此内容。我只需使用${__eval(${http.path})} 函数就得到了解决方案:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多