Saturday, March 21, 2020

Resst Assured Custom Filter

We can log all Request And Response using log.all() method in console but we can't log it within log file or can't customize the log what we need to print.

Here we will learn how we can configure the filter to store the request and response in logfile for future purpose like debug.

We will follow the below steps for creating the CustomFilter.

Step 1:

Below pom.xml file is required for sample RestAssured project

<project xmlns=""
        <!-- -->

        <!-- -->

        <!-- -->

        <!-- -->

        <!-- -->


Step 2:

Now we will create a class for CustomLoggingFilter, this class implements Filter class and override the filter method of Filter class.

package com.api.automation.test;

import java.nio.charset.Charset;


import io.restassured.filter.Filter;
import io.restassured.filter.FilterContext;
import io.restassured.filter.log.LogDetail;
import io.restassured.internal.print.RequestPrinter;
import io.restassured.internal.print.ResponsePrinter;
import io.restassured.response.Response;
import io.restassured.specification.FilterableRequestSpecification;
import io.restassured.specification.FilterableResponseSpecification;

public class CustomLoggingFilter implements Filter {


    public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec,FilterContext ctx) {
        Response response =, responseSpec);


        // Storing Request in String
        String requestString = RequestPrinter.print(requestSpec, requestSpec.getMethod(), requestSpec.getURI(), LogDetail.ALL, new PrintStream(new WriterOutputStream(new StringWriter(),Charset.defaultCharset())), true);

        // Storing Response in String
        String responseString = ResponsePrinter.print(response, response, new PrintStream(new WriterOutputStream(new StringWriter(),Charset.defaultCharset())), LogDetail.ALL, true);

        // Print request in String
        System.out.println("Print Request: " + requestString);

        // Print response in String
        System.out.println("Print Response: " + responseString);

         * Logging the response depends on response status code


         * int statusCode = response.getStatusCode(); if (statusCode >= 400 &&
         * statusCode <= 500) {
         * System.out.println("****************************************" +
         * responseString); } else { System.out.println(" (" + statusCode+ ")"); }

        return response;


Step 3:

Now we will write the test class to implement the CustomLoggingFilter.

We will add this custom filter in RequestSpecification.

package com.api.automation.test;

import org.testng.annotations.Test;

import io.restassured.RestAssured;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.http.ContentType;
import io.restassured.specification.RequestSpecification;

public class ValidateAPI {

    public void validateSimpleAPI() {
        RequestSpecification requestSpecification = new RequestSpecBuilder().setBaseUri("")

                .addFilter(new CustomLoggingFilter())


Sunday, March 1, 2020

DataTable Handling With Cucumber 4

Previously in older version of  Cucumber we can transform Datatable as per our own object easily but now we need to implement TypeRegistryConfigurer in our code.

Here we will discuss on it.

Step 1:

Create a Employee class as below

public class EmployeeDTO {

    private String username;
    private String password;

    public EmployeeDTO(String username, String password) {
        this.username = username;
        this.password = password;

    public String getUserName() {
        return username;

    public void setUserName(String userName) {
        this.username = userName;

    public String getPassword() {
        return password;

    public void setPasword(String password) {
        this.password = password;


Step 2: 

Now we need to implemnt TypeRegistryConfigurer as below

import java.util.List;
import java.util.Locale;
import java.util.Map;

import io.cucumber.core.api.TypeRegistry;
import io.cucumber.core.api.TypeRegistryConfigurer;
import io.cucumber.datatable.DataTableType;
import io.cucumber.datatable.TableEntryTransformer;
import io.cucumber.datatable.TableRowTransformer;
import io.cucumber.datatable.TableCellTransformer;

public class EmployeeRegistryConfigurer implements TypeRegistryConfigurer {

    public Locale locale() {
        // TODO Auto-generated method stub
        return Locale.ENGLISH;

//RowTransformer is List of Objects List<EmployeeDTO>  
    public void configureTypeRegistry(TypeRegistry typeRegistry) {
        typeRegistry.defineDataTableType(new DataTableType(EmployeeDTO.class,new TableRowTransformer<EmployeeDTO>() {

            public EmployeeDTO transform(List<String> row) throws Throwable {
                // TODO Auto-generated method stub
                return new EmployeeDTO(row.get(0), row.get(1));

//CellTransformer is List of List of Objects List<List<EmployeeDTO>> employeeDetails  
//    @Override
//    public void configureTypeRegistry(TypeRegistry typeRegistry) {
//        typeRegistry.defineDataTableType(new DataTableType(EmployeeDTO.class, new TableCellTransformer<EmployeeDTO>() {
//            @Override
//            public EmployeeDTO transform(String cell) throws Throwable {
//                return new EmployeeDTO(cell, cell);
//            }
//        }));
//EntryTransformer is List of Maps of Keys to Values List<EmployeeDTO>  
//        @Override
//        public void configureTypeRegistry(TypeRegistry registry) {
//            registry.defineDataTableType(new DataTableType(EmployeeDTO.class, new TableEntryTransformer<EmployeeDTO>() {
//                @Override
//                public EmployeeDTO transform(Map<String, String> entry) throws Throwable {
//                    return new EmployeeDTO(entry.get("username"),entry.get("password"));
//                }
//            }));
//    }

Step 3:

Now we can call this as below:

public class Steps {

    @Given("UserName and Password.")
    public void username_and_Password() {

    @Given("UserName and Password as below.")
    public void username_and_Password_as_below(List<EmployeeDTO> employeeDetails) {