In jmeter how can I create a folder dynamically? - windows

A monthly PDF report is getting downloaded by the HTTP sampler. I'm passing the file path of this report in the prefix of 'Save responses to a file' listener which is a child of the HTTP sampler.
I'm passing the month name variable like ${month} in the file path, to create a folder dynamically as given below.
E:/Work/Monthly Productivity Report/${month}/MONTHLY_PRODUCTIVITY_REPORT_${month}_${__time(yyyyMMdd-hhmmss)}
But the script is throwing errors like unknown source or path not found. Is there any way where I can create new folder dynamically?

Add JSR223 Sampler, choose Language Java and put the code in script window:
String folder = vars.get("folder");
boolean success = (new File(folder)).mkdirs();
return success;

For each test suite run:
JMeter always created a new testresult.csv and needs a blank new folder for report. So it is good to use .bat file to dynamically create the testresult.csv and report folder using this code.
Everytime you run .bat file, you will get new result.csv e.g. TestResults-Sat_09182021_160935_94.csv and new report folder e.g. Reports-Sat_09182021_160935_94
Add below code in Run.bat and open Run.bat:
set mydate=%date:/=%
set mytime=%time::=%
set mydatetime=%mydate:=_%_%mytime:.=_%
jmeter -n -t "F:\WebsitePerformanceTest.jmx" -l "F:\TestResults-%mydatetime%.csv" -e -o "F:\Reports-%mydatetime%"
Enjoy !

Related

Azure Load test preview unable to read data from parameterized CSV file in JSR223 Sampler

I used JSR223 Preprocessor to create the request body for a POST JSON request. I used Groovy language for it The code has some parameterization so, I mentioned the path for CSV file in the script as below and attached the Order.csv file to the test plan in load test preview.
CSV file path in the script:
"List lines = new File("Order.csv").readLines()"
So whenever I run the test in Azure load test preview, the is the error message im encountering:
javax.script.ScriptException: java.io.FileNotFoundException: Order.csv (No such file or directory)
How can I fix this. Please Help.
I tried just mentioning the CSV file name in the code and attached the CSV file to the Load test preview along with .jmx file.
This is throwing error saying, File not found
How do you know where guys from Microsoft are placing the CSV file and why do you expect them to copy it to the same place where the .jmx script lives?
I would suggest amending your Groovy script to get the file location dynamically using FileServer class, i.e. change this line:
List lines = new File("Order.csv").readLines()
to something like:
List lines = new File(org.apache.jmeter.services.FileServer.getFileServer().getBaseDir() + File.separator + 'Order.csv').readLines()
More information on Groovy scripting in JMeter: Apache Groovy: What Is Groovy Used For?

Saving an external ID to result file in Jmeter

I have an external data in my test data that is an id that I would like to be able to write to the the result file that is generated when I run this command:
./jmeter -n -t /home/usr/jmeter/script.jmx -l /home/usr/jmeter/scriptjtl
The id will not be used for any request, but the trackid is necessary to include i troubleshooting errors. Example of how the track_id can look: kJbc1W1YupprLcB8YZE0gla1T8APE7Td
Is there some possible away to save the track ID in the result file as a parameter or in any other way?
If you have this track_id extracted into a JMeter Variable you can use sample_variables property in order to add it to the .jtl results file, amend you command line as:
./jmeter -Jsample_variables=track_id -n -t /home/usr/jmeter/script.jmx -l /home/usr/jmeter/scriptjtl
once script finishes you will see an extra column called track_id containing the respective values for the ${track_id} variable for each Sample Result
In order to make the change permanent add the next line to user.properties file (lives in "bin" folder of your JMeter installation)
sample_variables=track_id
More information:
Configuring JMeter
Overriding Properties Via The Command Line
Apache JMeter Properties Customization Guide

Can we rename the file name in Jmeter during run time which will be uploaded in the script

I have to upload audio files in the Jmeter script which is stored in my system. E.g. abc.wav is the file store in the system. But in the script the file name format should be "Testinstanceid__itemid__ interactionid.wav". Here "Testinstanceid" is the dynamic value which we can get from correlation of previous response.
But how I can upload the file with this dynamic value during run time and it will upload correctly in the script.
Thanks in advance
You can copy your abc.wav file to the Testinstanceid__itemid__ interactionid.wav file using JSR223 PreProcessor and Groovy script like:
org.apache.commons.io.FileUtils.copyFile(new File('abc.wav'), new File(vars.get('Testinstanceid') + '__itemid__ interactionid.wav'))
once you finish the uploading you can delete the file to free up your drive in JSR223 PostProcessor like:
org.apache.commons.io.FileUtils.deleteQuietly(new File(vars.get('Testinstanceid') + '__itemid__ interactionid.wav'))
where vars stands for JMeterVariables class instance, check out the JavaDoc for all available functions

JMeter SMTP Sampler - is it possible to use parameterization with in "attach files" option?

am using JMeter 4.0 and I have been trying to parameterize the filename in "Attach files" option with no success. I am required to use attachments of varying sizes and with each loop a random pdf file (from the set of files saved locally in d drive) to be attached with the email.
I am using a CSV data set config for parameterization and see no issues in using the parameters in Subject line of within the Message.
However, when used with the attach files option, the JMeter test fails to execute with FileNotFoundException as the variable name is substituted as such instead of the pdf filename. Is there a solution?
Error Message: java.io.FileNotFoundException: D:\Data_Jmeter\${AttachFile}.pdf (The system cannot find the file specified)
I cannot reproduce your problem using the following settings:
Where ${path} is set via User Defined Variables on Test Plan level and resolves into "bin" folder of my JMeter installation
The same situation for ${path} variable originating from the CSV Data Set Config
So I would recommend to double check if your ${AttachFile} variable is defined, you can do this using Debug Sampler and View Results Tree listener combination

JMeter upload file with relative path from the jmx file

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?
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
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.
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

Resources