Ilya Zinkovich
Ilya Zinkovich

Reputation: 4410

Passing -J parameters programmatically to JMeter

I'm using programmatic way to run JMeter defined in the step 4 of this post. The code looks as follows:

final StandardJMeterEngine jmeter = new StandardJMeterEngine();
JMeterUtils.setJMeterHome(getAbsolutePath("/jmeter"));
JMeterUtils.loadJMeterProperties(getAbsolutePath("/jmeter/bin/jmeter.properties"));
JMeterUtils.initLocale();
try {
    SaveService.loadProperties();
    final File jmeterConfig = new File(getAbsolutePath(pathToJmx));
    final HashTree testPlanTree = SaveService.loadTree(jmeterConfig);
    jmeter.configure(testPlanTree);
} catch (final IOException e) {
    throw new JMeterConfigurationException(e);
}
jmeter.run();

I want to provide the values for ${__P(parameter_name)} parameters I specified in .jmx file, that can be done using -J parameter in console.

How can I pass values for this parameters in the code above?

Upvotes: 0

Views: 1192

Answers (1)

Dmitri T
Dmitri T

Reputation: 168002

Given you already use JMeterUtils class you should be able to call JMeterUtils.setProperty() function like:

JMeterUtils.setProperty("parameter_name","foo");

And then in your script refer the property using __P() function as ${__P(parameter_name,)}

You can also add the next line:

parameter_name=foo

to the jmeter.properties file which you're loading with JMeterUtils.loadJMeterProperties function.

Dont' forget to add ApacheJMeter_functions.jar to your project classpath otherwise __P() function will not be resolved.

More information: Apache JMeter Properties Customization Guide

Upvotes: 3

Related Questions