8

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?

jmingov
  • 13,553
  • 2
  • 34
  • 37
djangofan
  • 28,471
  • 61
  • 196
  • 289
  • 1
    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:gui – Ardesco Jan 07 '15 at 08:41
  • http://stackoverflow.com/a/36860307/3940047 - to get jmx test plan file location for both GUI / Non GUI mode executions. – Pavan Kumar Apr 27 '16 at 04:48

3 Answers3

9

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:

djangofan
  • 28,471
  • 61
  • 196
  • 289
Dmitri T
  • 159,985
  • 5
  • 83
  • 133
8

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
djangofan
  • 28,471
  • 61
  • 196
  • 289
  • 1
    You can get properties with the __P syntax, so jmeterHome can be simplified to ${__P(user.dir)} – Cyberwiz Nov 29 '16 at 07:41
  • 1
    What you get here is the bin folder of the Jmeter environment, not the location of the JMX file. – Shai Alon Jul 29 '18 at 13:53
  • @ShaiAlon `projectHome` is where the jmx file resides, while `jmeterHome` is actually the bin folder of the JMeter environment. – acejazz Oct 15 '18 at 14:10
  • 1
    @acejazz you are correct. However, the question after all was how to get the folder of the JMX file and not the bin folder. – Shai Alon Nov 20 '18 at 10:01
  • 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. – djangofan Aug 14 '20 at 21:04
  • the jmeter-maven-plugin does not use the bin directory as JMETER_HOME, it treats `target//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_HOME – Ardesco Apr 01 '21 at 09:54
5

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).

Yan Khonski
  • 12,225
  • 15
  • 76
  • 114
  • 1
    Thanks! Even shorter: `${__BeanShell(org.apache.jmeter.services.FileServer.getFileServer().getBaseDir())}` – jetnet Nov 14 '22 at 20:46