-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnformatedReport.java
More file actions
119 lines (110 loc) · 4.53 KB
/
UnformatedReport.java
File metadata and controls
119 lines (110 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package com.owler.report;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.testng.IReporter;
import org.testng.ISuite;
import org.testng.ITestNGMethod;
import org.testng.ITestResult;
import org.testng.xml.XmlSuite;
/*
* Author : Daniel George Valsarajan
*
* Description :
* Report class will create a report file of all test plan and,
* will also include the information logged during the execution of the test
* to understand and monitor the flow of the test.
*
* HTML file created by this class has doesn't have javascript or any styles.
*/
public class SlackReport implements IReporter {
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
for (ISuite suite : suites) {
String suiteName = suite.getName() + "-SlackReport.html";
String testName = suite.getName();
try {
FileWriter writer = createFile(outputDirectory, suiteName);
startHtml(writer, testName);
overallReport(writer, suite);
casewiseReport(writer, suite);
endHtml(writer);
} catch (IOException E) {
System.out.println("Report is not generator. " + E);
}
}
}
FileWriter createFile(String outputDirectory, String suiteName) throws IOException {
return new FileWriter(new File(outputDirectory, suiteName));
}
void startHtml(FileWriter writer, String testName) throws IOException {
writer.write("<!DOCTYPE html><html><head>");
writer.write("<title>" + testName + "</title>");
writer.write("</head>");
writer.write("<h2 style=text-align:center>" + testName + " Automation Report</h2>");
writer.write("<hr><body>");
}
void overallReport(FileWriter writer, ISuite suite) throws IOException {
writer.write(
"<table cellpadding=5, cellspacing=5, class='param'><tr><th class='numi'>Test Name</th><th class='numi'>Passed</th><th class='numi'>Failed</th><th class='numi'>Skipped</th><th class='numi'>Test Duration</th></tr>");
for (String temp : suite.getResults().keySet()) {
writer.write("<tr><td class='numi'>" + suite.getResults().get(temp).getTestContext().getName()
+ "</strong></td><td class='passed'><b>"
+ suite.getResults().get(temp).getTestContext().getPassedTests().size() + "</td>");
writer.write("<td class='failed'>" + suite.getResults().get(temp).getTestContext().getFailedTests().size()
+ "</td>");
writer.write("<td class='skipped'>" + suite.getResults().get(temp).getTestContext().getSkippedTests().size()
+ "</td>");
long diffTime = suite.getResults().get(temp).getTestContext().getEndDate().getTime()
- suite.getResults().get(temp).getTestContext().getStartDate().getTime();
long time = TimeUnit.MILLISECONDS.toSeconds(diffTime);
if (time >= 120) {
time = TimeUnit.MILLISECONDS.toMinutes(diffTime);
writer.write("<td class='numi'>" + time + " minutes</td>");
} else {
writer.write("<td class='numi'>" + time + " seconds</td>");
}
writer.write("</tr></table>");
}
}
void casewiseReport(FileWriter writer, ISuite suite) throws IOException {
Map<String, ITestResult> results = new HashMap<String, ITestResult>();
Set<String> testNames = suite.getResults().keySet();
for (String testName : testNames) {
for (ITestResult iResult : suite.getResults().get(testName).getTestContext().getFailedTests()
.getAllResults()) {
results.put(iResult.getName(), iResult);
}
for (ITestResult iResult : suite.getResults().get(testName).getTestContext().getPassedTests()
.getAllResults()) {
results.put(iResult.getName(), iResult);
}
for (ITestResult iResult : suite.getResults().get(testName).getTestContext().getSkippedTests()
.getAllResults()) {
results.put(iResult.getName(), iResult);
}
}
writer.write(
"<table cellpadding=5, cellspacing=5, class='param'><tr><th class='numi'>Test Method</th><th class='numi'>Description</th><th class='numi'>Result</th></tr>");
for (ITestNGMethod method : suite.getAllMethods()) {
writer.write("<tr><td class='content'>" + method.getMethodName() + "</td><td class='description'>"
+ method.getDescription() + "</td>");
if ((results.get(method.getMethodName()).getStatus()) == 1) {
writer.write("<td class='passed'>Pass</td>");
} else {
writer.write("<td class='failed'>Fail</td></tr>");
}
}
writer.write("</table>");
}
void endHtml(FileWriter writer) throws IOException {
writer.write("<hr>");
writer.write("</body></html>");
writer.flush();
writer.close();
}
}