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?
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:
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
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}
.
Interestingly, it works in both GUI mode and NON GUI mode (from command line).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With