Please consider this case in your JMeter’s Test Plan: If you’re using the CSV Data Set Config, you must provide the path to the CSV file. Basically, in the project, all scripts/data file should be located under a directory. And you know, we can fix the path to project directory, but most of us won’t do this way, we should get the current of the script via code, or some things like that. Fix the path can be counted as hard coded. Of course, the above situation is just one case, there are more some cases which we need to get the path of Test Plan file. So in this post, I will show you how to achieve it by many ways.

1. __BeanShell

The BeanShell function is a part of Functions and Variables of JMeter, it evaluates the script passed to it and returns the result.

Using the following BeanShell expression (which can be accessed everywhere in your Test Plan) to return the path to your script’s directory:

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

Let see how it work in the demo below:

I will use Dummy Sampler to display the Test Plan’s directory

base-dir-beanshell-func-1

2. __groovy

It’s similar to __BeanShell function. The __groovy function evaluates Apache Groovy scripts passed to it and returns the result.

As I just said, it’s similar __BeanShell function, it means totally similar, and below is the groovy expression to get the script’s location:

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

And this is the result:

base-dir-groovy-func-1.png

Tips:

Tip 1: If you want to have the separator at the end of the path, just add this ${__BeanShell(File.separator,)} or ${__groovy(File.separator)} at behind of the expression above. It looks like:

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

Tip 2: Usually, we store this value into a variable in User Define Variables under Test Plan, and just call the variable anywhere we want.

base-dir-tips-1.png
Click here to view original image

And the result:

base-dir-tips-2.png
Click here to view original image

Tip 3: If you want to access the parent directory of script’s directory, it’s very easy, just add two dots to the end of the path, such as /path/to/script/../

We do the same way in JMeter:

base-dir-tips-3-1.png
Click here to view original image

Note: please remember adding the separator (/ in Unix and \ in Windows) manually if you didn’t use the ${__BeanShell(File.separator,)}. But I strongly recommended you to use the function to call separator. It’s will help you run the test for both OS (Unix and Wins) normally, or you will get the error with / while running in Wins and vice versa.

Another way to access the parent directory: just define the PATH variable, and adding two dots when you want to call parent folder:

base-dir-tips-3-2.png

3. BeanShell Sampler, Pre/PostProcessor

Yes, of course, we can get the information of Test Plan’s directory via code. This section I will show you how to do it with BeanShell Sampler or BeanShell Pre/PostProcessor.

Actually, I just separated and breakdown line the statement in __BeanShell expression above. The code looks like

import org.apache.jmeter.services.FileServer;

//get path of current jmeter's script
String path = FileServer.getFileServer().getBaseDir();

//get the separator
String separator = File.separator;

log.info("Path: " + path);
log.info("Path with separator: " + path + separator);

Run this code and check the Log Viewer

base-dir-beanshell-sample.png
Click here to view original image

4. JSR223 with Groovy

The best bundle in JMeter if you want to develop some script, it’s JSR223 with Groovy. You can check it out Part 1 and Part 2 for more detail about JSR223 with Groovy.

The code to get JMeter’s script’s directory is almost the same with BeanShell. Please see below:

import org.apache.jmeter.services.FileServer;

//get current jmeter script's directory
def path = FileServer.getFileServer().getBaseDir();

//get the separator
def separator = File.separator;

log.info("Path: " + path);
log.info("Path with separator: " + path + separator);

Run and verify the result:

base-dir-jsr223-groovy.png

If you want to store that path into a variable and call outside of the script, please use vars.put variable. I mentioned this in JSR223 with Groovy part 1.

In that case, the code will be:

import org.apache.jmeter.services.FileServer;

//get current jmeter script's directory
def path = FileServer.getFileServer().getBaseDir();

//get the separator
def separator = File.separator;

log.info("Path: " + path);
log.info("Path with separator: " + path + separator);

vars.put("PATH_NO_SEPARATOR", path);
vars.put("PATH_WITH_SEPARATOR", path + separator);
Advertisements