0
votes

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?

1
Does it work fine in GUI mode first? - vins
Yes it works fine in GUI MODE - Venkatachalam Neelakantan
Can you provide the complete code if possible? Also try to escape all "". - vins
I have shared my code here "pastebin.com/yTvJBKkX" - Venkatachalam Neelakantan

1 Answers

0
votes

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]