JMeter Property to Specify Path of current file? - jmeter

Is there a property or function in JMeter that can specify the path to the folder from which the Test Plan has been opened?

In majority of cases, i.e. in Listeners you can use ~/ as location, relative to current .jmx file which is being executed.
However if you need the full path for any reason you can use __Beanshell() function to get it like:
${__BeanShell(org.apache.jmeter.services.FileServer.getFileServer().getBaseDir())}
References:
FileServer class JavaDoc
How to Use BeanShell: JMeter's Favorite Built-in Component

Yes, you can do this from a beanshell script. Here is what I am doing in my script

Related

How to provide relative path in Include controller in jmeter for test fragment

I have two thread groups from that in one thread I have two include controllers. but I want to provide relative path instead of absolute path so anyone can use this project in their system. as I am uploading this on GitHub.
enter image description here
If the external .jmx script lives at the same folder - just use its filename like Test Fragment.jmx in the Include Controller
If it resides under a folder - specify its relative location
somefolder/Test Fragment.jmx
For the parent folder:
../somefolder/Test Fragment.jmx
There is includecontroller.prefix property which is being added to the filename specified so if there is a common location for all external JMX files/test fragments you might want to set this property
More information:
Include Controller Documentation
Modularizing for Script Reusability: Examples in JMeter and YAML

Jmeter encoding of URL path

I have an URL path
path=mcat official mcat critical analysis and reasoning skills question pack volume 1 online.html
I want to encode with - in place of spaces.
e.g-mcat-official-mcat-critical-analysis-and-reasoning-skills-question-pack-volume 1-online.html
How will I do this in jmeter?
You can use __strReplace function which is available via Custom Functions bundle of the JMeter Plugins project. You can install it using JMeter Plugins Manager. The syntax would be
${__strReplace(${path}, ,-,)}
If you don't want (or cannot use) the plugins you can achieve the same using __groovy() function, in this case the syntax will be:
${__groovy(vars.get('path').replace(' '\, '-'),)}
Both examples assume that your URL is stored in ${path} JMeter Variable.
Demo:

Fail to Download File Using Jmeter and relative path

I'm using JMeter to test concurrent downloads, and need to store it into specific folder.
I'm working on windows and using "Save response to a file". If I use the absolute path it all works exactly as I want it to work.
Now, I need to share this with a group (will be stored in repo) and want to change my obviously wrong absolute path to relative.
Now:
"~" - stores download in project root folder, where .JMX file is, but I want it in: "~/downloads/" so, in subfolder.
None of "regular" things work and yes I tried all possible combinations even those I know it should not work at all.
~/downloads/
./downloads/
~./downloads/
\downloads\
\downloads\
...
It or fails, and I can see in log error like:
~\downloads\1.x-gzip (The system cannot find the path specified)
Or It get's store in root with the filename "downloads" instead 1.x-gzip
Is there a way to do it, maybe using beanshell?
Most important, why is that behavior in JMeter so different, I mean if it works for "home" and "~/../" why it does not recognize subfolders?
Thanks
JMeter seems to be removing trailing slash so there is no way to bypass it apart from providing full path to the folder.
If you need to do it dynamically, relevant Beanshell script will look like:
${__BeanShell(import org.apache.jmeter.services.FileServer;FileServer.getFileServer().getBaseDir() + System.getProperty("file.separator") + "downloads" + System.getProperty("file.separator"),)}
You need to have "downloads" folder at the same level with your .jmx test script.
References:
__Beanshell() - function
FileServer class JavaDoc
How to Use BeanShell: JMeter's Favorite Built-in Component

JMeter - Add random files to upload

I've a lot of files that i would like to upload to test my service but i need to pick up random files and send it. Is possible get random files and delete the file when request is completed?
Thank's in advance.
JMeter doesn't provide any test element to create random files and delete them, so you'll have to write the relevant code.
For example:
Add a Beanshell PreProcessor and Beanshell Post Processor as children of the requests which performs file upload
Put the following code into Beanshell PreProcessor "Script" area
import org.apache.commons.io.FileUtils;
File myFile = new File("myFile.txt");
FileUtils.writeStringToFile(myFile, "JMeter rocks!");
The code above creates "myFile.txt" file in JMeter's current working directory and writes "JMeter rocks!" line to it
In order to delete the file after request you can add the following code into the Beanshell PostProcessor
import org.apache.commons.io.FileUtils;
FileUtils.deleteQuietly(new File("myFile.txt"));
For more information on using Beanshell scripting in Apache JMeter see How to use BeanShell: JMeter's favorite built-in component guide.

BeanShell PreProcessor

I've a few BeanShellScripts inside my JMeter Project. I would like to use a few of them inside my project. I'm using command inside BeanShell PreProcessor to invoke another BeanShellProcessor in my project:
${__BeanShell(Name_Of_My_Script)}
But I realized that It's opening them a few times, so sometimes the request is sending with wrong value. Is there any another command or option to do this?
I don't think that it's a right way to execute Beanshell via function. As per JMeter Beanshell manual Beanshell Script attribute should be
A beanshell script (not a file name)
More correct way is storing your another Pre-Processor script code into a JMeter Variable, i.e. SCRIPT2 and call it as ${__BeanShell(${SCRIPT2})}.
You won't need to escape anything as the function automatically parses input script.
See How to use BeanShell guide for more details on Beanshell scripting.
It's working quite well if you use Module Controller which indicate a current Simple Controller with Beanshell script to run. Inside Simple Controller should be BeanShell Sampler.

Resources