What’s up Guys! Welcome to automationcalling.com
Reading Time: <5 Minutes
Integrating Rest Assured with Cucumber framework is quite easy in fact cucumber framework produce default report, but the challenging part is better readability of report, say for eg., what is the endpoint, request body, query param, response, etc., In this blog, we are going to see in detail about how to create better and quality report for Rest API automation.
Rest Assured Supporting Logs
Since from version 1.5 Rest Assured support Request and Response logging, this can be achieved as below in your Request and Response specification
Request Logging:
given().log().all(). .. // Log all request specification details including parameters, headers and body given().log().params(). .. // Log only the parameters of the request given().log().body(). .. // Log only the request body given().log().headers(). .. // Log only the request headers given().log().cookies(). .. // Log only the request cookies given().log().method(). .. // Log only the request method given().log().path(). .. // Log only the request path
Response Logging:
get("/x").then().log().body() get("/x").then().log().ifError() get("/x").then().log().all()
The above implementation in your code works like charm and produces detail logs which can be even written in the file. Please refer to the framework which has the same example Rest Assured Core + TestNG + Hamcrest
Filter Custom Logs
In this section, I’m going to explain how to implement custom logs and append in cucumber report.
What is Filter in Rest Assured?
A Filter is an interface that allows you to inspect and alter a request before it’s actually committed and also inspect and alter the response before it’s returned to the expectations. Filters can be used to implement custom authentication schemes, session management, logging, etc., Having said that, we do use Logging Filter to add custom report in our cucumber report.
RequestSpecification has a method name called “filter” which we can use to filter and generate custom logs for request and response object.
In order to generate custom logs, I’m implementing CustomLogFilter class which implement “Filter” interface that must expect to implement the following method.
So, the implementation of CustomLogFilter is as follow:
public class CustomLogFilter implements Filter { private StringBuilder requestBuilderLogs; private StringBuilder responseBuilderLogs; @Override public Response filter(FilterableRequestSpecification filterableRequestSpecification, FilterableResponseSpecification filterableResponseSpecification, FilterContext filterContext) { Response response = filterContext.next(filterableRequestSpecification, filterableResponseSpecification); requestBuilderLogs = new StringBuilder(); requestBuilderLogs.append("\n"); requestBuilderLogs.append("Request method: " + objectValidation(filterableRequestSpecification.getMethod())); requestBuilderLogs.append("\n"); requestBuilderLogs.append("Request URI: " + objectValidation(filterableRequestSpecification.getURI())); requestBuilderLogs.append("\n"); requestBuilderLogs.append("Form Params: " + objectValidation(filterableRequestSpecification.getFormParams())); requestBuilderLogs.append("\n"); requestBuilderLogs.append("Request Param: " + objectValidation(filterableRequestSpecification.getRequestParams())); requestBuilderLogs.append("\n"); requestBuilderLogs.append("Headers: " + objectValidation(filterableRequestSpecification.getHeaders())); requestBuilderLogs.append("\n"); requestBuilderLogs.append("Cookies: " + objectValidation(filterableRequestSpecification.getCookies())); requestBuilderLogs.append("\n"); requestBuilderLogs.append("Proxy: " + objectValidation(filterableRequestSpecification.getProxySpecification())); requestBuilderLogs.append("\n"); requestBuilderLogs.append("Body: " + objectValidation(filterableRequestSpecification.getBody())); requestBuilderLogs.append("\n"); requestBuilderLogs.append("******************************"); responseBuilderLogs = new StringBuilder(); responseBuilderLogs.append("\n"+"\n"+"\n"); responseBuilderLogs.append("Status Code: "+response.getStatusCode()); responseBuilderLogs.append("\n"); responseBuilderLogs.append("Status Line: "+response.getStatusLine()); responseBuilderLogs.append("\n"); responseBuilderLogs.append("Response Cookies: "+response.getDetailedCookies()); responseBuilderLogs.append("\n"); responseBuilderLogs.append("Response Content Type: "+response.getContentType()); responseBuilderLogs.append("\n"); responseBuilderLogs.append("Response Headers: "+response.getHeaders()); responseBuilderLogs.append("\n"); responseBuilderLogs.append("Response Body: "+"\n"+response.getBody().prettyPrint()); return response; } public String getRequestBuilder() { return requestBuilderLogs.toString(); } public String getResponseBuilder() { return responseBuilderLogs.toString(); } public String objectValidation(Object o) { if (o == null) return null; else return o.toString(); } }
The above code would filter and build logs that are required. The objectValidation method returns null in case any of parameter in request specification not available otherwise return as string type.
How to Implement Custom Logs in Cucumber Report
Cucumber supports Hooks, which are blocks of code that run before and after the execution of each scenario in the feature file.
cucumber.api.Scenario
Cucumber API provides an interface called Scenario which actually helps to achieve certain functions of pre and post-execution of scenarios. For eg.,
- Current Scenario status (Pass/Fail): Type getStatus();
- Attach Screenshot: void embed(byte[] var1, String var2);
- Write text to Cucumber report: void write(String var1);
For Custom log generation on cucumber report, we use method “void write(String var1”
- Refers instance variable (scenario) of current class
- Instantiating CustomLogFilter Class and refer to logFilter which is Object of Filter Interface
- Initialized logfilter object in request specification
- Verifying logfilter is instance of CustomLogFilter and write custom logs that were built in requestBuilderLogs and responseBuilderLogs using scenario.write hooks (cucumber api). This must be implemented where exactly you invoke request type “GET, PUT, POST, DELETE” etc.,
- Run your cucumber test using maven and see the report gets generated with detail Rest API request and response specification.
Hope! The above cucumber report shows in detail and better readable for debugging and better logging mechanism.
To play with this sample framework, please feel free to visit GitHub: Clone from here
Thanks for your time, Please do subscribe for more updates!
Leave a Reply