I have a custom jmeter sampler written in java that takes file location and file path as parameters and based on those creates a json file for posting to a service (based on a template file). Since my script needs to run on multiple environments with varying directory structures, I need to use a relative path in my sampler to point to the file I need to translate into valid json. Is there a way to get the directory my script lives in as a variable in jmeter? I have tried ${user.dir}, but that returns the directory where jmeter launched from, not the directory where my script lives. Is there another system property I could reference to get this value? If not, has anyone programmatically accomplished this?
Within the Apache JMeter application, save your JMeter test plan (File > Save) to a JMX file on a local computer. Next, log in to your LoadView account and click New Test on the Test Manager page . On the Select a Load Testing Type page, pick the JMeter test type.
To save the Test Plan, select "Save" or "Save Test Plan As …" from the File menu (with the latest release, it is no longer necessary to select the Test Plan element first). JMeter allows you to save the entire Test Plan tree or only a portion of it.
I ran into a similar situation a while ago where I had to upload a signature file as part of a POST request. This signature file lived relative to the folder structure in which my JMeter scripts were located.
Try using the following BeanShell expression (which can be accessed as a system-level property) to resolve the relative path from the location in which your JMeter test-cases are located/executed.
${__BeanShell(import org.apache.jmeter.services.FileServer; FileServer.getFileServer().getBaseDir();)}${__BeanShell(File.separator,)}
You can enter this with the name of the file you want to send/POST in the "Send Files With the Request" field by simply appending the file name.
${__BeanShell(import org.apache.jmeter.services.FileServer; FileServer.getFileServer().getBaseDir();)}${__BeanShell(File.separator,)}fileName.sig
A more cleaner implementation would be declare this as a variable (local or global depending on the scope) and use this variable within the appropriate fields.
absolutePath = ${__BeanShell(import org.apache.jmeter.services.FileServer; FileServer.getFileServer().getBaseDir();)}${__BeanShell(File.separator,)}
${absolutePath}fileName.sig
NOTE: This approach is super useful if you intend to plug your JMeter test files into a Continuous Integration system such as Jenkins. It makes your test-cases more maintainable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With