How to add HTML report and logs to the Cucumber test automation framework (6/7)
In this article we will utilize listeners implemented in the previous article and add reporting capabilities to the framework. We will use ExtentReports library for the HTML reports and Log4j2 for the console and file logging.
As usual, code from this article can be found here.
Add log4j2 and ExtentReports dependencies
First of all, add dependencies to the pom.xml file:
Add log4j2 configuration files
Configuration files for the log4j2 contains log message format, log file name and files rotation strategy.
Create log4j2.properties files at the both resources folders:
And text below to the files:
Add log messages to the code
We will log events occurred in the ListenerPlugin. Create LOG object at the ListenerPlugin:
Reporting to the HTML will be used the same way as log messages. We will implement TestReport class which will be write messages and screenshots into the HTML report file.
Create class TestReport and this code:
Method init initialize all objects in the class and create HTML file inside reports folder.
Method createTest add test to the HTML report.
Method closeThreadLocalCollections correctly finish work with the currentTest and currentTestCases collections.
The rest of the methods adds logs, screenshots and test results to the HTML report.
Add additional methods to the listener
We need to add few method to the SeleniumDriverManager. All created drivers object will be added to the list, also last created driver will available via method getCurrentDriver.
Final version of SeleniumDriverManager:
Then we add new methods to the ListenerPlugin and update old ones:
onTestRunStarted — execute method TestInitiazlization.init(),
onTestCaseStarted — add test to the HTML report,
onTestCaseFinished — print log message,
onTestRunFinished — close all created Selenium drivers and clear thread local collections,
onTestStepFinished — add screenshot from the current Selenium driver if test has @web tag,
onPassedTest — add passed test result to the HTML report,
onSkippedTest — add skipped test result to the HTML report,
onFailedTest- add failed test result to the HTML report,
Final look of the ListenerPlugin class.
Add report initialization
Add line below to the init method from the TestInitialization class:
Now, test initialization will be performed before all tests and we can remove this line from the step classes.
Add request/response logging to the RestAssured
Also, we want to log all API requests and responses. To achivee it, we utilize filters functionalities from RestAssured. Open TestInitializer, add LOG object and filter implementation to the init method:
Execute the tests
Final version of a code is here.
execute test via mvn test, and check log output. It contains log messages according to the format in the configuration. HTML report is added to the reports folder.
At the next article will integrate this framework with the AzureDevops builds!