JMeter upload file with relative path from the jmx file
Asked Answered
R

4

37

I am creating a HTTP Request Sampler in JMeter to automate uploading file to a http service. Is there a way to set the "File Path" in the "Send Files With the Request" to a relative path from the location of the jmx file?

Representational answered 28/5, 2013 at 6:54 Comment(1)
As of JMeter 5.6 the File.path named stringProp property in the .jmx file may be set to a relative value without any __BeanShell or custom variable and it simply works. (tested in the multipart request).Shreeves
R
56

Yes. (This was confirmed in JMeter v2.9)

You can use the following BeanShell expression in the filename input field:

${__BeanShell(import org.apache.jmeter.services.FileServer; FileServer.getFileServer().getBaseDir();)}${__BeanShell(File.separator,)}<YOUR FILENAME HERE>

So if your file, "upload.jpg" was located in the same folder as your JMX test file, the complete value would be

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

To keep it a bit cleaner, add a "User defined variables" config element where you assign the base of the expression to a variable e.g

SCRIPT_PATH = ${__BeanShell( ...etc

Then your file path input value would read a more readable:

${SCRIPT_PATH}upload.jpg

Rodenticide answered 20/6, 2013 at 12:41 Comment(1)
This worked for me. However, I wonder if there's a better way to make this association rather than using the User Defined Variables element, as I might want to use this in multiple different test cases. Surely there must be some way to tell jmeter to always use the relative path?Bighead
E
9

I'm under version 2.11, and the BeanShell script didn't work for me, to make the CSV file relative, and so to make a relative path you simple need to use this ~/filename.csv. This would mean that the CSV file exist in the same directory as the JMX file. But I guess you can modify it to this for example ~/../results_dir/filename.csv etc..

Good luck.

Elater answered 9/4, 2014 at 17:24 Comment(0)
D
5

I found the answer here http://kisbigger.blogspot.com/2013/09/pointing-to-files-with-relative-paths.html which was ...

  • step 1: Create a user-defined-variable Config element to your project.
  • step 2: Add a new variable CONFIG_PATH
  • step 3: in the value column place this code : ${__BeanShell(import org.apache.jmeter.services.FileServer; FileServer.getFileServer().getBaseDir();)}
  • step 4 : use the variable name wherever you want like ${CONFIG_PATH}\config.xml

having a filename.csv in the same folder as the jmx and putting ~/filename.csv with JMeter 2.11 and a newer JAVA did not work

Developer answered 20/10, 2017 at 17:10 Comment(1)
This is actually the same as the most upvoted answer given 4 years earlier, although I must admit yours is more cut-and-dried.Snotty
S
0

TL;DR: it now works with the relative path.

I tried the solution with the user-defined variable, but then I saw that JMeter is resolving it as a relative path anyway.

As of JMeter 5.6, the File.path named stringProp property in the .jmx file may be set to a relative value without any __BeanShell or user-defiend variable and it simply works. (tested in the multipart request).

In the UI mode and the CLI mode it seams to be resolving the relative paths to be relative to the location of the .jmx file.

Shreeves answered 10/11, 2023 at 12:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.