|
1 | 1 | package com.baeldung.reports; |
2 | 2 |
|
3 | | -import org.testng.IReporter; |
4 | | -import org.testng.IResultMap; |
5 | | -import org.testng.ISuite; |
6 | | -import org.testng.ISuiteResult; |
7 | | -import org.testng.ITestContext; |
8 | | -import org.testng.ITestResult; |
| 3 | +import org.slf4j.Logger; |
| 4 | +import org.slf4j.LoggerFactory; |
| 5 | +import org.testng.*; |
9 | 6 | import org.testng.xml.XmlSuite; |
10 | 7 |
|
11 | | -import java.io.BufferedWriter; |
12 | | -import java.io.File; |
13 | | -import java.io.FileWriter; |
14 | | -import java.io.IOException; |
15 | | -import java.io.PrintWriter; |
| 8 | +import java.io.*; |
| 9 | +import java.nio.file.Files; |
| 10 | +import java.nio.file.Paths; |
16 | 11 | import java.util.List; |
17 | 12 | import java.util.Map; |
18 | | -import java.util.Set; |
| 13 | +import java.util.stream.Stream; |
19 | 14 |
|
20 | 15 | public class CustomisedReports implements IReporter { |
21 | | - private PrintWriter reportWriter; |
| 16 | + private static final Logger LOGGER = LoggerFactory.getLogger(CustomisedReports.class); |
22 | 17 |
|
23 | 18 | public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) { |
24 | | - new File(outputDirectory).mkdirs(); |
25 | | - try { |
26 | | - reportWriter = new PrintWriter(new BufferedWriter(new FileWriter(new File(outputDirectory, "my-report.html")))); |
27 | | - } catch (IOException e) { |
28 | | - e.printStackTrace(); |
29 | | - } |
| 19 | + String reportTemplate = initReportTemplate(); |
30 | 20 | String resultRow = "<tr class=\"%s\"><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>"; |
31 | | - initReportTemplate(); |
32 | | - for (ISuite suite : suites) { |
| 21 | + StringBuilder rows = new StringBuilder(); |
| 22 | + suites.forEach(suite -> { |
33 | 23 | Map<String, ISuiteResult> suiteResults = suite.getResults(); |
34 | 24 | suiteResults.forEach((testName, suiteResult) -> { |
| 25 | + |
35 | 26 | ITestContext testContext = suiteResult.getTestContext(); |
36 | 27 |
|
37 | | - IResultMap failedResult = testContext.getFailedTests(); |
38 | | - Set<ITestResult> testsFailed = failedResult.getAllResults(); |
39 | | - for (ITestResult testResult : testsFailed) { |
40 | | - reportWriter.println(String.format(resultRow, "danger", suite.getName(), testName, testResult.getName(), "FAILED", "NA")); |
41 | | - } |
| 28 | + Stream<ITestResult> failedTests = testContext.getFailedTests().getAllResults().stream(); |
| 29 | + Stream<ITestResult> passedTests = testContext.getPassedTests().getAllResults().stream(); |
| 30 | + Stream<ITestResult> skippedTests = testContext.getSkippedTests().getAllResults().stream(); |
| 31 | + |
| 32 | + String suiteName = suite.getName(); |
| 33 | + |
| 34 | + Stream<ITestResult> allTestResults = Stream.concat(Stream.concat(failedTests, passedTests), skippedTests); |
| 35 | + generateReportRows(resultRow, rows, testName, suiteName, allTestResults); |
| 36 | + }); |
| 37 | + }); |
| 38 | + reportTemplate = reportTemplate.replaceFirst("</tbody>", rows.toString() + "</tbody>"); |
| 39 | + saveReportTemplate(outputDirectory, reportTemplate); |
| 40 | + } |
42 | 41 |
|
43 | | - IResultMap passResult = testContext.getPassedTests(); |
44 | | - Set<ITestResult> testsPassed = passResult.getAllResults(); |
45 | | - for (ITestResult testResult : testsPassed) { |
46 | | - reportWriter.println(String.format(resultRow, "success", suite.getName(), testName, testResult.getName(), "PASSED", String.valueOf(testResult.getEndMillis() - testResult.getStartMillis()))); |
| 42 | + private void generateReportRows(String resultRow, StringBuilder rows, String testName, String suiteName, Stream<ITestResult> allTestResults) { |
| 43 | + allTestResults |
| 44 | + .forEach(testResult -> { |
| 45 | + String testReportRow = ""; |
| 46 | + if (testResult.getStatus() == ITestResult.FAILURE) { |
| 47 | + testReportRow = String.format(resultRow, "danger", suiteName, testName, testResult.getName(), "FAILED", "NA"); |
47 | 48 | } |
| 49 | + if (testResult.getStatus() == ITestResult.SUCCESS) { |
| 50 | + testReportRow = String.format(resultRow, "success", suiteName, testName, testResult.getName(), "PASSED", String.valueOf(testResult.getEndMillis() - testResult.getStartMillis())); |
48 | 51 |
|
49 | | - IResultMap skippedResult = testContext.getSkippedTests(); |
50 | | - Set<ITestResult> testsSkipped = skippedResult.getAllResults(); |
51 | | - for (ITestResult testResult : testsSkipped) { |
52 | | - reportWriter.println(String.format(resultRow, "warning", suite.getName(), testName, testResult.getName(), "SKIPPED", "NA")); |
53 | 52 | } |
| 53 | + if (testResult.getStatus() == ITestResult.SKIP) { |
| 54 | + testReportRow = String.format(resultRow, "warning", suiteName, testName, testResult.getName(), "SKIPPED", "NA"); |
| 55 | + } |
| 56 | + rows.append(testReportRow); |
54 | 57 | }); |
55 | | - } |
56 | | - finishReportTemplate(); |
57 | | - reportWriter.flush(); |
58 | | - reportWriter.close(); |
59 | 58 | } |
60 | 59 |
|
61 | | - private void initReportTemplate() { |
62 | | - reportWriter.println( |
63 | | - "<html>" + "<head>" + "<title>My Custom Report</title>" + "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">" + "<link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css\">" |
64 | | - + "<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js\"></script>" + "<script src=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js\"></script></head>" + "<body><div class=\"container\">"); |
65 | | - reportWriter.println("<table class=\"table\"><thead><tr>" + "<th>Suite</th>" + "<th>Test</th>" + "<th>Method</th>" + "<th>Status</th>" + "<th>Execution Time(ms)</th>" + "</tr></thead> <tbody>"); |
| 60 | + private String initReportTemplate() { |
| 61 | + String template = null; |
| 62 | + byte[] reportTemplate = null; |
| 63 | + try { |
| 64 | + reportTemplate = Files.readAllBytes(Paths.get("src/test/resources/reportTemplate.html")); |
| 65 | + template = new String(reportTemplate, "UTF-8"); |
| 66 | + } catch (IOException e) { |
| 67 | + LOGGER.error("Problem initializing template", e); |
| 68 | + } |
| 69 | + return template; |
66 | 70 | } |
67 | 71 |
|
68 | | - private void finishReportTemplate() { |
69 | | - reportWriter.println(" </tbody></div></body></html>"); |
| 72 | + private void saveReportTemplate(String outputDirectory, String reportTemplate) { |
| 73 | + new File(outputDirectory).mkdirs(); |
| 74 | + try { |
| 75 | + PrintWriter reportWriter = new PrintWriter(new BufferedWriter(new FileWriter(new File(outputDirectory, "my-report.html")))); |
| 76 | + reportWriter.println(reportTemplate); |
| 77 | + reportWriter.flush(); |
| 78 | + reportWriter.close(); |
| 79 | + } catch (IOException e) { |
| 80 | + LOGGER.error("Problem saving template", e); |
| 81 | + } |
70 | 82 | } |
71 | 83 | } |
0 commit comments