0

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

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)}
| improve this answer | |
  • 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 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 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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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