Reputation: 55
I am currently working on a jmeter JDBC scripts which involves executing huge queries on the DB to setup data for test.
Its a bit of a complex script where I need to validate and extract specific filed from the result set and then write it all in the file and use in the next thread group
I written a Beanshell code as below to validate a particulat condition
FileWriter fWriter = new FileWriter ("C:/UserPath/Number.csv", true);
BufferedWriter buff = new BufferedWriter(fWriter);
if (${JMeterThread.last_sample_ok} == true)
{
buff.write(vars.get("p_PI_ID") + "," + vars.get("p_AC_Number"));
buff.write(System.getProperty("line.separator"));
log.info(vars.get("Update_Assertion"));
}
buff.close();
The condition is I want the data to be written to the file when the JMeterThread.last_sample_ok= true
I have tried various combination to write this up but this code write to the file no matter what the condition is. So far I have tried the below conditions
if (${JMeterThread.last_sample_ok} == true)
if (${JMeterThread.last_sample_ok} != false)
if (!${JMeterThread.last_sample_ok} == false)
I did also try to use some other logics to no success, the thing is this code works fine with other validation I am doing, not sure what is wrong here
I have also tried to place the beanshell post processor outside the JDBC sample which should then capture the state of the sampler execution as True or false.
Thanks for reading though.
Upvotes: 2
Views: 294
Reputation: 168002
Don't inline JMeter Functions or Variables into scripts, either use Parameters section or go for code-based equivalents instead. So change this line:
if (${JMeterThread.last_sample_ok} == true)
to this one:
if (vars.get("JMeterThread.last_sample_ok").equals("true"))
where vars
stands for JMeterVariables class instance, see Top 8 JMeter Java Classes You Should Be Using with Groovy article for more information on this and other JMeter API shorthands available for the JSR223 Test Elements.
Another option is put this variable into "Parameters" section and go for if (Parameters.equals("true"))
expression:
And last but not the least since JMeter 3.1 you're supposed to use JSR223 Test Elements and Groovy language for scripting so it might worth considering migrating.
Upvotes: 1
Reputation: 1821
You may try with
if (prev.isSuccessful()){
}
BTW it is recommended to switch to JSR223 Elements from Beanshell.
Upvotes: 1