Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 252
djangofan Avatar asked Jan 07 '15 00:01

djangofan


3 Answers

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:

  • GuiPackage class JavaDoc
  • How to use BeanShell: JMeter's favorite built-in component guide
like image 145
Dmitri T Avatar answered Oct 17 '22 00:10

Dmitri T


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
like image 32
djangofan Avatar answered Oct 16 '22 22:10

djangofan


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

like image 43
Yan Khonski Avatar answered Oct 17 '22 00:10

Yan Khonski