grasshopper7 / extentreports-cucumber6-adapter

Cucumber-JVM 6 Adapter for Extent Framework
https://ghchirp.online/2098/
MIT License
16 stars 15 forks source link

Example to add console output in cucumber reports #30

Closed SonaliSihra closed 3 years ago

SonaliSihra commented 3 years ago

@grasshopper7 I'm using the same version of cucumber and reports as shown in README and i have a requirement to show console logs in HTML report due to which the step failed. Is this feature available ? Can you please some example to do that? I'm using Junit for support.

grasshopper7 commented 3 years ago

The report displays the exception stacktrace of the failure in the test details tab. Not sure I understand completely. Can u add some more details about your requirement? Are u referring the Spark report as the HTML report is deprecated.

SonaliSihra commented 3 years ago

The report displays the exception stacktrace of the failure in the test details tab. Not sure I understand completely. Can u add some more details about your requirement? Are u referring the Spark report as the HTML report is deprecated.

Yes, currently I'm using spark html report. I'll try to explain my requirements from an example. Suppose there is one get api and I was asserting for 200 status code but for some backend error it is throwing error. That assert failed and shown as step failed in report. But my senior wants us to display the exact error message returned by that api for better representation. If this report is deprecated, there is any other way to do the same?

grasshopper7 commented 3 years ago

U are using the correct report - Spark. Catch the original exception when response is not 200 and throw it again. The report should display the exception stacktrace.

SonaliSihra commented 3 years ago

The report is just throwing assertion exception not the response ones. Can you please share an example on how to throw them to report?

grasshopper7 commented 3 years ago

Would be easier if u add the relevant step definition code lines here.

SonaliSihra commented 3 years ago
    @Given("Incorrect token is sent on user")
    public void incorrectTokenIsSentOnUser() {
        RestAssured.baseURI = loadTestData.getString("STG");
        String path = RestAssured.baseURI + "/user";

        values.put("access_token", loadTestData.getString("IncorrectToken"));

        Response res = CommonMethods.getRequest(path, values, headers);
        actualMessage = res.path("message");
        responseCode = res.statusCode();
    @Then("Error message is {string}")
    public void errorMessageIs(String message) {
        CommonMethods.verifyEquals(actualMessage, message, "Comparing unauthorized error message");
        CommonMethods.verifyEquals(responseCode, 401, "Comparing unauthorized error status code");
    }

Scenario for the same is: @stg Scenario: Check error message for incorrect token on /user Given Incorrect token is sent on user Then Error message is "Unauthorized"

Now here if for some reason my setp failed the report only shows the assertion error for example: image

Here, Along with this i want to add error message shared by the api (due to which the test failed) so that it can bring more clarity to the team. I'm printing them on my console and wanted some guidance to the same on reports.

SonaliSihra commented 3 years ago

Also, if this is not possible i have a LogFile.log in which i'm printing the responses is there any chance to add the logfile to the report so that the teams can take reference from it? I'm suggesting this is as a alternative from the above situation.

grasshopper7 commented 3 years ago

I am not that familiar with API testing, so these maybe dumb queries. Are these error messages in the console from the API in a separate process? If the test code is generating this logfile then u can use the error message to raise a runtime exception. Is this the code which should error if the server has an issue - Response res = CommonMethods.getRequest(path, values, headers);

SonaliSihra commented 3 years ago

@grasshopper7 Sharing code for that part:

    public static Response getRequest(String getPath, Map<String, String> values, Map<String, String> headers) {
        //Log.logInfo("sending GET request");
        Response jsonResponse = null;
        Log.logInfo(getPath);
        try {
            jsonResponse =
                    given()
                            .params(values)
                            .headers(headers)
                            .when()
                            .get(getPath);
        } catch (Exception e) {
            Log.logError("CommonMethods", "getRequest", "Exception occurred: " + e);
        }

        assert jsonResponse != null;
        Log.logInfo("getRequest response: " + jsonResponse.asString());
        return jsonResponse;

    }

And to log i'm using log4j:

    public static void logError(String className, String methodName,
                                String exception) {
        LOGGER.addAppender((Appender) appender);
        LOGGER.setLevel((Level) Level.INFO);
        LOGGER.info("ClassName :" + className);
        LOGGER.info("MethodName :" + methodName);
        LOGGER.info("Exception :" + exception);
        LOGGER.info("-----------------------------------------------------------------------------------");
    }
grasshopper7 commented 3 years ago

Rethrow the exception from the getRequest() method. U can use any other custom exception from ur project also.

} catch (Exception e) {
            Log.logError("CommonMethods", "getRequest", "Exception occurred: " + e);
           throw new RuntimeException(e);
 }
SonaliSihra commented 3 years ago

I tried with your suggestion and end up doing this in my step def to get desired results: if (!success) { String message = res.path("message"); throw new CucumberBackendException(message); } Thanks for your support @grasshopper7 .