Search code examples
cucumberrest-assuredcucumber-java

Rest Assured with Cucumber: How to put the request information inside the HTML report


I would like to show my requests and responses details in my HTML report.

A feature file example:

Feature: Rest Assured under Cucumber POC

  Scenario: Azure Login Scenario
    Given Request specifications are set with base uri "https://login.microsoftonline.com/"
    When Azure Login Request Executed
    Then Verify Status Code is 200

The Runner class is:

@RunWith(Cucumber.class)

@CucumberOptions(
        features = "src/main/resources/features",
        glue = {""},
        tags = "@tests",
        plugin = {      "pretty",
                        "json:target/cucumber-reports/Cucumber.json",
                        "html:target/cucumber-reports"}//reporting plugin
)
public class CucumberRunner {}

The steps are:

@Given("Request specifications are set with base uri {string}")
public void setRequestsSpec(String baseUri){

    RequestSpecification spec = new RequestSpecBuilder()
            .setBaseUri(baseUri)
            .addFilter(new ResponseLoggingFilter())//log request and response for better debugging. You can also only log if a requests fails.
            .addFilter(new RequestLoggingFilter())
            .build();

    testContext().setRequestSpec(spec);
}

@When("^Azure Login Request Executed$")
public void azureLoginExecuted() {

    response =
    given()  //Add x-www-form-urlencoded body params:
        .formParam(GRANT_TYPE_KEY, GRANT_TYPE_VALUE)
        .formParam(AUTO_TEAM_CLIENT_ID_KEY, AUTO_TEAM_CLIENT_ID_VALUE)
        .formParam(AUTO_TEAM_CLIENT_SECRET_KEY, AUTO_TEAM_CLIENT_SECRET_VALUE)
        .formParam(RESOURCE_KEY, RESOURCE_VALUE)
    .when()
        .post(AUTO_TEAM_TENANT_ID + RESOURCE); //Send the request along with the resource

    testContext().setResponse(response);

    setAuthorizationToken();
}

@Then("Verify Status Code is {int}")
public void verifyStatusCode(int expected_repsonse_code) {
    testContext().getResponse().then().statusCode(expected_repsonse_code);
}

Currently I found out how to show those details only in my IntelliJ console:

For example:

@tests
Feature: Rest Assured under Cucumber POC

  @tests
  Scenario: Azure Login Scenario                                                            # src/main/resources/features/poc.feature:5
    Given Request specifications are set with base uri "https://login.microsoftonline.com/" # CommonStepsDefinitions.setRequestsSpec(String)
Request method: POST
Request URI:    https://login.microsoftonline.com/6ae4e000-b5d0-4f48-a766-402d46119b76/oauth2/token
Proxy:          <none>
Request params: <none>
Query params:   <none>

and more..

But the HTML report shows only:

enter image description here

Thank you!


Solution

  • I can give you some details which might not answer your question fully.

    In order to add data to Cucumber HTML report you can use:

    @After
    public void addDataToReport(Scenario scenario) { //scenario is provided from Cucumber
        scenario.write(string with the information about scenario);
    }
    

    It won't be formatted and I don't know how to change how the report displays it. Each of the messages will be under each Test Case.

    You have to, somehow, pass the information to @After hook.

    I hope someone else will answer with more details.

    EDIT:

    In order to store the info about what Scenario is running at the moment, or even in parallel, we can create a class to store necessary information, based on the Thread, so it will be Thread-safe.

    Let's create a class to store Scenario. Let's call it Storage

    public class Storage {
        private static final HashMap<Thread, Scenario> map = new HashMap<>();
    
        public static void putScenario(Scenario scenario) {
            map.put(Thread.currentThread(), scenario);
        }
    
        public static Scenario getScenario() {
            return map.get(Thread.currentThread());
        }
    }
    

    Now, we have to somehow get the Scenario. It can be achieved by using @Before hook like this:

    public class BeforeHook {
    
        @Before(order = 1)
        public void getScenario(Scenario scenario) {
            Storage.putScenario(scenario);
        }
    }
    

    @Before hooks are run before each scenario. We get the information about Scenario and put it in Storage so we know what Scenario is run on what Thread. Remember that hooks have to be reachable by the glue parameter in Cucumber Runner!

    And now, if we want to write additional information to the report:

        @Then("Data is saved to the report")
        public void data_is_saved_to_the_report() {
            System.out.println("Saving data to report");
            Storage.getScenario().write("Test data and stuff");
        }
    

    We just get current scenario from the Storage and use Scenario.write() method to add information to the report.

    It will look like this in the report: enter image description here