How to display a customised error message which is based on response
Is there a way to show custom error messages when the request fails
For example, when the response is 403, I want to customize an error message and that error message should be displayed either in assertion or data sink. Is it possible? Any help will be very much appreciated.
Thanks in advance
You can create a Groovy script after the request step.
This line shows how to get the status code from a named request request...
// The bit in brackets is the test step name. Must be spelt correctly. def status = testRunner.testCase.testSteps["REST Request"].testRequest.response.responseHeaders["#status#"];
From here you can decide what to do.
If you assert, and the status fails the assertion, then the test will fail and stop. This might be fine and at least you'll be able to see the custom error message.
Here's an Groovy script step example using assertion.
def status = testRunner.testCase.testSteps["REST Request"].testRequest.response.responseHeaders["#status#"]; assert status.toString().contains("300"), "Here is my custom fail message" log.info("Yes! If we got this far, then the assertion passed.");
If you don't want to fail, but instead simply report in a datasink, then play with this example.
For example purposes, create a Groovy script step and call it "Check Request Status - Groovy Script".
def status = testRunner.testCase.testSteps["REST Request"].testRequest.response.responseHeaders["#status#"]; def answer = ""; if (status.toString().contains("200")) { answer = "All's good. Nothing to see here"; } else if (status.toString().contains("404")) { answer = "Oh dear, looks like the service isn't there"; } else if (status.toString().contains("503")) { answer = "Oh dear, looks like the service blew up"; } else { answer = "We got status ${status.toString()} back from the web service, don't know what to do with this one"; } return answer;
Then, in the datasink screen, you can add a result column to report the answer from the Groovy script step....