How to print the values of variables in a JMeter bean shell assertion

42,181

Solution 1

You have a problem in your script. In Beanshell you cannot access variables like ${Request_Configuration}, you need to use vars.get("Request_Configuration") instead.

vars is a shorthand for JMeterVariables class instance for current context.

So your Beanshell Assertion code should look as follows:

String req_data=vars.get("Request_Configuration");
String res_data=vars.get("mongo_db_Configuration");


if(res_data.equalsIgnoreCase(req_data)){
   Failure=false;
   FailureMessage = "Data stored in DB is correct";
   System.out.println("ReqData="+req_data);
   System.out.println("ResData="+res_data);
}
else{
   Failure = true;
   FailureMessage = "Data Stored in DB is NOT correct";
   System.out.println("ReqData="+req_data);
   System.out.println("ResData="+res_data);
}

I would also suggest using log.info() instead of System.out.println() as in that case results will go to jmeter.log file and won't be "eaten" by exceeding screen buffer size.

See How to use BeanShell: JMeter's favorite built-in component guide for more information on Beanshell scripting and various JMeter API objects exposed to Beanshell explanation.

Solution 2

Use log.info()

Example

log.info("myVariable: " + vars.get("myVariable"));

My use case:

I did use the following code snipped in a BeanShell Assertion within my HTTP Request-sampler to print out my three variables id, type and value:

log.info(Thread.currentThread().getName()+": " + SampleLabel + ": id: " + vars.get("id"));
log.info(Thread.currentThread().getName()+": " + SampleLabel + ":  +-type: " + vars.get("type"));
log.info(Thread.currentThread().getName()+": " + SampleLabel + ":  +-value: " + vars.get("value"));

Printing also the built-in SampleLabel variable gives you the hint from which sampler you logged this information.

Share:
42,181
Udhay
Author by

Udhay

Updated on February 14, 2022

Comments

  • Udhay
    Udhay over 2 years

    Using Jmeter, I'm passing values to a webservice through a REST API. On Success the API updates the values to a mongo DB. While Asserting using JMeter BeanShell Assertion..I want to display the values sent in the Request and values Stored in the DB.

    Im Using the below Script..

    String req_data="${Request_Configuration}";
    String res_data="${mongo_db_Configuration}";
    
    
    if(res_data.equalsIgnoreCase(req_data)){
       Failure=false;
       FailureMessage = "Data stored in DB is correct";
       System.out.println("ReqData="+req_data);
       System.out.println("ResData="+res_data);
    }
    else{
       Failure = true;
       FailureMessage = "Data Stored in DB is NOT correct";
       System.out.println("ReqData="+req_data);
       System.out.println("ResData="+res_data);
    }
    

    Im Just not able to Print ReqData and ResData. Please help out.