如何在JMeter中从全局变量获取价值

时间:2018-08-27 16:04:52

标签: jmeter

我正在尝试从响应代码中的全局变量获取值。我已经尝试过正则表达式和Beanshell代码,但是仍然将值设为null。有人可以帮我吗?

我的beanshell代码:

import org.apache.jmeter.services.FileServer;
String path=FileServer.getFileServer().getBaseDir();
String sid= vars.get("whiteboardVars");
FileOutputStream f = new FileOutputStream("/Users/diya/testgui/sesid.csv", true); //spec-ify true if you want to overwrite file. Keep blank otherwise.
p = new PrintStream(f); 
this.interpreter.setOut(p); 
p.println(sid);
f.close();

Debug sampler output- whiteboardVars = 0 here

Search of whiteboardVars which exists in the response data of the http request. My main target is to get the sessionId from the whiteboardVars variable

1 个答案:

答案 0 :(得分:0)

  1. 使用Debug Sampler and View Results Tree listener组合检查您的whiteboardVars变量是否确实具有该值。如果未定义变量,则尝试将其写入文件将没有任何意义,因此请仔细检查其是否具有该值。
  2. 请注意,starting from JMeter 3.1 it is recommended to use JSR223 Test Elements and Groovy language适用于任何形式的脚本。您可以将代码缩短到这一行:

    new File('/Users/diya/testgui/sesid.csv').newWriter().withWriter { w -> w << vars.get('whiteboardVars') }
    

    ,它将更快。