Jmeter - how to properly send request name and reason on assertion failure - java

I am new to jmeter (I am using version 3.3) and I created a test plan with the goal of sending an email on Assertion failure.
in this email I want to add some information about the name of the request and its reason
so I have added and if controller with this condition:
${JMeterThread.last_sample_ok}
a beanshell preprocess with this script:
and an SMTP Sampler with this body:
and a response assertion:
I want to get an email anytime it hits the condition of response code=500 and the info described above.
I got to receive the email on that condition but the body message is literally this:
${body}
this is the assertion result:
what am I doing wrong?
How to get what I need?
Thanks

Your prev variable is not defined in the Beanshell PreProcessor (it exists only for PostProcessor and Listener, I would suggest using ctx.getPreviousResult() instead where ctx stands for JMeterContextService instance.
You also need to enable your HTTP Request GET - posts as this will be this "previous result" you're interested in.
Be aware that starting from JMeter 3.1 it is recommended to use Groovy for any form of scripting so consider migrating to JSR223 PreProcessor and Groovy language on next available opportunity. You should be even able to re-use the same code as I don't see any Beanshell-specific features in it. See Apache Groovy - Why and How You Should Use It article for more information on Groovy scripting in JMeter.

The script is sending an email if the last sampler is ok, therefore the assertion is ok too, so the body variable won't hold any value.
Just adjust your if statement to !${JMeterThread.last_sample_ok}, and then move up your HTTP sampler HTTP Request - POST/articles above the if controller.
This will make your script send an email if the sampler failed and you will have the body variable holding the failure message.

Related

How to configure HTTP request body parser using Bigquery Java client library

I need to configure a parser which can add few default fields(such as default labels) in the request body of underlying BQ HTTP request whenever any query is run via this bqserv instance (like mentioned in the code snippet below). How to build bqserv?
bqserv.query(QueryJobConfiguration.newBuilder(query).build)
I found BigQueryOptions.newBuilder().setTransportOptions() to set transport options, but I am unable to figure out exactly how to configure/(or set a parser for) HTTP request body ? I came across com.google.cloud.spark.bigquery.repackaged.com.google.api.client.util.ObjectParser as well, but again not sure how to use it and create bqserv (class com.google.cloud.spark.bigquery.repackaged.com.google.cloud.bigquery.BigQueryImpl) object.

Is it possible to validate max length check in API Testing Using Selenium

I'm developing a automation script using selenium-cucumber-java with testNG combination for API testing.
In UI there is a some validation available as text box should accept 500 characters only, if more that text box will not allow to enter.
Same validation I should expect in API end point testing with help of rest assured. How to test max length check in rest assured API automation? Please help to resolve it, it is a new journey.
you need to send the request with the parameter value corresponding to that textbox, with more than 500 characters. it should throw you a response 400 with an error status message customized by your developer. You need to Assert the response code with the expected response code(400) and status message with the expected status message.
If you further want to validate, you may validate in the database the field is not updated.

Can I return non-JSON data from an AWS lambda called through API gateway?

I have a simple lambda in AWS that takes plain text (non-JSON) input and gives plain text (non-JSON) output, and it works when I test it in the Lambda Management Console. I'd like to call it by POSTing the input via HTTP, so I added API Gateway as a trigger, but it says "An error occurred during JSON parsing" - fair enough, because I didn't output JSON.
I found a number of similar questions here, but they focused on fixing the JSON. My question is whether it's possible to avoid it altogether. Can I configure API Gateway to just pass through the request and response bodies, or is there another way to get an HTTP request to my lambda that would allow that?

JMeter sending email if one of the HTTP requests fails

I planned to create Test Plan in my JMeter like below
Test Plan
Thread Group
Loop Controller
HTTP Request Sampler 1
HTTP Request Sampler 2
HTTP Request Sampler 3
HTTP Request Sampler 4
HTTP Request Sampler 5
HTTP Request Sampler 6
HTTP Request Sampler 7
HTTP Request Sampler 8
If Controller condition ${JMeterThread.last_sample_ok}==false
SMTP Sampler
I would like to run the test 24/7.
If one (or more) HTTP Request Sampler returns error for what ever reason, then execute the SMTP sampler and send email to me.
If none of the HTTP Request Sampler returns error , then do not execute SMTP sampler but keep repeating the loop forever.
I don't quite understand the if controller condition ${JMeterThread.last_sample_ok}.
What does JMeterThread.last_sample_ok mean?
Last_sample_ok means only the last HTTP Request sampler (in my case HTTP Request Sampler 8 only) ? If I want to check the condition for every sampler 1-8, what can be done here?
Is JMeterThread a JMeter Java class or Java object?
Besides last_sample_ok , what other method can be applied to JMeterThread object?
Thanks.
JMeterThread.last_sample_ok is a static String field of the JMeterThread class.
This line from the source code of JMeterThread class shows that LAST_SAMPLE_OK will be set to String representation of the parent.isSuccessful() boolean variable, where parent is a SampleResult object:
threadContext.getVariables().put(LAST_SAMPLE_OK, Boolean.toString(parent.isSuccessful()));
Basically, LAST_SAMPLE_OK will indicate if the last sample was successful (until it gets reset by the next sampler).
Yes, if JMeterThread.last_sample_ok is true then the last sampler was successful. In your case it is HTTP Request Sampler 8.
You can add all your HTTP Samplers as children of a Transaction Controller and select the "Generate a parent sample" checkbox.
This will make JMeter generate a parent sample for all the inner samples of a Transaction Controller. If one of the child samplers fails, the parent sample will fail as well.
Add your If Controller right after the Transaction Controller.
Now JMeterThread.last_sample_ok will relate to the generated parent sample.
JMeterThread is a class in the org.apache.jmeter.threads package .
LAST_SAMPLE_OK is not a method, but a static field. There is one more field and a number of methods in the JMeterThread class.
One way you could do this is using Jenkins Email Extension Plugin which you can set to email you when there is a failure on you build.
(so you could fail the build when the script has a failed HTTP request.)
https://wiki.jenkins-ci.org/display/JENKINS/Email-ext+plugin

How to parse a custom XML-style error code response from a website

I'm developing a program that queries and prints out open data from the local transit authority, which is returned in the form of an XML response.
Normally, when there are buses scheduled to run in the next few hours (and in other typical situations), the XML response generated by the page is handled correctly by the java.net.URLConnection.getInputStream() function, and I am able to print the individual results afterwards.
The problem is when the buses are NOT running, or when some other problem with my queries develops after it is sent to the transit authority's web server. When the authority developed their service, they came up with their own unique error response codes, which are also sent as XMLs. For example, one of these error messages might look like this:
<Error xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Code>3005</Code>
<Message>Sorry, no stop estimates found for given values.</Message>
</Error>
(This code and similar is all that I receive from the transit authority in such situations.)
However, it appears that URLConnection.getInputStream() and some of its siblings are unable to interpret this custom code as a "valid" response that I can handle and print out as an error message. Instead, they give me a more generic HTTP/1.1 404 Not Found error. This problem cascades into my program which then prints out a java.io.FileNotFoundException error pointing to the offending input stream.
My question is therefore two-fold:
1. Is there a way to retrieve, parse, and print a custom XML-formatted error code sent by a web service using the plugins that are available in Java?
2. If the above is not possible, what other tools should I use or develop to handle such custom codes as described?
URLConnection isn't up to the job of REST, in my opinion, and if you're using getInputStream, I'm almost certain you're not handling character encoding correctly.
Check out Spring's RestTemplate - it's really easy to use (just as easy as URLConnection), powerful and flexible. You will need to change the ResponseErrorHandler, because the default one will throw an exception on 404, but it looks like you want it to carry on and parse the XML in the response.

Categories

Resources