Best Testng code snippet using org.testng.reporters.EmailableReporter2.writeBody
Source:CustomEmailableReporter.java  
...47            suiteResults.add(new SuiteResult(suite));48        }49        writeDocumentStart();50        writeHead();51        writeBody();52        writeDocumentEnd();53        writer.close();54    }55    @Override56	protected PrintWriter createWriter(String outdir) throws IOException {57        new File(outdir).mkdirs();58        return new PrintWriter(new BufferedWriter(new FileWriter(new File(59                outdir, "emailable-report.html"))));60    }61    @Override62	protected void writeDocumentStart() {63        writer.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">");64        writer.print("<html xmlns=\"http://www.w3.org/1999/xhtml\">");65    }66    @Override67	protected void writeHead() {68        writer.print("<head>");69        writer.print("<title>TestNG Report for Rest Automation</title>");70        writeStylesheet();71        writer.print("</head>");72    }73    @Override74	protected void writeStylesheet() {75//    	File cssFile = new File("./conf/report.css");76//    	String csstext = null;77//    	if(cssFile.exists()){78//			try{79//				csstext = FileUtils.readFileToString(cssFile);80//			}catch (IOException e) {81//				csstext = null;82//			}83//    	}84//    	if(csstext == null ){85	        writer.print("<style type=\"text/css\">");86	        writer.print("table {width:70%;horizontal-align:center;margin-bottom:10px;padding-bottom: 50px;border-collapse:collapse;empty-cells:show}");87	        writer.print("th,td {border:2px solid #009;padding:.25em .5em}");88	        writer.print("th {vertical-align:bottom}");89	        writer.print("td {font-family: verdana, Times, serif;vertical-align:top}");90	        writer.print("table a {font-weight:bold}");91	        writer.print(".stripe td {background-color: #E6EBF9}");92	        writer.print(".num {text-align:right}");93	        writer.print(".passedodd td {background-color: #5DDA70}");94	        writer.print(".passedeven td {background-color: #5DDA70}");95	        writer.print(".skippedodd td {background-color: #E2DEDD}");96	        writer.print(".skippedeven td {background-color: #E2DEDD}");97	        writer.print(".failedodd td,.attn {background-color: #FA8A6E}");98	        writer.print(".failedeven td,.stripe .attn {background-color: #FA8A6E}");99	        writer.print(".stacktrace {white-space:pre;font-family:monospace}");100	        writer.print(".totop {font-size:85%;text-align:center;border-bottom:2px solid #000}");101	        writer.print("</style>");102//    	}else{103//    		writer.print(csstext);104//    	}105    }106    @Override107	protected void writeBody() {108        writer.print("<body>");109        writeSuiteSummary();110        writeScenarioSummary();111        writeScenarioDetails();112        writer.print("</body>");113    }114    @Override115	protected void writeDocumentEnd() {116        writer.print("</html>");117    }118    @Override119	protected void writeSuiteSummary() {120        NumberFormat integerFormat = NumberFormat.getIntegerInstance();121        NumberFormat decimalFormat = NumberFormat.getNumberInstance();...Source:CustomReporter.java  
...49            suiteResults.add( new SuiteResult( suite ) );50        }51        writeDocumentStart();52        writeHead();53        writeBody();54        writeDocumentEnd();55        writer.close();56    }57    protected PrintWriter createWriter(String outdir) throws IOException {58        new File(outdir).mkdirs();59        return new PrintWriter( new BufferedWriter( new FileWriter( new File( outdir, outFilename ) ) ) );60    }61    protected void writeDocumentStart() {62        writer.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">");63        writer.print("<html xmlns=\"http://www.w3.org/1999/xhtml\">");64    }65    protected void writeHead() {66        writer.print("<head>");67        writer.print("<title>TestNG Report</title>");68        writeStylesheet();69        writer.print("</head>");70    }71    protected void writeStylesheet() {72        writer.print("<style type=\"text/css\">");73        writer.print("table {margin-bottom:10px;border-collapse:collapse;empty-cells:show}");74        writer.print("th,td {border:1px solid #009;padding:.25em .5em}");75        writer.print("th {vertical-align:bottom}");76        writer.print("td {vertical-align:top}");77        writer.print("table a {font-weight:bold}");78        writer.print(".stripe td {background-color: #E6EBF9}");79        writer.print(".num {text-align:right}");80        writer.print(".passedodd td {background-color: #3F3}");81        writer.print(".passedeven td {background-color: #0A0}");82        writer.print(".skippedodd td {background-color: #DDD}");83        writer.print(".skippedeven td {background-color: #CCC}");84        writer.print(".failedodd td,.attn {background-color: #F33}");85        writer.print(".failedeven td,.stripe .attn {background-color: #D00}");86        writer.print(".stacktrace {white-space:pre;font-family:monospace}");87        writer.print(".totop {font-size:85%;text-align:center;border-bottom:2px solid #000}");88        writer.print("</style>");89    }90    protected void writeBody() {91        writer.print("<body>");92        writeReportTitle( "TestNG Report" );93        writeSuiteSummary();94        writeScenarioSummary();95        writeScenarioDetails();96        writer.print("</body>");97    }98    protected void writeReportTitle( String title ) {99        writer.print( "<center><h1>" + title + " - " + getDateAsString() + "</h1></center>" );100    }101    protected void writeDocumentEnd() {102        writer.print("</html>");103    }104    protected void writeSuiteSummary() {...Source:EmailableReporter2.java  
...51            suiteResults.add(new SuiteResult(suite));52        }53        writeDocumentStart();54        writeHead();55        writeBody();56        writeDocumentEnd();57        writer.close();58    }59    protected PrintWriter createWriter(String outdir) throws IOException {60        new File(outdir).mkdirs();61        String jvmArg = System.getProperty(JVM_ARG);62        if (jvmArg != null && !jvmArg.trim().isEmpty()) {63            fileName = jvmArg;64        }65        return new PrintWriter(newBufferedWriter(new File(outdir, fileName).toPath(), UTF_8));66    }67    protected void writeDocumentStart() {68        writer.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">");69        writer.println("<html xmlns=\"http://www.w3.org/1999/xhtml\">");70    }71    protected void writeHead() {72        writer.println("<head>");73        writer.println("<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\"/>");74        writer.println("<title>TestNG Report</title>");75        writeStylesheet();76        writer.println("</head>");77    }78    protected void writeStylesheet() {79        writer.print("<style type=\"text/css\">");80        writer.print("table {margin-bottom:10px;border-collapse:collapse;empty-cells:show}");81        writer.print("th,td {border:1px solid #009;padding:.25em .5em}");82        writer.print("th {vertical-align:bottom}");83        writer.print("td {vertical-align:top}");84        writer.print("table a {font-weight:bold}");85        writer.print(".stripe td {background-color: #E6EBF9}");86        writer.print(".num {text-align:right}");87        writer.print(".passedodd td {background-color: #3F3}");88        writer.print(".passedeven td {background-color: #0A0}");89        writer.print(".skippedodd td {background-color: #DDD}");90        writer.print(".skippedeven td {background-color: #CCC}");91        writer.print(".failedodd td,.attn {background-color: #F33}");92        writer.print(".failedeven td,.stripe .attn {background-color: #D00}");93        writer.print(".stacktrace {white-space:pre;font-family:monospace}");94        writer.print(".totop {font-size:85%;text-align:center;border-bottom:2px solid #000}");95        writer.print(".invisible {display:none}");96        writer.println("</style>");97    }98    protected void writeBody() {99        writer.println("<body>");100        writeSuiteSummary();101        writeScenarioSummary();102        writeScenarioDetails();103        writer.println("</body>");104    }105    protected void writeDocumentEnd() {106        writer.println("</html>");107    }108    protected void writeSuiteSummary() {109        NumberFormat integerFormat = NumberFormat.getIntegerInstance();110        NumberFormat decimalFormat = NumberFormat.getNumberInstance();111        int totalPassedTests = 0;112        int totalSkippedTests = 0;...Source:CustomSummaryReport.java  
...64		}6566		writeDocumentStart();67		writeHead();68		writeBody();69		writeDocumentEnd();7071		writer.close();72	}7374	protected PrintWriter createWriter(String outdir) throws IOException {75		new File(outdir).mkdirs();76		return new PrintWriter( new BufferedWriter( new FileWriter( new File( outdir, outFilename ) ) ) );77	}7879	protected void writeDocumentStart() {80		writer.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">");81		writer.print("<html xmlns=\"http://www.w3.org/1999/xhtml\">");82	}8384	protected void writeHead() {85		writer.print("<head>");86		writer.print("<title>TestNG Report</title>");87		writeStylesheet();88		writer.print("</head>");89	}9091	protected void writeStylesheet() {92		writer.print("<style type=\"text/css\">");93		writer.print("table {margin-bottom:10px;border-collapse:collapse;empty-cells:show}");94		writer.print("th,td {border:1px solid #009;padding:.25em .5em}");95		writer.print("th {vertical-align:bottom}");96		writer.print("td {vertical-align:top}");97		writer.print("table a {font-weight:bold}");98		writer.print(".stripe td {background-color: #E6EBF9}");99		writer.print(".num {text-align:right}");100		writer.print(".passedodd td {background-color: #3F3}");101		writer.print(".passedeven td {background-color: #0A0}");102		writer.print(".skippedodd td {background-color: #DDD}");103		writer.print(".skippedeven td {background-color: #CCC}");104		writer.print(".failedodd td,.attn {background-color: #F33}");105		writer.print(".failedeven td,.stripe .attn {background-color: #D00}");106		writer.print(".stacktrace {white-space:pre;font-family:monospace}");107		writer.print(".totop {font-size:85%;text-align:center;border-bottom:2px solid #000}");108		writer.print("</style>");109	}110111	protected void writeBody() {112		writer.print("<body>");113		writeReportTitle( "TestNG Report" );114		writer.print("<table><h1>Suite Summary</h1></table>");115		writeSuiteSummary();116		writer.print("<table><h1>User Summary</h1></table>");117		writeUserSummary();118		writer.print("</body>");119	}120121	protected void writeReportTitle( String title ) {122		writer.print( "<center><h1>" + title + " - " + getDateAsString() + "</h1></center>" );123	}124125	protected void writeDocumentEnd() {
...Source:ReportOverview.java  
...77            suiteResults.add(new SuiteResult(suite));78        }79        writeDocumentStart();80        writeHead();81        writeBody();82        writeDocumentEnd();83        writer.close();84    }85    /**86     * Generates the output file87     *88     * @param outdir - the output directory to save the output file to89     * @return PrintWriter - the writer for our custom output file90     * @throws IOException - if unable to create the file, IOException will be thrown91     */92    @Override93    protected PrintWriter createWriter(String outdir) throws IOException {94        new File(outdir).mkdirs();95        return new PrintWriter(newBufferedWriter(new File(outdir, FILE_NAME).toPath(), UTF_8));96    }97    /**98     * Creates the header information for the test report99     */100    @Override101    protected void writeHead() {102        writer.println("    <head>");103        writer.println("        <meta name='viewport' content='width=device-width, initial-scale=1'>");104        writer.println("        <title>Selenified Test Report</title>");105        writeStylesheet();106        writer.println("    </head>");107    }108    /**109     * Sets the css and js for the test report110     */111    @Override112    protected void writeStylesheet() {113        writer.println("        <link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css'>\n");114        writer.println("        <script src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js'></script>\n");115        writer.println("        <script src='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js'></script>\n");116        writer.println("        <link rel='stylesheet' href='https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css'>\n");117        writer.println("        <script src='https:////cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js'></script>\n");118        writer.println("        <script>\n");119        writer.println("            $(document).ready( function () {\n");120        writer.println("                $('.sort').DataTable();\n");121        writer.println("            } );\n");122        writer.println("        </script>\n");123    }124    /**125     * Create the title and content of the test report126     */127    @Override128    protected void writeBody() {129        writer.println("    <body>");130        writer.println("        <h1 style='text-align:center;'>Selenified Test Results</h1>");131        writeSuiteSummary();132        writeScenarioDetails();133        writer.println("    </body>");134    }135    /**136     * Generates the high level summary of the test report137     */138    @Override139    protected void writeSuiteSummary() {140        int totalPassedTests = 0;141        int totalSkippedTests = 0;142        int totalFailedTests = 0;...writeBody
Using AI Code Generation
1package com.automation.test;2import java.io.File;3import java.io.IOException;4import java.util.ArrayList;5import java.util.List;6import org.apache.commons.io.FileUtils;7import org.testng.TestNG;8import org.testng.annotations.Test;9import org.testng.reporters.EmailableReporter2;10public class TestNGEmailableReport {11public void test() throws IOException {12    TestNG testng = new TestNG();13    List<String> suites = new ArrayList<String>();14    suites.add("C:\\Users\\sathish.kumar15\\eclipse-workspace\\TestNGProject\\testng.xml");15    testng.setTestSuites(suites);16    testng.run();17    EmailableReporter2 emailableReporter2 = new EmailableReporter2();18    emailableReporter2.generateReport(new ArrayList<>(), "C:\\Users\\sathish.kumar15\\eclipse-workspace\\TestNGProject\\test-output\\emailable-report.html", "C:\\Users\\sathish.kumar15\\eclipse-workspace\\TestNGProject\\test-output\\emailable-report.html", true);19    File source = new File("C:\\Users\\sathish.kumar15\\eclipse-workspace\\TestNGProject\\test-output\\emailable-report.html");20    File destination = new File("C:\\Users\\sathish.kumar15\\eclipse-workspace\\TestNGProject\\test-output\\emailable-report2.html");21    FileUtils.copyFile(source, destination);22}23}24package com.automation.test;25import java.io.File;26import java.io.IOException;27import java.util.ArrayList;28import java.util.List;29import org.apache.commons.io.FileUtils;30import org.testng.TestNG;31import org.testng.annotations.Test;32import org.testng.reporters.EmailableReporter2;33public class TestNGEmailableReport {34public void test() throws IOException {35    TestNG testng = new TestNG();36    List<String> suites = new ArrayList<String>();37    suites.add("C:\\Users\\sathish.kumar15\\eclipse-workspace\\TestNGProject\\testng.xml");38    testng.setTestSuites(suites);39    testng.run();40    EmailableReporter2 emailableReporter2 = new EmailableReporter2();41    emailableReporter2.generateReport(new ArrayList<>(), "C:\\Users\\sathishwriteBody
Using AI Code Generation
1public class EmailableReporter2 {2    public static void main(String[] args) {3        String report = "path/to/testng-failed.xml";4        String emailableReport = "path/to/emailable-report.html";5        try {6            List<ISuite> suites = new TestNG().setXmlSuites(Lists.newArrayList(SuiteXmlParser.parse(report)));7            new EmailableReporter2().generateReport(suites, null, null, emailableReport);8        } catch (Exception e) {9            e.printStackTrace();10        }11    }12}13public class EmailableReporter2 {14    public static void main(String[] args) {15        String report1 = "path/to/testng-failed1.xml";16        String report2 = "path/to/testng-failed2.xml";17        String emailableReport = "path/to/emailable-report.html";18        try {19            List<ISuite> suites = new TestNG().setXmlSuites(Lists.newArrayList(SuiteXmlParser.parse(report1), SuiteXmlParser.parse(report2)));20            new EmailableReporter2().generateReport(suites, null, null, emailableReport);21        } catch (Exception e) {22            e.printStackTrace();23        }24    }25}26public class EmailableReporter2 {27    public static void main(String[] args) {28        String reportDir = "path/to/testng-xml-files";29        String emailableReport = "path/to/emailable-report.html";30        try {31            List<ISuite> suites = new TestNG().setXmlSuites(Lists.newArrayList(SuiteXmlParser.parse(reportDir)));32            new EmailableReporter2().generateTestNG is a Java-based open-source framework for test automation that includes various test types, such as unit testing, functional testing, E2E testing, etc. TestNG is in many ways similar to JUnit and NUnit. But in contrast to its competitors, its extensive features make it a lot more reliable framework. One of the major reasons for its popularity is its ability to structure tests and improve the scripts' readability and maintainability. Another reason can be the important characteristics like the convenience of using multiple annotations, reliance, and priority that make this framework popular among developers and testers for test design. You can refer to the TestNG tutorial to learn why you should choose the TestNG framework.
You can push your abilities to do automated testing using TestNG and advance your career by earning a TestNG certification. Check out our TestNG certification.
Watch this complete tutorial to learn how you can leverage the capabilities of the TestNG framework for Selenium automation testing.
Get 100 minutes of automation test minutes FREE!!
