com.auto.common.testng.reporter.TestNGDataProviderEmailReporter 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.ITestResult;
import org.testng.TestListenerAdapter;
import org.testng.xml.XmlSuite;
import com.auto.data.model.Employee;
public class TestNGDataProviderEmailReporter extends TestListenerAdapter implements IReporter {
private static Logger logger = LoggerFactory.getLogger(TestNGDataProviderEmailReporter.class);
private static PrintWriter f_out;
private static String outputDir;
private static StringBuilder[] sBuilder = new StringBuilder[3];
private static StringBuilder currentValue = null;
NumberFormat nf = NumberFormat.getInstance();
private String background_color = "background-color: #004040; padding:0 15px 0 15px; border: 0px solid grey; height: 25px; color: white; font-family: Georgia;";
/**
* @param result This method will be called on every Test skipped
*/
@Override
public void onTestSuccess(ITestResult result) {
if (sBuilder[0] == null) {
sBuilder[0] = new StringBuilder();
}
if (sBuilder[1] == null) {
sBuilder[1] = new StringBuilder();
}
if (sBuilder[2] == null) {
sBuilder[2] = new StringBuilder();
}
sBuilder[2] = sBuilder[2].append(getSummaryReport(result, "passed"));
}
/**
* @param result This method will be called on every Test skipped
*/
@Override
public void onTestSkipped(ITestResult result) {
if (sBuilder[0] == null) {
sBuilder[0] = new StringBuilder();
}
if (sBuilder[1] == null) {
sBuilder[1] = new StringBuilder();
}
if (sBuilder[2] == null) {
sBuilder[2] = new StringBuilder();
}
sBuilder[1] = sBuilder[1].append(getSummaryReport(result, "skipped"));
//System.out.println(result.getName());
//PricingTestData pricingTestData = (PricingTestData) result.getParameters()[0];
//System.out.println("Pricing Test data :: "+pricingTestData);
}
/**
* @param result This method will be called on every Test failure
*/
@Override
public void onTestFailure(ITestResult result) {
if (sBuilder[0] == null) {
sBuilder[0] = new StringBuilder();
}
if (sBuilder[1] == null) {
sBuilder[1] = new StringBuilder();
}
if (sBuilder[2] == null) {
sBuilder[2] = new StringBuilder();
}
sBuilder[0] = sBuilder[0].append(getSummaryReport(result, "failed"));
//System.out.println(result.getName());
//PricingTestData pricingTestData = (PricingTestData) result.getParameters()[0];
//System.out.println("Pricing Test data :: "+pricingTestData);
}
public void generateReport(List arg0, List suites, String outdir) {
System.out.println("****** generateReport");
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 + " ";
}
for (ISuite suite : suites) {
suiteName = suite.getName();
boolean isHeaderPrinted = false;
Map tests = suite.getResults();
for (ISuiteResult r : tests.values()) {
overview = r.getTestContext();
}
}
Collection passedTest = overview.getPassedTests().getAllMethods();
Collection failedTest = overview.getFailedTests().getAllMethods();
Collection skippedTest = overview.getSkippedTests().getAllMethods();
f_out.println(""
+ "Execution Summary "
+ "Test Suite Name "
+ "# Unique TestCases "
+ "# Passed "
+ "# Failed "
+ "# Skipped "
+ "# Failed + Skipped "
+ "Success Rate "
+ "# Failure Rate "
+ ""
+ suiteName
+ " "
+ (passedTest.size() + failedTest.size() + skippedTest.size())
+ " "
+ suite_pass
+ suite_fail
+ ""
+ skippedTest.size()
+ " "
+ ""
+ (failedTest.size() + skippedTest.size())
+ " "
+ nf.format(((float) passedTest.size() / (float) ((passedTest.size() + failedTest.size() + skippedTest.size()))) * 100) + " %" + " "
+ ""
+ nf.format(((float) (failedTest.size() + skippedTest.size()) / (float) ((passedTest.size() + failedTest.size() + skippedTest.size()))) * 100) + " %" + "
");
f_out.flush();
f_out.println("
");
String failedTestCondition = "true";
if (failedTestCondition.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";
}
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 "
+ "ID "
+ "Component "
+ "Data "
+ "Status "
+ " ");
isHeaderPrinted = true;
}
}
int indexSubStr;
int subStringLeng = SLNUM.length();
while (true) {
indexSubStr = sBuilder[0].indexOf(SLNUM);
if (indexSubStr == -1)
break;
sBuilder[0].replace(indexSubStr, indexSubStr + subStringLeng, "" + (count++));
}
while (true) {
indexSubStr = sBuilder[1].indexOf(SLNUM);
if (indexSubStr == -1)
break;
sBuilder[1].replace(indexSubStr, indexSubStr + subStringLeng, "" + (count++));
}
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 StringBuilder getSummaryReport(final ITestResult result, final String status) {
Employee employeeTestData = (Employee) result.getParameters()[0];
StringBuilder current = new StringBuilder();
String testlinkURL = "";
current.append("");
current.append("");
current.append("");
current.append(" ");
String methodStr = result.getName();
/*Testcase ID*/
current.append("");
if (!methodStr.isEmpty()) {
String[] split = methodStr.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("");
current.append("Component");
current.append(" ");
String data = employeeTestData.toString();
if (data == null) {
data = "";
}
current.append("");
current.append(data + " ");
current.append("");
if (status.contains("passed")) {
current.append("");
current.append(status + "");
} else if (status.contains("failed")) {
current.append("");
current.append(status + "");
} else {
current.append("");
current.append(status + "");
}
current.append(" ");
current.append(" ");
return current;
}
private PrintWriter createWriter(String outdir) throws IOException {
new File(outdir).mkdirs();
return new PrintWriter(
new BufferedWriter(new FileWriter(new File(outputDir, "testng-email.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