com.auto.common.testng.reporter.TestNGEmailReporter Maven / Gradle / Ivy
package com.auto.common.testng.reporter;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.IReporter;
import org.testng.ISuite;
import org.testng.ISuiteResult;
import org.testng.ITestContext;
import org.testng.ITestNGMethod;
import org.testng.TestListenerAdapter;
import org.testng.xml.XmlSuite;
public class TestNGEmailReporter extends TestListenerAdapter implements IReporter {
private static Logger logger = LoggerFactory.getLogger(TestNGEmailReporter.class);
private static PrintWriter f_out;
private static String outputDir;
NumberFormat nf = NumberFormat.getInstance();
private String background_color = "background-color: #004040; border: 0px solid grey; height: 25px; color: white; font-family: Georgia;";
public void generateReport(List arg0, List suites, String outdir) {
try {
outputDir = "target/custom-test-reports";
} catch (Exception ex) {
ex.printStackTrace();
}
try {
f_out = createWriter(outputDir);
} catch (IOException e) {
e.printStackTrace();
}
startHtmlPage(f_out);
generateTestExecutionStatus(suites);
endHtmlPage(f_out);
f_out.flush();
f_out.close();
}
private void generateTestExecutionStatus(List suites) {
int totalPassedMethods = 0;
int totalFailedMethods = 0;
int totalSkippedMethods = 0;
int totalSkippedConfigurationMethods = 0;
int totalFailedConfigurationMethods = 0;
int totalMethods = 0;
int suite_totalPassedMethods = 0;
int suite_totalFailedMethods = 0;
int suite_totalSkippedMethods = 0;
String suite_passPercentage = "";
String suiteName = "";
ITestContext overview = null;
HashMap dashboardReportMap = new HashMap();
for (ISuite suite : suites) {
suiteName = suite.getName();
Map tests = suite.getResults();
for (ISuiteResult r : tests.values()) {
overview = r.getTestContext();
totalPassedMethods = overview.getPassedTests().getAllMethods().size();
totalFailedMethods = overview.getFailedTests().getAllMethods().size();
totalSkippedMethods = overview.getSkippedTests().getAllMethods().size();
totalMethods = overview.getAllTestMethods().length;
nf.setMaximumFractionDigits(2);
nf.setGroupingUsed(true);
ITestNGMethod[] allTestMethods = overview.getAllTestMethods();
String browser = "NA";
String browser_version = "NA";
String platform = "NA";
if (platform == null || platform.trim().length() == 0) {
platform = "Windows XP";
}
if (browser_version == null || browser_version.trim().length() == 0) {
browser_version = "N/A";
}
if (browser == null || browser.trim().length() == 0) {
browser = "N/A";
}
if (!(dashboardReportMap.containsKey(""))) {
if (browser_version.equalsIgnoreCase("N/A")) {
browser_version = "";
}
dashboardReportMap.put("", "os1~" + platform + "|browser1~" + browser + browser_version
+ "|testcase_count_1~" + totalMethods + "|pass_count_1~" + totalPassedMethods
+ "|fail_count_1~" + totalFailedMethods + "|skip_count_1~" + totalSkippedMethods
+ "|skip_conf_count_1~" + totalSkippedConfigurationMethods + "|fail_conf_count_1~"
+ totalFailedConfigurationMethods);
} else {
for (String key : dashboardReportMap.keySet()) {
if (key.equalsIgnoreCase("")) {
if (browser_version.equalsIgnoreCase("N/A")) {
browser_version = "";
}
String value = dashboardReportMap.get(key);
int index = StringUtils.countMatches(value, "#") + 1;
index += 1;
value = value + "#" + "os" + index + "~" + platform + "|browser" + index + "~" + browser
+ browser_version + "|testcase_count_" + index + "~" + totalMethods
+ "|pass_count_" + index + "~" + totalPassedMethods + "|fail_count_" + index + "~"
+ totalFailedMethods + "|skip_count_" + index + "~" + totalSkippedMethods
+ "|skip_conf_count_" + index + "~" + totalSkippedConfigurationMethods
+ "|fail_conf_count_" + index + "~" + totalFailedConfigurationMethods;
dashboardReportMap.put(key, value);
}
}
}
suite_totalPassedMethods += totalPassedMethods;
suite_totalFailedMethods += totalFailedMethods;
suite_totalSkippedMethods += totalSkippedMethods;
try {
suite_passPercentage = nf
.format(((float) suite_totalPassedMethods / (float) (suite_totalPassedMethods
+ suite_totalFailedMethods + suite_totalSkippedMethods)) * 100);
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
}
StringBuilder dashboardResults = new StringBuilder();
dashboardResults.append("");
int total_browser_combinations = 0;
int total_unique_testcases = 0;
for (String key : dashboardReportMap.keySet()) {
String module = key;
String value = dashboardReportMap.get(key);
String[] values = value.split("#");
int testcase_count = 0;
int pass_count = 0;
int fail_count = 0;
int skip_count = 0;
int skip_conf_count = 0;
int fail_conf_count = 0;
for (String val : values) {
String[] tokens = val.split("\\|");
for (String token : tokens) {
if (token.contains("testcase_count")) {
testcase_count = testcase_count + Integer.parseInt(token.split("~")[1]);
}
if (token.contains("pass_count")) {
pass_count = pass_count + Integer.parseInt(token.split("~")[1]);
}
if (token.contains("fail_count")) {
fail_count = fail_count + Integer.parseInt(token.split("~")[1]);
}
if (token.contains("skip_count")) {
skip_count = skip_count + Integer.parseInt(token.split("~")[1]);
}
if (token.contains("skip_conf_count")) {
skip_conf_count = skip_conf_count + Integer.parseInt(token.split("~")[1]);
}
if (token.contains("fail_conf_count")) {
fail_conf_count = fail_conf_count + Integer.parseInt(token.split("~")[1]);
}
}
}
logger.debug("Value: " + value);
String[] sub = value.split("#");
String temp = "";
for (String s : sub) {
s = s.substring(0, s.indexOf("fail_count"));
temp = temp + s;
}
temp = temp.substring(0, temp.lastIndexOf("|"));
temp = temp.replace(" ", "%20");
NumberFormat nformat = NumberFormat.getInstance();
nformat.setMaximumFractionDigits(2);
nformat.setGroupingUsed(true);
String passPercent = nformat
.format(((float) pass_count / (float) (pass_count + fail_count + skip_count)) * 100);
String finalStr = "[";
String[] val = dashboardReportMap.get(key).split("#");
int unique_testcase = 0;
int limit = val.length - 1;
for (int i = 0; i < val.length; i++) {
String testCaseCount = (val[i].split("\\|")[2]).split("~")[1];
int next = Integer.parseInt(testCaseCount);
if (next > unique_testcase) {
unique_testcase = next;
}
finalStr = finalStr + testCaseCount + " T * 1 B]";
if (i != limit) {
finalStr += " + [";
}
}
String finalString = "";
if ((unique_testcase * values.length) != (pass_count + fail_count + skip_count)) {
finalString = "" + (pass_count + fail_count + skip_count)
+ "";
} else {
finalString = String.valueOf((pass_count + fail_count + skip_count));
}
String passCount = "";
String failCount = "";
String skipCount = "";
if (pass_count > 0) {
passCount = ""
+ pass_count + " ";
} else {
passCount = ""
+ pass_count + " ";
}
if (fail_count > 0) {
failCount = ""
+ fail_count + " ";
} else {
failCount = ""
+ fail_count + " ";
}
if (skip_count > 0) {
skipCount = ""
+ skip_count + " ";
} else {
skipCount = ""
+ skip_count + " ";
}
dashboardResults
.append(""
+ module
+ " "
+ unique_testcase
+ " "
+ values.length
+ " "
+ passCount
+ failCount
+ skipCount
+ ""
+ finalString
+ " "
+ passPercent + " %" + " ");
if (total_browser_combinations < values.length) {
total_browser_combinations = values.length;
}
total_unique_testcases += unique_testcase;
}
dashboardResults.append("
");
String suite_pass = "";
String suite_fail = "";
String suite_skip = "";
if (suite_totalPassedMethods > 0) {
suite_pass = ""
+ suite_totalPassedMethods + " ";
} else {
suite_pass = ""
+ suite_totalPassedMethods + " ";
}
if (suite_totalFailedMethods > 0) {
suite_fail = ""
+ suite_totalFailedMethods + " ";
} else {
suite_fail = ""
+ suite_totalFailedMethods + " ";
}
if (suite_totalSkippedMethods > 0) {
suite_skip = ""
+ suite_totalSkippedMethods + " ";
} else {
suite_skip = ""
+ suite_totalSkippedMethods + " ";
}
f_out.println(""
+ "Execution Summary "
+ "Test Suite Name "
+ "# Unique TestCases "
+ "# Passed "
+ "# Failed "
+ "# Skipped "
+ "# Failed + Skipped "
+ "Success Rate "
+ "# Failure Rate "
+ ""
+ suiteName
+ " "
+ total_unique_testcases
+ " "
+ suite_pass
+ suite_fail
+ ""
+ (total_unique_testcases - (suite_totalPassedMethods + suite_totalFailedMethods))
+ " "
+ ""
+ (total_unique_testcases - suite_totalPassedMethods)
+ " "
+ nf.format(((float) suite_totalPassedMethods / (float) (total_unique_testcases)) * 100) + " %" + " "
+ ""
+ nf.format(100 - ((float) suite_totalPassedMethods / (float) (total_unique_testcases)) * 100) + " %" + "
");
f_out.flush();
f_out.println("
");
String failedTest = "true";
if (failedTest.equalsIgnoreCase("true")) {
String jiraURL = "";
String testlinkURL = "";
String testlinkPrefix = "";
String BUILD_URL = "";
String SLNUM = "";
try {
jiraURL = "https://jira.corp.olacabs.com";
testlinkURL = "https://jira.corp.olacabs.com";
testlinkPrefix = "OSP";
} catch (Exception e) {
e.printStackTrace();
}
int count = 1;
String testIncludePassTest = "true";
String tableSummary = "Failed and Skipped Tests Summary";
if (testIncludePassTest.contains("true")) {
tableSummary = "Tests Summary";
}
StringBuilder[] sBuilder = new StringBuilder[3];
sBuilder[0] = new StringBuilder();
sBuilder[1] = new StringBuilder();
sBuilder[2] = new StringBuilder();
for (ISuite suite : suites) {
suiteName = suite.getName();
boolean isHeaderPrinted = false;
Map tests = suite.getResults();
for (ISuiteResult r : tests.values()) {
overview = r.getTestContext();
if (overview.getAllTestMethods().length > 0 && !isHeaderPrinted) {
f_out.println(""
+ ""
+ tableSummary
+ " "
+ " S.No "
+ "Test ID "
+ "Component "
+ "Feature "
+ "Owner Name "
+ "Test Description "
+ "Bug ID "
+ "Status "
+ " ");
isHeaderPrinted = true;
}
Collection passedTestCases = overview.getPassedTests().getAllMethods();
Collection failedTestCases = overview.getFailedTests().getAllMethods();
Collection skippedTestCases = overview.getSkippedTests().getAllMethods();
StringBuilder current = null;
for (ITestNGMethod method : overview.getAllTestMethods()) {
if (passedTestCases.contains(method)) {
if (!testIncludePassTest.contains("true")) {
continue;
}
}
if (failedTestCases.contains(method)) {
current = sBuilder[0];
} else if (skippedTestCases.contains(method)) {
current = sBuilder[1];
} else if (passedTestCases.contains(method)) {
current = sBuilder[2];
} else {
current = sBuilder[1];
}
current.append("");
current.append("");
current.append(SLNUM);
current.append(" ");
String[] groups = method.getGroups();
String component = "", testId = "", bugids = "", feature = "", owner = "";
for (String group : groups) {
if (group.contains("Component:")) {
component = group.split("Component:")[1];
}
if (group.contains("Feature:")) {
feature = group.split("Feature:")[1];
}
if (group.contains("Owner:")) {
owner = group.split("Owner:")[1];
}
if (group.contains(testlinkPrefix + "-") || group.contains("cm-")) {
testId = group;
}
if (group.contains("Bug:")) {
bugids = group;
}
}
/*Testcase ID*/
current.append("");
if (!testId.isEmpty()) {
String[] split = testId.split(",");
int counter = 0;
for (String tc : split) {
if (counter != 0) {
current.append("
");
}
current.append("" + tc + " ");
counter++;
}
counter = 0;
} else {
current.append("");
}
current.append(" ");
/*Component Name*/
current.append("");
if (!component.isEmpty()) {
String[] reqs = component.replace("Component:", "").split(",");
for (int i = 0; i < reqs.length; i++) {
current.append(reqs[i]);
}
} else {
current.append("");
}
current.append(" ");
/*Feature Name*/
current.append("");
if (!feature.isEmpty()) {
String[] reqs = feature.replace("Feature:", "").split(",");
for (int i = 0; i < reqs.length; i++) {
current.append(reqs[i]);
}
} else {
current.append("");
}
current.append(" ");
/*Owner Name*/
current.append("");
if (!owner.isEmpty()) {
String[] reqs = owner.replace("Owner:", "").split(",");
for (int i = 0; i < reqs.length; i++) {
current.append(reqs[i]);
}
} else {
current.append("");
}
current.append(" ");
String testClassName = method.getMethodName();
String toolTilps = "";
/*Test case name*/
current.append("");
current.append(testClassName);
current.append(" ");
current.append("");
if (!bugids.isEmpty()) {
String[] bugs = bugids.replace("Bug:", "").split(",");
for (int i = 0; i < bugs.length; i++) {
current.append("" + bugs[i] + "");
}
} else {
current.append("");
}
current.append(" ");
current.append("");
if (skippedTestCases.contains(method)) {
current.append("");
current.append("Skipped");
} else if (!passedTestCases.contains(method)) {
current.append("");
current.append("Failed");
} else {
current.append("");
current.append("Passed");
}
current.append(" ");
current.append(" ");
}
}
int indexSubStr = 0;
int subStringLeng = SLNUM.length();
while (true) {
indexSubStr = sBuilder[0].indexOf(SLNUM);
if (indexSubStr == -1)
break;
sBuilder[0].replace(indexSubStr, indexSubStr + subStringLeng, "" + (count++));
}
indexSubStr = 0;
while (true) {
indexSubStr = sBuilder[1].indexOf(SLNUM);
if (indexSubStr == -1)
break;
sBuilder[1].replace(indexSubStr, indexSubStr + subStringLeng, "" + (count++));
}
indexSubStr = 0;
while (true) {
indexSubStr = sBuilder[2].indexOf(SLNUM);
if (indexSubStr == -1)
break;
sBuilder[2].replace(indexSubStr, indexSubStr + subStringLeng, "" + (count++));
}
f_out.print(sBuilder[0].toString());
f_out.print(sBuilder[1].toString());
f_out.print(sBuilder[2].toString());
f_out.print("
");
}
f_out.println("
");
}
f_out.flush();
}
private PrintWriter createWriter(String outdir) throws IOException {
new File(outdir).mkdirs();
return new PrintWriter(
new BufferedWriter(new FileWriter(new File(outputDir, "emailable-test-run-report.html"))));
}
/**
* Starts HTML Stream
*/
private void startHtmlPage(PrintWriter out) {
out.println("");
out.println("");
out.println("");
out.println("Automation Test Results Summary ");
out.println("");
out.println(""
+ ""
+ "Automation Report
");
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm a z");
TimeZone obj = TimeZone.getTimeZone("IST");
formatter.setTimeZone(obj);
out.flush();
}
/**
* Finishes HTML Stream
*/
private void endHtmlPage(PrintWriter out) {
out.println("");
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy