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

djangofan picture djangofan · Jan 7, 2015 · Viewed 16.4k times · Source

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?

Answer

Dmitri T picture Dmitri T · Jan 8, 2015

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: