When we build a keyword driven framework, we put more focus on actions which we want to execute. When we use standard reporting using TestNG , its require the multiple test methods which are executed and shown in the report.When its comes to Keyword Driven framework, we have single test method which runs all the actions coming from test cases keywords. to handle this we required custom reporting of each action execution.Now, how to handle it. Here I have created two classes Reported and result.
Before doing all we required a basic HTML template which is required to create a report. Following is template I have created
<html>
<head>
<title>Report generated on #DATETIME#</title>
<head>
<body>
<h3>Test results</h3>
<table>
<tr>
<th width="20%">Test Case</th>
<th width="10%">Step</th>
<th width="20%">Description</th>
<th width="10%">Result</th>
<th width="10%">Actions</th>
</tr>
<!-- INSERT_RESULTS -->
</table>
</body>
It looks like a below
Now created Reporter class as follows
package frameWorkUtility;
import config.Constants;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import static config.Constants.reportTemplatePath;
public class Reporter {
private static List<Result> details;
private static final String resultPlaceholder = "<!-- INSERT_RESULTS -->";
private static final String templatePath = reportTemplatePath;
public Reporter() {
}
public static void initialize() {
details = new ArrayList<Result>();
}
public static void report(String actualValue,String expectedValue,String Keyword, String testCaseId,String testStepId,String testDescription) {
if(actualValue.equals(expectedValue)) {
if(Keyword.contains("="))
{
Result r = new Result("====",Keyword,testCaseId,testStepId,testDescription);
details.add(r);
}
else
{ Result r = new Result("Pass",Keyword,testCaseId,testStepId,testDescription);
details.add(r);
}
} else {
if(Keyword.contains("="))
{
Result r = new Result("====",Keyword,testCaseId,testStepId,testDescription);
details.add(r);
}
else {
Result r = new Result("Fail", Keyword, testCaseId,testStepId,testDescription);
details.add(r);
}
}
}
public static void showResults() {
for (int i = 0;i < details.size();i++) {
System.out.println("Result " + Integer.toString(i) + ": " + details.get(i).getResult());
}
}
public static void writeResults() {
try {
String reportIn = new String(Files.readAllBytes(Paths.get(templatePath)));
for (int i = 0; i < details.size();i++) {
if(details.get(i).getTestCaseId().contains("Scenario"))
{
reportIn = reportIn.replaceFirst(resultPlaceholder, "<tr><td style=\"text-align: left;\">" + details.get(i).getTestCaseId() + "</td><td style=\"text-align: center;\">" + details.get(i).getTestStepId() + "</td><td style=\"text-align: center;\">" + details.get(i).getTestDescription() + "</td><td style=\"text-align: center;\">" + "</td><td style=\"text-align: center;\">"+details.get(i).getResultText() + "</td></tr>" + resultPlaceholder);
}
else {
reportIn = reportIn.replaceFirst(resultPlaceholder, "<tr><td style=\"text-align: center;\">" + details.get(i).getTestCaseId() + "</td><td style=\"text-align: center;\">" + details.get(i).getTestStepId() + "</td><td style=\"text-align: center;\">" + details.get(i).getTestDescription() + "</td><td style=\"text-align: center;\">" + details.get(i).getResult() + "</td><td style=\"text-align: center;\">" + details.get(i).getResultText() + "</td></tr>" + resultPlaceholder);
}
}
String currentDate = new SimpleDateFormat("dd-MM-yyyy_HHmmss").format(new Date());
String reportPath = Constants.reportOutputPath+"\\report_" + currentDate + ".html";
Files.write(Paths.get(reportPath),reportIn.getBytes(),StandardOpenOption.CREATE);
} catch (Exception e) {
System.out.println("Error when writing report file:\n" + e.toString());
}
}
}
And result class aas follows
package frameWorkUtility;
public class Result {
private String result;
private String resultText;
private String testCaseId;
private String testStepId;
private String testDescription;
public Result(String result,String resultText,String testCaseId,String testStepId,String testDescription) {
this.result = result;
this.resultText = resultText;
this.testCaseId=testCaseId;
this.testStepId=testStepId;
this.testDescription=testDescription;
}
public void setResult(String result) {
this.result = result;
}
public String getResult() {
return this.result;
}
public void setResultText(String resultText) {
this.resultText = resultText;
}
public String getResultText() {
return this.resultText;
}
public void setTestCaseId(String resultText) {
this.testCaseId = testCaseId;
}
public String getTestCaseId() {
return this.testCaseId;
}
public void setTestStepId(String testStepId) {
this.testStepId = testStepId;
}
public String getTestStepId() {
return this.testStepId;
}
public String getTestDescription() {
return this.testDescription;
}
public void setTestDescription(String description) {
this.testDescription = testDescription;
}
}
Two add values in reports we need to call report class present under-reported as
Reporter.report("true", "true", sActionKeyword, testId,testStepId,testDescription);
and finally after all execution call
Reporter.writeResults();
the above method responsible for creating the HTML report
Final reports look like as below
Let me know you inputs on it
0 on: "Writing Custom HTML report when building Keyword Driven Framework"