How to add "Send Parameters With the Request" using java in jmeter - jmeter

I am new to jmeter and i want to execute jmeter in non-gui fashion using java code.
When testing on the endpoints i would like to pass the "Send Parameters with the Request" via java code. So i have tried sending the data as follows
HTTPSampler httpSampler = new HTTPSampler();
httpSampler.setProtocol("http");
httpSampler.setDomain(hostip);
httpSampler.setPort(8080);
httpSampler.setPath(endpointpath);
httpSampler.setMethod("POST");
HTTPArgument httpArgument = new HTTPArgument();
httpArgument.setValue("[{ \"firstname\": "", \"name\": \"Venkat\"}]");
No Error came. The request was not added to the DB
Can some one suggest and provide a sample code to add the post request body using java?

httpSampler has no arguments associated. Basically you are just sending a http request without any argument.
You might have to use addTestElement / setArgument method to associate the parameters to the httpsampler.
httpSampler.setMethod("POST");
HTTPArgument httpArgument = new HTTPArgument();
httpArgument.setValue("[{ \"firstname\": \"venkatachalam\", \"name\": \"Venkata\"}]");
httpSampler.addTestElement(httpArgument);
Just curious - why are you creating your JMeter test using java by the way?
To run your JMeter test in non gui mode, use below command & options. (assuming you have %JMETER_HOME%/bin is added to your PATH variable)
jmeter -n -t D:\TestScripts\script.jmx -l D:\TestScripts\scriptresults.jtl
-n [This specifies JMeter is to run in non-gui mode]
-t [name of JMX file that contains the Test Plan]
-l [name of JTL file to log sample results to]
-j [name of JMeter run log file].
Besides these options, JMeter has several other parameters that can be used for running in non-GUI mode.
-R [list of remote servers] Run the test in the specified remote servers
-H [proxy server hostname or ip address]
-P [proxy server port]

Related

Jmeter command line option issue

I am running a load test using jmeter command line and saving the result into csv file using -l command line option. After the test is completed, i see the data in the csv and while i upload it to a view result tree and try to check the failure response data in the response section, i dont see any response data noted in there. Is there something am missing here?
i have the following command
<path to jmeter bat> -n -t <jmx loc> -l <loc of result.csv> -j <path to jmeterlog> -Gparam1 -Gparam2 -Gparam3 -Gjmeter.save.saveservice.output_format=csv -Gjmeter.save.saveservice.output_format=csv -Gjmeter.save.saveservice.assertion_results_failure_message=true -Gjmeter.save.saveservice.data_type=true -Gjmeter.save.saveservice.label=true -Gjmeter.save.saveservice.response_message=true -Gjmeter.save.saveservice.successful=true -Gjmeter.save.saveservice.thread_name=true -Gjmeter.save.saveservice.time=true -Gjmeter.save.saveservice.response_message=true -Gjmeter.save.saveservice.successful=true -Gjmeter.save.saveservice.thread_name=true -Gjmeter.save.saveservice.time=true -Gjmeter.save.saveservice.connect_time=true -Gjmeter.save.saveservice.assertions=true -Gjmeter.save.saveservice.latency=true -Gjmeter.save.saveservice.connect_time=true -Gjmeter.save.saveservice.thread_counts=true -Gjmeter.save.saveservice.response_data=true -Gjmeter.save.saveservice.response_data.on_error=true -Gjmeter.save.saveservice.response_message=true -Gjmeter.save.saveservice.samplerData=true -Gjmeter.save.saveservice.requestHeaders=true -e -o <Reportpath>
By default JMeter uses CSV format for saving test metrics
CSV format is not suitable for storing response data due to eventual delimiters and line breaks. Moreover as per Reducing resource requirements chapter it's not recommended to store response data.
If you really need to save response data you need to switch JMeter's .jtl file format to XML and instruct JMeter to store it, it's controllable via the following properties:
jmeter.save.saveservice.output_format - defaulting to csv
jmeter.save.saveservice.response_data - defaulting to false
So you need to amend your command line to override these properties like:
<path to jmeter bat> -Jjmeter.save.saveservice.output_format=xml -Jjmeter.save.saveservice.response_data=true ......
in order to make the changes permanent add the corresponding lines to user.properties file
More information:
Results file configuration
Configuring JMeter
Apache JMeter Properties Customization Guide
Another option is adding a separate Listener like Simple Data Writer and configure it to save response data into a separate file:

jmeter command line option doesnt pick my params

Why my command below doesn't pick the values I give at runtime rather it takes from my JMeter test plan itself where I defined with option ${__P(param1,value)}
<path to jmeter bat> -n -t <jmx loc> -l <loc of result.csv> -j <path to jmeterlog> -Gparam1=paramval1 -Gparam2=paramval2 -Gparam3=paramval3 -Gjmeter.save.saveservice.output_format=csv -Gjmeter.save.saveservice.output_format=csv -Gjmeter.save.saveservice.assertion_results_failure_message=true -Gjmeter.save.saveservice.data_type=true -Gjmeter.save.saveservice.label=true -Gjmeter.save.saveservice.response_message=true -Gjmeter.save.saveservice.successful=true -Gjmeter.save.saveservice.thread_name=true -Gjmeter.save.saveservice.time=true -Gjmeter.save.saveservice.response_message=true -Gjmeter.save.saveservice.successful=true -Gjmeter.save.saveservice.thread_name=true -Gjmeter.save.saveservice.time=true -Gjmeter.save.saveservice.connect_time=true -Gjmeter.save.saveservice.assertions=true -Gjmeter.save.saveservice.latency=true -Gjmeter.save.saveservice.connect_time=true -Gjmeter.save.saveservice.thread_counts=true -Gjmeter.save.saveservice.response_data=true -Gjmeter.save.saveservice.response_data.on_error=true -Gjmeter.save.saveservice.response_message=true -Gjmeter.save.saveservice.samplerData=true -Gjmeter.save.saveservice.requestHeaders=true -e -o
I am running this from jenkins. Why it is not picking up my parameters?
-G is used for distributed testing/Remote JMeter servers
defines a JMeter property to be sent to all remote servers.
Use -J instead to define JMeter property
-J[prop_name]=[value]
defines a local JMeter property.
You need to use -J command-line argument, -G is for sending the properties to JMeter Slaves in case of Distributed testing.
More information:
Configuring JMeter
Overriding Properties Via The Command Line
Full list of command-line options
Apache JMeter Properties Customization Guide

Why jmeter generate other requests and put a number for them

I have a script, one of the requests in the script is: redireccion.html, but when I generate the HTML Dashboard report I see:
redireccion.html-0, redireccion.html-1, redireccion.html-2
Why those requests are generated by Jmeter?
HTML Dashboard Report Graph
This happens when your first request i.e. redireccion.html encounters a HTTP Redirect, i.e. gets a Redirection Message
JMeter stores all these redirects as sub-results
If you don't want these sub-results to be present in the HTML Reporting Dashboard - you can run your test providing jmeter.save.saveservice.subresults property with the value of false like:
jmeter -Jjmeter.save.saveservice.subresults=false -n -t test.jmx -l result.jtl -e -o dashboard
In order to make the change permanent - just add the next line to user.properties file:
jmeter.save.saveservice.subresults=false
More information: Apache JMeter Properties Customization Guide

parametrize to csv file url in JMeter

I configured this in following steps: in this moment I prepare following csv file:
protocol $url
${dev} http://10.200.XXX.XX/{$url}
${trial} trial.mycompany.io{$url}
${product} product.mycompany.io{$url}
then I add to my Test Plan -> Config element - CSV Data Set Config, and then I add Switch Controller,
and I have WebSocket Samplers by Peter Doornbosch and I dont have HTTP Samplers and WebSocket Samplers;
In Switch Controller I add HTTP Request and WebSocket request-response Sampler. In HTTP request I set
protocol:http,
server name or IP: localhost,
port number 8080.
HTTP request method POST,
path: ${URL}, and
parameters Add: Name: Value ${protocol} ws://{$URL} ${protocol} http://{$URL}
and in WebSocket request-response Sampler I set: use existing connection
do I correctly set the parameterization of the url variable, do I do something wrong, how to correct it, what to change?
Your understanding of parameterization in JMeter seems to be vague, as far as I get your idea your CSV file should look like:
protocol, url
http, some-http-host/some-http-path
ws, some-websocket-host/some-websocket-path
http, some-http-host/some-other-http-path
etc.
Then you should be able to use it in the CSV Data Set Config as
Switch Controller will allow you to choose either HTTP Request or WebSocket samplers depending on ${protocol} variable value so when ${protocol} is equal to http - only children of the http Simple Controller will be executed and ws will not be run and vice versa, when ${protocol} is ws - only ws samplers are being kicked off and http are not executed.
And in Samplers you can refer the values from the CSV file as ${protocol} and ${url} correspondingly
Open cmd prompt, change the path to the jmeter bin folder.
Now write jmeter command to run:
jmeter -n -t testing.jmx
If you want to log the results then use -l with a file name for output, So the command will be changed to
jmeter -n -t testing.jmx -l results.jtl

Details about which user from csv failed response assertion in Jmeter

I am using JMeter to webUI performance testing. I have a list of users in csv with passwords. I am using response assertion to check failed password scenario.
How to record which user from csv is failed?
I would recommend going for Sample Variables property. For example, if you defined a ${username} which holds the user name from the CSV you can get it added to JMeter .jtl results file by adding the next line to user.properties file:
sample_variables=username
If you need to store more variables - provide them separated by commas:
sample_variables=username,password
Remember that:
JMeter restart is required to pick the property up
You can pass it via -J command line argument as well like:
jmeter -Jsample_variables=username,password -n -t test.jmx -l results.jtl
See Apache JMeter Properties Customization Guide for more information on different JMeter properties types and ways of working with them

Resources