JMeter update user defined variable at run time - jmeter

I am trying to update the user defined variable set at a test plan level from a thread group. This is my scenario:
Test plan> user defined variable (variable name/value: fBurst=0)
Test plan> Threadgroup1 > Once only controller> JSR223 test plan (inside the test plan I have the following code)
log.info("fBurst user defined value : " + ${fBurst})
vars.put("fBurst", Integer.toString(111))
log.info("fBurst user defined value vars.put' : " + ${fBurst})
props.put("fBurst", 222)
log.info("fBurst user defined value props.put' : " + ${fBurst})
What I am trying to do here is to update the fBurst user defined value from inside the once only controller sampler and so far not been able to do it using the vars.put or props.put. Is there another way to do this?

Take a look at JSR223 Sampler documentation:
The JSR223 test elements have a feature (compilation) that can significantly increase performance. To benefit from this feature:
Use Script files instead of inlining them. This will make JMeter compile them if this feature is available on ScriptEngine and cache them.
Or Use Script Text and check Cache compiled script if available property.
When using this feature, ensure your script code does not use JMeter variables or JMeter function calls directly in script code as caching would only cache first replacement. Instead use script parameters.
So replace ${fBurst} with vars.get('fBurst") or props.get('fBurst') correspondingly and it should start working as expected
More information: Top 8 JMeter Java Classes You Should Be Using with Groovy

Related

Jmeter Debug Sampler | How to save all variable values to CSV

I am able to extract values for many variables and are showing in Debug Sampler.
Is there any way to save these all variable values to a CSV file?
I found a solution (using BeanShell script) to save multiple Jmeter variable to CSV but I want all variables values to a single CSV, so that I can use the CSV file for next thread run.
Here is the snapshot of one of the Debug Sampler:
enterCompanyname=APITENANT
CreateTenant_Status=Success
CreateTenant_Status_matchNr=1
Current_UTC_Time=2018-03-07T01:53:18.310Z
DB_DataSource=dev4574857
DB_Password=1234
DB_UserName=web
DeviceCount=19
DevicesPerUser=94
EXCELPATH=X:\QualityAssurance\XLSX_3 columns_1000 rows.xlsx
Email=apitenant#apitenant.com
EndDate=2018-12-31
Exist=false
Exist_matchNr=1
FirstName=API
JMeterThread.last_sample_ok=true
JMeterThread.pack=org.apache.jmeter.threads.SamplePackage#69ab73cf
LastName=TENANT
LicensePlan=Pro
LicenseType=Device
MaxUsers=11
Password=Password
Protocol=http
RandomNumber=10
Add JSR223 Sampler to your Test Plan (where you want variables to be saved)
Put the following code into "Script" area:
def csv = new File('vars.csv')
vars.entrySet().each {var ->
csv << var.key + '=' + var.value + System.getProperty('line.separator')
}
That's it, you will have vars.csv file created in JMeter's "bin" folder having all variables listed. You might also want to replace = with , for better CSV Data Set Config compatibility.
vars is a shorthand to JMeterVariables class instance, it provides read/write access to all JMeter Variables.
Also be aware that starting from JMeter 3.1 users are encouraged to switch to JSR223 Test Elements and Groovy language so consider migrating to Groovy as soon as it will be possible. See Apache Groovy - Why and How You Should Use It for more details.

Listing every one of the variables stored in JMeterVariables

I'm making extensive use of JMeterVariables/vars in my current JMeter code- in particular I'm generating variable names dynamically, based on JSON input.
Is there any way of getting the program to kick out a list of every single variable that is stored in vars??
Thanks!
You can do one of the following:
Add Debug Sampler in your script, with JMeter Variables = true
Similarly, add Debug Post-Processor under the sampler which creates dynamic variables
Since vars is just a regular Map, you can iterate it using one of the scripting languages (BeanShll, Groovy, etc):
for(java.util.Map.Entry<java.lang.String,java.lang.Object> var in vars.entrySet())
{
log.info(var.getKey() + "=" + var.getValue().toString());
}
Note that some variables are objects, so their representation will depend on how toString() function is implemented for them.
Add JSR223 Sampler to your Test Plan
Put the following code into "Script" area:
vars.entrySet().each { var ->
log.info(var.getKey() + '=' + var.getValue())
}
All the variable names and values will be printed to jmeter.log file
More information: Apache Groovy - Why and How You Should Use It

How to set and get environment variables in Jmeter to test API's

I need to set and get variables in Jmeter for API automation.
I am using the groovy script for same.
I have achieved same using code as below:
import org.apache.jmeter.util.JMeterUtils;
JMeterUtils.setProperty("PC_CREATED_PROMO_CODE", "shubhamvalue");
log.info("will it work? ="+JMeterUtils.getProperty("PC_CREATED_PROMO_CODE"))
Now the problem is I am not able to see the value in any contanier where I can set my hardcode values like token, baseURL, Headers. it should be similar we do in SOAP-UI or postman tests.
Please let me know if I can see these setProperty values in file/section/container in Jmeter.
Or suggest me any other workaround which is more feasible for same.
Any workaround will be helpful and appreciated in advance.
If you need to get and set variables I would recommend using vars shorthand
As per 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.
So I would suggest setting variables as: vars.put('foo', 'bar') and accessing them as ${foo} where required as my expectation is that you will be getting different PC_CREATED_PROMO_CODE for each thread (virtual user)
Also be aware that it is also recommended to avoid scripting where possible so consider going for JSON Extractor instead.
To view properties in file/section you can use 2 functions __property or__P while the second will return 1 as default. in your case
${__property(PC_CREATED_PROMO_CODE)}
${__P(PC_CREATED_PROMO_CODE)}
For example you can change next sampler name to Post2 ${__property(PC_CREATED_PROMO_CODE)}
BTW you can set property use props instead
props.setProperty("PC_CREATED_PROMO_CODE", "shubhamvalue" )
Example of use in HTTP Header Manager, adding cotentType from property:
1) In JMeter GUI mode, under WorkBench, create Property Display by
WorkBench > Add > Non-Test Elements > Property Display.
Then select JMeter Properties checkbox to view all the exist properties
props.put("shubhamKey", "shubhamValue")
When you execute this code the property will set in a property file and you can see it in below location:
WorkBench > Add > Non-Test Elements > Property Display.
2) Now if you are want to use User Defined Variables in your scripts you can call value like below:
vars.get("shubhamUserKey")
Still looking to set the value from code in User Defined Variables

How to create simple counter using Beanshell?

I'm trying to create a simple counter that will print the iteration number to the log.
the problem is that I didn't find a way to initialize the int value of i to 0.
if I'll do it inside the Beanshell script it will keep initializing, I need it to run only once at the beginning of the test.
My code:
int i=0;
log.info(string.valueOf(i));
i=i+1;
Add Once Only Controller, under it JSR223 Sampler with the initialization
vars.putObject("i", 0);
Then you can increment it after it (not under the Controller) with other JSR223 Sampler:
myI = vars.getObject("i")
log.info(String.valueOf(myI));
vars.putObject("i", ((Integer)myI+1));
It is recommended to avoid scripting where possible, and if you cannot live without scripting you should be using the most performing option which is JSR223 Test Elements and Groovy language.
Particularly your case can be implemented without any scripting, you can use the following JMeter Functions:
__log() - which prints an arbitrary message to jmeter.log file
__iterationNum() - which returns the number of current iteration
So if you use the statement like: ${__log(Current iteration is: ${__iterationNum},,,)} JMeter will return it where the function is called and additionally print the corresponding message to the log file.
Demo:
You can install __iterationNum() function as a part of Custom JMeter Functions bundle using JMeter Plugins Manager

Can we control Jmeter's components through Beanshell? I want to disable all assertions through one flag. how can I do it?

Can we control Jmeter's components through Beanshell? I want to disable all assertions through one flag. How can I do it?
If any other solution than beanShell then let me know.
The easiest way is running your JMeter test using Taurus tool as a wrapper, it naturally supports JMeter tests, moreover it provides some nice extensions.
Particular in your case you can use Modifications for Existing Scripts functionality which allows enabling or disabling Test Elements
---
scenarios:
modification_example:
script: /your/jmeter/testplan.jmx
modifications:
disable: # Names of the tree elements to disable
- Response Assertion
- Duration Assertion
I see two ways in addition to previous answers:
1) Wrap assertions into If controllers, then - yes, set a flag var & check at the If block.
2) Run JMeter programmatically through JMeter API - here you'd have programmatic access to each and every element in the TestPlan.
Although that way is documented quite poorly while the API model is far from being clear itself.
UPD: some clues for the way of doing the latter
1) Here's the main reference: http://jmeter.apache.org/api/index.html
2) Instantiate engine and load properties:
StandardJMeterEngine jmeter = new StandardJMeterEngine();
JMeterUtils.loadJMeterProperties("/path/to/jmeter.properties");
3) Instantiate SaveService and load your plan (yes, save service is what resposible for that)
SaveService.loadProperties();
File yourplan = new File("/path/to/yourplan.jmx");
HashTree planTree = SaveService.loadTree(yourplan);
4) Here's the point where you can access & work your plan elements, going through the HashTree, retrieving test elements in sub-hashtrees (for elements, see the reference mentioned in p.1) & changing them and/or the test structure (cast to TestElement must be good enough for enabling/disabling).
5) As you got done with it, the rest is straightforward:
jmeter.configure(planTree);
jmeter.run();
That should be pretty much it.
From my knowledge, you can not disable all Assertions in the Test Plan by using BeanShell
The work around is as follows:
Create a variable as processAssertions in User Defined Variable config element.
Keep All Assertions inside If Controllers.
Add condition as ${processAssertions}==true, so Assertions will be evaluated ONLY when you set the processAssertions value to true. Set any value other than true, to make JMeter to ignore Assertions.
Using Bean Shell Assertion:
Pre-condition: create processAssertions (Set to TRUE) in User Defined Variables
import org.apache.jmeter.assertions.ResponseAssertion;
log.info("hello");
try{
ResponseAssertion obj = new ResponseAssertion();
if(${processAssertions}==TRUE) { // value accessed from UDV
log.info("inside if");
obj.Enabled=false;
SampleResult.setSuccessful(true); // set sample result to PASS, set to false to mark it failure.
}
} catch(Exception e) {
}
If Controller - with assertions:
If Controller - without assertions:
References:
https://www.blazemeter.com/blog/how-use-jmeter-assertions-3-easy-steps
http://jmeter.apache.org/usermanual/component_reference.html#assertions

Resources