How to get the absolute folder location of a Jmeter .jmx project file from within itself?
Asked Answered
S

3

10

I have a Jmeter project that is executed by Maven and is able to locate external Beanshell scripts by the path src/test/jmeter/external-scripts-dir/script1.bsh , but when I run Jmeter directly in the GUI on my computer the relative location doesn't work and the tests cannot be ran standalone. This forces me to run Jmeter from Maven.

So, I have a project file located at a Maven layout location like C:\files\git\projectA\src\test\jmeter\Project.jmx but since I ran jmeter from its installation folder at C:\Jmeter2.12 , it cannot find the relative location of the external script I mentioned earlier.

To solve this, all I need is to set a variable to the directory containing the .jmx file. Is there any possible way to do this?

I can dynamically determine the home of Jmeter ( C:\Jmeter2.12 ) pretty easily (using the following code) but that doesn't help me get the location of the project file.

${__BeanShell(import org.apache.jmeter.services.FileServer; FileServer
  .getFileServer().getBaseDir();)}${__BeanShell(File.separator,)}

Is there something similar to the above code that would allow me to deduce the project file location?

Stagg answered 7/1, 2015 at 0:6 Comment(2)
Not sure if this helps but did you know that you can run JMeter in GUI mode using the meter-maven plugin? Just go into your project and do: mvn jmeter:guiPush
https://mcmap.net/q/1176547/-how-to-get-the-name-of-jmx-jmeter-filename-in-a-variable - to get jmx test plan file location for both GUI / Non GUI mode executions.Scopp
P
11

If you're looking for the way to locate current script when you run JMeter in GUI mode you can try the following Beanshell expression:

${__BeanShell(import org.apache.jmeter.gui.GuiPackage;GuiPackage.getInstance().getTestPlanFile();)}

If you brake this down into 5 lines there will be:

import org.apache.jmeter.gui.GuiPackage;
import org.apache.commons.io.FilenameUtils;
String testPlanFile = GuiPackage.getInstance().getTestPlanFile();
String testPlanFileDir = FilenameUtils.getFullPathNoEndSeparator(testPlanFile);
vars.put("testPlanFileDir", testPlanFileDir);
log.info("testPlanFileDir:" + testPlanFileDir);

Your current .jmx file fill be stored as scriptFile JMeter Variable.

References:

Protoactinium answered 8/1, 2015 at 12:13 Comment(1)
This works when running the test from the JMeter GUI. For running from Maven, @djangofan's suggestion helped.Anticlinorium
S
9

The solution was (thanks to Ardesco) to do the following:

Use the following variables, set in the Test Plan global variables:

projectHome = ${__BeanShell(import org.apache.jmeter.services.FileServer; FileServer.getFileServer().getBaseDir();)}
jmeterHome = ${__BeanShell(System.getProperty("user.dir");)}
scriptHome = ${projectHome}/scripts
Stagg answered 7/1, 2015 at 19:26 Comment(6)
You can get properties with the __P syntax, so jmeterHome can be simplified to ${__P(user.dir)}Edd
What you get here is the bin folder of the Jmeter environment, not the location of the JMX file.Nonu
@ShaiAlon projectHome is where the jmx file resides, while jmeterHome is actually the bin folder of the JMeter environment.Goodspeed
@Goodspeed you are correct. However, the question after all was how to get the folder of the JMX file and not the bin folder.Nonu
jmeterHome is NOT the bin folder of the environment if you launch JMeter correctly, from the root of the JMeter install by running ./bin/jmeter . If you setup your path correctly, this is effectively what you should have. I know this is often misunderstood, such as with the jmeter-maven-plugin, which uses /bin as the home.Stagg
the jmeter-maven-plugin does not use the bin directory as JMETER_HOME, it treats target/<GUID>/jmeter as JMETER_HOME. look in the config.json in the target dir after running the configure goal with the plugin jmeterDirectoryPath is used as JMETER_HOMEPush
B
6

Thanks to the accepted answer, I was able to solve a related problem.

Now, I can calculate this parameter once and re-use it as a user variable.

JMETER_SCRIPTS_DIRECTORY=${__BeanShell(import org.apache.jmeter.services.FileServer; FileServer.getFileServer().getBaseDir();)}

Its usage is ${JMETER_SCRIPTS_DIRECTORY}. enter image description here

Interestingly, it works in both GUI mode and NON GUI mode (from command line).

Bibliographer answered 10/8, 2020 at 14:11 Comment(1)
Thanks! Even shorter: ${__BeanShell(org.apache.jmeter.services.FileServer.getFileServer().getBaseDir())}Kitkitchen

© 2022 - 2024 — McMap. All rights reserved.