Non text response data received in Jmeter sampler response - jmeter

When I running Jmeter in non gui mode Im getting response error in Jmeter
Im using below mentioned settings in the simple data writer in the script
I want to know whether server under test has thrown this Non-text response data error or error is something else and Jmeter is not able to interpret it so it is showing that.
Can someone help me understand this.
Thank you!!!

This error will occur when SampleResult.getDataType() function returns anything other than text
You can try writing the response data using JSR223 Listener and some custom Groovy code, for instance:
if (!prev.isSuccessful()) {
new File('response.bin').bytes = prev.getResponseData()
}
you can amend this response.bin filename to include the current thread number, iteration, name of the sampler and so on

Related

Extracting response and using While Controller to wait for response in Jmeter

All, I need additional assistance. I have tried researching and was using this following resource guide. https://www.blazemeter.com/blog/using-while-controller-jmeter
Problem
I need to Implement a way to wait for a specific Response from the server and record the time it takes from the start of the request to the end where I get a response of Complete in Jmeter. I have been looking at while controller and researching it.
Problem#1
If I add a while controller, my request failed because its adding multiple token. If I leave it without the while controller then it only gets 1 bearer token and it is able to submit a get request and get the response appropriately. I am suspecting there is a pre-procession somewhere else in my script that doing this...
Uses beanshell preprocesser to grab the token
sampler.getHeaderManager().add(new Header("Authorization","Bearer " + vars.get("BEARER")));
Problem #2
I have a GET Request to check the status of the request. Response in the BODY comes back as this json
"{"Status": "RECEIVED", "DllUrl": "", "Message": " "}"
I need it to continue checking for the status of COMPLETE……..also may need to check for failed as well
Was looking at regular Expression extractor to get the status and only stop until it finds COMPLETE status. That way I can measure time start and end time for completion. Any help and guidance will be helpful.
You're adding a header and not removing the previous one therefore it adds another header on each iteration.
In fact you don't need to do any scripting for this, you can just define the header in the HTTP Header Manager:
With regards to extracting this Status value, it's better to do it using JSON Extractor configured like:
Once done you can use the following __jexl3() function to "wait" till the operation finishes:
${__jexl3("${status}" != "COMPLETE",)}

How to save test result as pass and failed in jmeter in csv file

I am new in jmeter , I have created multiple HTTP Request in a single thread group , Now what i am trying is save all the HTTP Request that is http request with response code 200 in csv file as TestCase pass and http request with response code other than 200 as failed. As I want to execute each and every HTTP request one by one and store result in csv file
here is my beanshell PostProcessor
import java.io.*;
import org.apache.jmeter.services.FileServer;
File f=new File("E:\\apache-jmeter-5.2.1\\apache-jmeter-5.2.1\\bin\\JmeterProResult\\testResult.csv");
FileWriter fw=new FileWriter(f);
BufferedWriter bw=new BufferedWriter(fw);
var rc = prev.getResponseCode();
if(rc.equals("200")){
bw.write("test is passed");
}
else{
bw.write("test is failed");
}
bw.close();
fw.close();
which is generating output as show only one result but I run two successful http request
here is mine jmeter structure-- I want to execute each and every HTTP request
here my desired output should look like
Your approach will work only if you have 1 virtual user, any concurrency will lead to a race condition when 2 threads will be writing the same file resulting in concurrent modification and leading to the data loss
Since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting so you will need to reconsider your "beanshell" approach in any case
So I would recommend using Flexible File Writer plugin which allows you to store various request/response details into a file in a "safe" way.

How to check the response assertion is passed or failed in Jmeter using beanshell

I want to save the request and response of the sample into a text file, if response assertion is failed.
Here I can save the request and response it to a text file. But I am struck where,
how to check the whether response assertion is passed or failed for the respective sample.
How to validate the is current "Http Request" sample is passed or failed?
Thanks In advance.
Add Beanshell Listener as a child of your HTTP Request sampler which has Assertion(s)
Put the following code into "Script" area:
import org.apache.jmeter.assertions.AssertionResult;
for (AssertionResult assertionResult : sampleResult.getAssertionResults()) {
if (assertionResult.isFailure()) {
//do what you need here
}
}
As Listeners are executed after assertions the listener will have information regarding all assertion results therefore you will be able to conditionally store request/response details into a file.
Be aware that starting from JMeter version 3.0 the Groovy scripting engine is being shipped with JMeter so consider switching to JSR223 Listener and replacing all your Beanshell Test Elements with the JSR223 equivalents, performancewise it would be much better.

How do I resend a sampler upon a failed assertion on JMeter?

I am designing a load test in JMeter.
With the current application that we have whenever a HTTP request is sent, the web server will very occasionally send back a page with a message. To get around this we just have to reload the page. This page could come up for literally any HTTP request.
Is there any way to design a test in JMeter where when a sampler fails, the sampler simply retries?
I'm not sure how I can get a Beanshell sampler to resend a HTTP request.
It is possible via additional Beanshell Assertion
You can re-run arbitrary sampler from the Beanshell Assertion as simple as
ctx.getCurrentSampler().sample(null);
Add a Beanshell Assertion after all other assertions. It matters as assertions are being executed upside-down.
Put the following code into Beanshell Assertion's "Script" area (just change "message" to what your server returns on error.
import org.apache.jmeter.samplers.SampleResult;
if (new String(ResponseData).equals("message")) {
SampleResult result = ctx.getCurrentSampler().sample(null);
if (result.getResponseDataAsString().equals("message")) {
Failure = true;
} else {
SampleResult.setSuccessful(true);
}
}
You'll have only one result recorded.
If assertion passes 1st time - it'll be successful
If assertion fails 1st time and passes 2nd time - it'll be successful
If assertion fails 2 times - it'll be recorded as failed.
For extended information on Beanshell scripting check out How to use BeanShell: JMeter's favorite built-in component guide.
Create such hierarchy:
Thread Group (1 user, 1 second ramp-up, forever)
-While Controller (empty condition = forever)
--Counter (start – 1, increment – 1, reference name – counter)
--HTTP request
---Timer (I prefer constant Timer, responseble for pause betwee retrying)
---BeanShell Post Processor
BeanShell Post Processor should contains(pseudo code):
if(Integer.parseInt(vars.get("counter")>5)
{
prev.setSuccessful(false);
prev.setStopTestNow(true);
}
if(successCondition)
{
prev.setStopTest(true);
}
There is no direct way to achieve it, but I think you can use While controller in conjuction with Regex extractor to resend the failed requests.
Logical flow could be,
1. HTTP request
2. Regex extractor post processor - check response contains failure extract value in msg variable, default is success
3. While controller - run till msg=failure, default value of msg is success
Example screenshot,
Let me know if this works.

Jmeter Script-Error Message in Response Data but Successful result in sample Result

While running Jmeter scripts, one of the step is failing but Successful Result is coming in Sample Result as:
Sample Count: 1
Error Count: 1
Response code: 200
Response message: OK
But error message is coming in Response Data as:
Unfortunately, we could not delete your entire itinerary because of a database synchronization error.
If you could please re-load your itinerary and try again, we would appreciate it.
Thank you for your patience.
I need to re-submit the Request for that i am using the While loop comparing the Response Data then try to resubmit the request which is not working as.
if(ResponseData.equals("Unfortunately")==true)
{
log.error("Database synchronization error...Re sending Request");
vars.put("resubmitflag","true");
}
The problem i believe that i am not using correct functions for it as
ResponseData.equals will not work as how we can compare whole response data
which is not possible.
Kindly anyone help how to proceed and what functions need to use for that.
If you're talking about Beanshell Post Processor, there are a couple of issues with your code.
ResponseData isn't something, which is available to Beanshell Post Processor. You need either use data (which is byte array previous sampler response representation) or prev (which stands for SampleResult class for previous sampler).
equals method checks 2 strings for being identical. In your case you need to consider either startsWith or even contains method.
See following code samples for reference
String response = new String(data);
if (response.contains("Unfortunately")){
log.error("Database synchronization error...Re sending Request");
vars.put("resubmitflag","true");
}
or
String response = prev.getResponseDataAsString();
.....
See How to use BeanShell guide for more details.
Hope this helps

Resources