15
votes

Is there a way of accessing the current script's absolute physical path via a variable/property? There doesn't appear to be anything listed via a Debug Sampler.

It's incredibly annoying that actions like loading CSV files and JMX Includes uses the current working directory as its relative path.

4

4 Answers

20
votes

I used the answer provided by haridsv. It worked great except that I needed to put the directory to the JMX file in a variable. I made a "User Defined Variables" component and used BeanShell in the variable's "Value" field like this:

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

The first BeanShell section calls the Java class that gets the directory in question. The second appends a file separator to the path, which is of course optional.

10
votes
  • Include Controller
    As per component's reference:

    This element does not support variables/functions in the filename field.
    However, if the property includecontroller.prefix is defined, the contents are used to prefix the pathname. If the file cannot be found at the location given by prefix+filename, then the controller attempts to open the fileName relative to the JMX launch directory (versions of JMeter after 2.3.4).

    You can pass JMeter a java property named includecontroller.prefix which can be used to prepend a directory to the JMX file you're including.

    1) In case of console launch use:

    -Jincludecontroller.prefix=/full/path/to/jmx/scripts/dir/

    2) in case of GUI - add the same to .sh/.cmd/.bat file or write a wrapper file;
    3) in case of Jmeter Ant Task usage - set as separate property:

    <jmeter 
    jmeterhome="${jmeter.home}" 
    testplan="..." 
    resultlog="...">
        <property name="jmeter.save.saveservice.assertion_results" value="all"/>
        <property name="jmeter.save.saveservice.output_format" value="xml"/>
        <property name="includecontroller.prefix" value="..."/>
    </jmeter>
    
  • CSV Data Set Config
    As per component's reference:

    Relative file names are resolved with respect to the path of the active test plan.
    Absolute file names are also supported, but note that they are unlikely to work in remote mode, unless the remote server has the same directory structure. If the same physical file is referenced in two different ways - e.g. csvdata.txt and ./csvdata.txt - then these are > > treated as different files. If the OS does not distinguish between upper and lower case, csvData.TXT would also be opened separately.


    You can declare a test plan variable that retrieves parameter value with the folder containing csv data files:
    e.g.

    csv.path | ${__P(csv.path, ${__property(user.dir)}${__BeanShell(File.separator,)})} 
    CSV Data Set Config
    Filename = ${csv.path}${__P(users-list,)}
    

    Setting from console:

    -Jcsv.path=/full/path/to/csv/data/dir/

    Setting for distributed testing setup:

    -Gcsv.path=/full/path/to/csv/data/dir/
8
votes

By saying "current script's absolute physical path", I am guessing OP is referring to the location where the testplan (jmx file) is loaded from. I needed exactly this to generate a CSV file from BeanShell script at the beginning of the run, which is subsequently used in a CSV Data Set Config to read back, so I wanted the script to work just like how the later works when no path is specified. I went through the JMeter source and found this working solution:

import org.apache.jmeter.services.FileServer;
log.info(FileServer.getFileServer().getBaseDir());

I tested this and saw the correct path in the jmeter.log.

3
votes

My particular issue was that my relative Include Controller path included a backslash which broke on Linux and OSX.

The solution was to use a forward slash in relative paths, which works on all platforms.