1

I've got a JMeter POST request that has body data like below:

${__FileToString(/${json_request}.txt,,)}

The json_request variable comes in from a CSV data set config, allowing me to iterate through a series of files.

The CSV file and the body data files are all stored a level beneath the .jmx test plan file. I've been able to use relative file paths for the CSV data set config. Is there anyway to do the same with FileToString? Using relative paths is leading to the file not being found. If not, is there another way to handle this?

Thanks,

1

2 Answers 2

1

It is more than possible, you're just using incorrect syntax, I believe you should remove this slash completely

Examples:

  • ${__FileToString(../README.md,,)} - shows the content of the README.md file which lives 1 level above of the "bin" folder of JMeter installation

    enter image description here

  • ${__FileToString(examples/CSVSample_user.csv,,)} - shows the content of the CSVSample_user.csv file which lives under examples folder in the "bin" folder of your JMeter installation

    enter image description here

More information:


Update: get file contents from a folder relative to the test plan location:

${__groovy(new File(org.apache.jmeter.services.FileServer.getFileServer().getBaseDir() + System.getProperty('file.separator') + vars.get('json_request') + '.txt').text)}
2
  • Thanks for the reply Dmitri. I had seen you could refer, relatively, to the bin folder in the JMeter folder. My JMeter test plans are held under C:\Users\<Username> so I was trying instead to refer, relatively, to this location. Is that possible with filetostring or do I need to try something else?
    – Moorpheus
    May 4, 2020 at 7:31
  • See updated answer after the horizontal line, it uses __groovy() function to get the current working directory, adds OS-independent file separator and reads ${json_request} variable.
    – Dmitri T
    May 4, 2020 at 12:25
0

I fixed this with use of a BeanShell PreProcessor to get the location of the .jmx test plan file, outputting this to a variable.

I then used this variable in the FileToString function, so I'm effectively using a blend of absolute and relative file paths.

The BeanShell PreProcessor code looks like:

import org.apache.jmeter.gui.GuiPackage;
import org.apache.commons.io.FilenameUtils;
String testPlan = GuiPackage.getInstance().getTestPlanFile();
String testPlanLocation = FilenameUtils.getFullPathNoEndSeparator(testPlan);
vars.put("testPlanLocation", testPlanLocation);

Then, my FileToString changes to ${__FileToString(${testPlanLocation}\\${json_request}.txt,,)} which works fine.

2
  • 1
    Your solution will not work if you ]run JMeter command-line non-GUI mode](blazemeter.com/blog/…), moreover since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language. The relevant function would be something like: ${__groovy(new File(org.apache.jmeter.services.FileServer.getFileServer().getBaseDir() + System.getProperty('file.separator') + vars.get('json_request') + '.txt').text)}
    – Dmitri T
    May 4, 2020 at 12:22
  • You are correct. Amending my POST request to use the groovy script has worked in GUI and non-GUI mode. Thanks for the help!
    – Moorpheus
    May 4, 2020 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.