JMeter - Add random files to upload - jmeter

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.

Related

Jmeter JSR223 write assertion response to a csv file

i need to write a Assertion error, assertion failure and Assertion failure message(example data below in the pic) to a csv file. What is the best way to do this? Is there a possibility to create a JSR223 Sampler to read assertion messages from all http requests at once and save that to csv file? Built-in saving to file does not meet requirements.
Picture
JSR223 Sampler will only have access to the previous sampler result, I would recommend using JSR223 Listener and the following code:
vars.put('failureMessage', prev.getAssertionResults().find { assertionResult -> assertionResult.isFailure() }.getFailureMessage())
the code will extract the failure message from the first assertion
then you need to declare Sample Variables property in user.properties file like:
sample_variables=failureMessage
and finally the variable can be written to a file using Flexible File Writer listener (this guy can be installed using JMeter Plugins Manager)

Any option to change the name of the file which is on disk during runtime to execute upload functionality in Jmeter

I have a scenario where one of the test REST API returns a transaction_Id, this transaction Id I need to pass in my next request(API Request), also upload the file as well. This upload file has name format like ${transaction_Id}_1_1_bkg.raw,without correct format backend server may not be able to analyze the data.
Since I have to run a load test, these transaction Ids will generate at run time and the same I have to change the file name and upload it as well.
Did any one had face this challenge before in Jmeter to change the file name at run time and upload it, if yes, what was the solution.
Thanks,
Akshat
You could copy the file to a new one with the desired name using JSR223 PreProcessor like
org.apache.commons.io.FileUtils.copyFile(new File('template_bkg.raw'), new File(vars.get('transaction_Id') + '_1_1_bkg.raw'))
where vars stands for JMeterVariables class instance, see the JavaDoc for more information on all existing functions and Top 8 JMeter Java Classes You Should Be Using with Groovy article to learn more about JMeter API shorthands available for the JSR223 Test Elements.
Once upload is finished you can delete the file using JSR223 PostProcessor

How can i use the Result of an HTTP request (zip file) in the body of another http request?

I have two HTTP requests (Export and Import).
The result of the "Export" request is a zip file and i want to use that downloaded file in the body of the "Import" request.
Does anyone have an idea ?
Thanks in advance.
Normally you should be storing the file to your drive via Save Responses to a file listener added as a child of the Export request and then specifying the path to the stored file in the "Files Upload" section of the HTTP Request sampler for the Import request
Example test plan structure:
More information: Performance Testing: Upload and Download Scenarios with Apache JMeter
However if your scenario is different and you need to do this in memory, you can read the zip file contents into a JMeter Variable using Regular Expression Extractor and the following regular expression:
(?s)(^.*)
once done you will be able to use the associated JMeter Variable where required.

JMeter zip file download from server

I have a requirement to download zip file from server through JMeter to test the peroformance but for me the downloaded files are shown in x-filler, i need to have the zip file downladed.
Please help me here
Thanks in Advance
Simulating file download event using JMeter is as simple as sending HTTP GET request using HTTP Request sampler.
If you need to save the response somewhere for later reuse or analysis add Save Response to a file listener as a child of the request which performs the download. Check out Performance Testing: Upload and Download Scenarios with Apache JMeter article for comprehensive explanation.
Be aware that storing responses will cause huge disk IO overhead during the load test so I would recommend ticking Save response as MD5 hash box under "Advanced" tab of the HTTP Request sampler and use MD5Hex Assertion to compare the MD5 checksum of the response with some reference value.
To download any file, you need to extract it using Regular Expression Extractor.
Add the Regular Express Extractor to the request where you want to download and configure the fields as shown below. Use (?s)(^.*) as the expression to extract everything.
Add the Save Responses to a File sampler and configure the fields as shown below.
Execute the test plan. In the JMETER_HOME\bin\, you can see the zip file. Extract the zip file and validate.
The easiest way is to use your own code to download the file. The options are BeanShell Postprocessor or JSR223 Postprocesor.
I extracted file name from response header Content-Disposition, save it to varible, and the use variable for filename. Additional variable was defined on Test level, holding folder name where to save files.

How to read XML data inside a response body in jmeter

I have been assigned with a task to capture the XML data inside a response body of a sampler in Jmeter. I've tried many ways to save the response data but there is no luck.is there any way to save the Response data in xml format using Jmeter? looking for a positive reply.
Add the next 2 lines to user.properties file (located in JMeter's "bin" folder)
jmeter.save.saveservice.output_format=xml
jmeter.save.saveservice.response_data=true
Run your JMeter test in command-line non-GUI mode like:
jmeter -n -t test.jmx -l result.jtl
You will be able to see response data for each sampler in result.jtl file.
See Configuring JMeter user manual chapter for more information on JMeter property files and properties
If you need to store the response data of one single sampler only, add JSR223 PostProcessor as a child of this sampler and put the following code into "Script" area:
new File('myFile.xml') << prev.getResponseDataAsString()
Once you execute JMeter test you should see myFile.xml in JMeter's "bin" folder, it will contain response data. Check out Groovy Is the New Black guide to learn about Groovy scripting in JMeter.

Resources