plaidshirt
plaidshirt

Reputation: 5671

Get empty response data when variable not defined

I try to use following code to check response of a HTTP Request, which is called from another Thread Group by a Module Controller.

File myfile = new File(FileServer.resolveBaseRelativeName("results/" + filen + "-report.xml"));

if(${__isVarDefined(vars.get("myvar"))} == true){
        FileUtils.writeByteArrayToFile(myfile,bytes);
} else {
        FileUtils.writeStringToFile(myfile, prev.getResponseDataAsString(), "UTF-8");
}

Empty xml is created when myvar variable is empty, it doesn't contains response data of request.

Upvotes: 0

Views: 1386

Answers (2)

Dmitri T
Dmitri T

Reputation: 167992

  1. You cannot use vars shorthand to retrieve values from the different Thread Group as JMeter Variables are local to the Thread hence they cannot be accessed from another Thread Group. You need to use props instead. See the documentation:

    Properties are not the same as variables. Variables are local to a thread; properties are common to all threads, and need to be referenced using the __P or __property function.

  2. Don't inline JMeter Functions or Variables in Groovy scripts, if you need to check whether variable is defined or not - attempt to get it and see if it is null:

    if (vars.get('myvar') != null) {
        //the code will be executed if the var is defined
    }
    

Check out Transferring Data and Objects (like List, Maps, Array etc.) Between Samplers and Threads chapter of The Groovy Templates Cheat Sheet for JMeter for more details.


In the absolute majority of cases it's easier to use Inter-Thread Communication Plugin

Upvotes: 1

Ori Marko
Ori Marko

Reputation: 58774

You can write byte[] in both cases using getResponseData() (and removed function call)

if(vars.get("myvar") ! null){
    FileUtils.writeByteArrayToFile(myfile, bytes);
} else {
    FileUtils.writeByteArrayToFile(myfile, prev.getResponseData());
}

Upvotes: 1

Related Questions