I have a setup thread group and I am trying to access a system property. Currently I have the following in my BeanShell...
String filename = System.getenv().get("server.portal.articleFile");
System.out.println(filename);
I have also tried...
String filename = props.get("server.portal.articleFile");
All of these seem to output null. When I check the debug sampler I see the server.portal.articleFile property set correctly under system properties.
How do I access these system properties from a beanshell sampler in a setup thread group.
This seems to work...
import org.apache.jmeter.util.JMeterUtils;
String filename = JMeterUtils.getProperty("server.portal.articleFile");
Related
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.
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
Is there a way in Groovy Code to get the Processor Group Name the ExecuteScript Processor is in and Processor Name of the ExecuteScript Processor the Groovy Code is in. If so what would the code be. Any help would be greatly appreciated.
To get the processor name, use ProcessContext#getName(). The ProcessContext class is referenceable from ExecuteScript via the provided variable context, so the code would be String processorName = context.getName().
To get the process group name, I am not aware of an easy way through the framework code. You can, of course, use the Apache NiFi REST API to request the list of process groups and iterate through, checking to see if the process group contains a processor with the identifier of the current processor.
to get the name of all the processors and process groups name, you can use
the following code.
final EventAccess access = context.getEventAccess();
final ProcessGroupStatus procGroupStatus = access.getControllerStatus();
procGroupStatus.getProcessGroupStatus();
final ProcessorStatus processorstatus = procGroupStatus.getProcessorStatus()
ProcessorStatus class contains getName method, which can be used to get the name other processor.
Below is the source code of the same class for your reference.
https://github.com/apache/nifi/blob/master/nifi-api/src/main/java/org/apache/nifi/controller/status/ProcessorStatus.java
Im new in load and performance testing so could anyone explain me, what is difference between vars object and props object in JMeter beanshell script.
Im also bit confuse about JMeter variable and properties.
Thanks.
The most simplest explanation would be that variables(vars) are not shared between threads, and properties(props) ARE shared.
Usage:
vars - ( JMeterVariables) - gives read/write access to variables:
vars.get(key);
vars.put(key,val);
vars.putObject("OBJ1",new Object());
vars.getObject("OBJ2");
props - (JMeterProperties - class java.util.Properties):
props.get("START.HMS");
props.put("PROP1","1234");
You can refer to this link to get more info on vars and props.
As per Blazemeter Blog
vars
vars (JMeter variables) is the most frequently used component. It’s an
instance of the org.apache.jmeter.threads.JMeterVariables class and
provides read/write access to current variables, is capable of
enumerating/changing existing variables, creating new ones, and
obtaining nested properties. All JMeter variables are Java strings.
If you need to put something else to a JMeter variable, you’ll need to
cast it to the string first. The following code snippet demonstrates
how to save previous sampler response data into a JMeter variable.
byte [] samplerdata = ctx.getPreviousResult().getResponseData();
String samplerdatastring = new String(samplerdata);
vars.put("samplerdata",samplerdatastring);
props
Basically, this is the same as “vars,” but it exposes JMeter
properties instead. See JavaDoc on java.util.Properties and JMeter
documentation on JMeter properties for more information. The primary
distinction between props and vars is that props have a “global”
scope, whereas the scope of “vars” is limited to the current thread
group.
Refer to this link.
From a JSR223 Sampler, I can get access to the current test element using the sampler variable.
From there, how can I navigate the tree of TestElement objects? For example, how can I get access to the parent test element (and then it’s parent, etc) or how can I get access to the TestPlan test element?
Background:
I want to dynamically create a JDBC Connection Configuration element from a JSR223 Sampler using Groovy.
From other questions (e.g., here) and web searches (e.g., here), I know how to create test plan elements from the top down (e.g., how to create a test plan and build the tree down from there). So I know how to do the new DataSourceElement() which is a TestElement but I don’t know how to add that new element to the test plan. In the sampler script I have access to the sampler (Sampler) and the ctx (JMeterContext) variables but I don’t know how to navigate the test element tree.
I tried just using sampler.addTestElement but a config element isn’t really valid under a sampler element. Still, I did try but the config element was not found when I tried to use it in a JDBC Request (error: "No pool found named: 'myDatabaseThreadPool', ensure Variable Name matches Variable Name of JDBC Connection Configuration").
I’m hoping that if I can get the TestPlan element and add the config element to that, then it would work.
FWIW, my test plan looks like this:
Test Plan
Thread Group 1 (could be a setup thread group)
JSR223 Sampler (this is where I want to create the dynamic config)
Thread Group 2 (multiple threads)
JDBC Request (uses the pool variable name specified in the dynamic config)
View Results Tree
I can go into further detail about why I want to dynamically create the JDBC Connection Configuration, but if there’s an easy answer about how to navigate the test element tree from inside my sampler script I’d like to know that anyway.
As you have mentioned you have access to JMeterContext via ctx shorthand. Hence you have access to StandardJMeterEngine class instance via ctx.getEngine(); method.
Looking into StandardJMeterEngine source you can see that test plan is being stored as HashTree structure:
private HashTree test;
So the choices are in:
change access modifier to public and recompile JMeter from sources
use Trail - Java Reflection API in order to access test value
Reference code:
import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.testelement.TestPlan;
import org.apache.jorphan.collections.HashTree;
import org.apache.jorphan.collections.SearchByClass;
import java.lang.reflect.Field;
import java.util.Collection;
StandardJMeterEngine engine = ctx.getEngine();
Field test = engine.getClass().getDeclaredField("test");
test.setAccessible(true);
HashTree testPlanTree = (HashTree) test.get(engine);
SearchByClass testPlans = new SearchByClass(TestPlan.class);
testPlanTree.traverse(testPlans);
Collection testPlansRes = testPlans.getSearchResults();
TestPlan testPlan = (TestPlan)testPlansRes.toArray()[0];
//do what you need with "testPlanTree" and/or "testPlan"
Check out How to Use BeanShell: JMeter's Favorite Built-in Component guide for more information using JMeter and Java API from scripting test elements.