Extent Report in Kotlin

ExtentReportKotlin

   We know that TestNG itself providing an HTML report and the report will generate in the test-output folder of your project structure. In my previous articles, already discussed the implementation of Extent Report with TestNG+Java and NUnit+C#. Here, I would like to share the details on the implementation of the Extent Report using Kotlin language and controlled by TestNG.

    We know that Kotlin is a general-purpose, open-source, statically typed programming language initially designed for the JVM (Java Virtual Machine) and Android that combines object-oriented and functional programming features. Let’s discuss the implementation of the Extent Report using Kotlin language.

Extent Report using Kotlin

Prerequisites

Following are the prerequisites to implement Extent Report using Kotlin,

  • Install Java SDK 8 and above.
  • Install Eclipse or IntelliJ IDEA IDEs.
  • Install the Kotlin plugin in IDEs. Here I am using Eclipse as IDE and Kotlin plugin for Eclipse downloaded from https://marketplace.eclipse.org/content/kotlin-plugin-eclipse
  • The latest version of following maven dependencies:
    • testng
    • selenium-java
    • selenium-server
    • kotlin-test
    • kotlin-stdlib-jdk8
    • extentreports

Step-by-step procedure

Step 1: Create a maven project and add the above dependencies in pom.xml of the project.

Step 2: Create a Kotlin class to keep the logic to generate the Extent Report. Let say the class name as AutomationReport. Here I am using ITestListener interface of TestNG to control the executions and results. Below is the code snippet of the AutomationReport class to implements the ITestListener and declare the objects for ExtentSparkReporter, ExtentReports, and ExtentTest,

class AutomationReport : ITestListener {

public lateinit var sparkReporter: ExtentSparkReporter
public lateinit var extentReport: ExtentReports
public lateinit var extentTest: ExtentTest

…}

Step 3: Create an override method onStart() with logic to generate an HTML template for the test report,

override fun onStart(testContext: ITestContext) {
try {
sparkReporter = ExtentSparkReporter(System.getProperty(“user.dir”) + “/AutomationReport/”)
sparkReporter.config().setDocumentTitle(“Kotlin Automation”)
sparkReporter.config().setReportName(“Automation Execution Report”)
sparkReporter.config().setTheme(com.aventstack.extentreports.reporter.configuration.Theme.DARK)
extentReport = ExtentReports()
extentReport.attachReporter(sparkReporter)
extentReport.setSystemInfo(“Application Name”, “Kotlin Report Demo”)
extentReport.setSystemInfo(“Platform”, System.getProperty(“os.name”))
extentReport.setSystemInfo(“Environment”, “QA”)

} catch (e: Exception) {
e.printStackTrace()
}
}

Step 4: Create an override method onTestStart() with logic to collect current test case name and add it to the report,

override fun onTestStart(result: ITestResult) {
var testName: String = result.getMethod().getMethodName()
extentTest = extentReport.createTest(testName)
}

Step 5: Create an override method onTestSuccess() with logic to add pass status to the report,

override fun onTestSuccess(result: ITestResult) {
var testName: String = result.getMethod().getMethodName()
try {
extentTest.log(
Status.PASS,
MarkupHelper.createLabel(testName + ” Test Case PASSED”, ExtentColor.GREEN)
)
} catch (e: Exception) {
e.printStackTrace()
}
}

Step 6: Create an override method onTestSkipped() with logic to add skip status to the report,

override fun onTestSkipped(result: ITestResult) {
var testName: String = result.getMethod().getMethodName()
try {
extentTest.log(
Status.SKIP,
MarkupHelper.createLabel(testName + ” Test Case SKIPPED”, ExtentColor.ORANGE)
)
} catch (e: Exception) {
e.printStackTrace()
}
}

Step 7: Create an override method onTestFailure() with logic to add fail status to the report,

override fun onTestFailure(result: ITestResult) {
var driver: WebDriver
var currentClass = result.getInstance()
var testName: String = result.getMethod().getMethodName()
try {
driver = (currentClass as AutomationBase).getDriverInstance()
var screenshotPath = Utilities().screenshotCapture(driver, result.getName())
extentTest.log(
Status.FAIL,
MarkupHelper.createLabel(testName + ” Test Case FAILED”, ExtentColor.RED)
)
extentTest.log(
Status.FAIL,
MarkupHelper.createLabel(“Reason for Failure: ” + result.getThrowable().toString(), ExtentColor.RED)
)
extentTest.addScreenCaptureFromPath(screenshotPath)
} catch (e: Exception) {
e.printStackTrace()
}
}

Step 8: Create an override method onFinish() with logic to store HTML report to the specified path  and flush extent report instance,

override fun onFinish(testContext: ITestContext) {
try {
extentReport.flush()
val dateFormat = SimpleDateFormat(“dd-MMM-yyyy_HH-mm-ss”)
val date = Date()
val filePathdate: String = dateFormat.format(date).toString()
var actualReportPath: String = System.getProperty(“user.dir”) + “/AutomationReport/” + “index.html”
File(actualReportPath).renameTo(
File(
System.getProperty(“user.dir”) + “/AutomationReport/”
+ “Automation_Report_” + filePathdate + “.html”
)
)
} catch (e: Exception) {
e.printStackTrace()
}
}

Step 9: Create another class called Utilities to keep common utility functions required for automation. Here I just added one utility to capture the screenshot. In onTestFailure() method, I already used a method called screenshotCapture(). Below is the code snippet to capture the screenshot,

fun screenshotCapture(driver: WebDriver, fileName: String): String {
var destination: String = “”
try {
var scrFile = (driver as TakesScreenshot).getScreenshotAs(OutputType.FILE)
var dateFormat = SimpleDateFormat(“yyyyMMddHHmmss”)
var cal = Calendar.getInstance()
var path = File(“Failure_Screenshots”).getAbsolutePath()
destination = path + “/” + fileName + dateFormat.format(cal.getTime()) + “.png”
scrFile.copyTo(File(destination))
} catch (e: Exception) {
e.printStackTrace()
}
return destination
}

Step 10: Prior to starting the automation execution, you have to map the AutomationReport class to your test runner class (the class which starting your driver instance within TestNG annotation). You can represent AutomationReport class in test runner class as below,

@Listeners(AutomationReport::class)

Step 11: Now you can run your test cases using testng.xml and once execution complete the HTML report will generate inside the AutomationReport folder in the project structure.

     I hope you got an idea of Extent Report implementation using Kotlin language. Try to use the above step-by-step procedure in your automation world and explore it.

make it perfect!

 

Advertisement

2 thoughts on “Extent Report in Kotlin

Add yours

  1. Please do let me know which version of extent report is supporting Android Espresso Kotlin as with Extent 5.0.4 while running my testng test case we are getting below error
    Caused by: java.lang.ClassNotFoundException: com.aventstack.extentreports.reporter.ExtentSparkReporter

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: