Best Testng code snippet using org.testng.reporters.TestHTMLReporter.generateTable
Source:TestHTMLReporter.java
...45 /////46 private static String getOutputFile(ITestContext context) {47 return context.getName() + ".html";48 }49 public static void generateTable(StringBuffer sb, String title,50 Collection<ITestResult> tests, String cssClass, Comparator<ITestResult> comparator)51 {52 sb.append("<table width='100%' border='1' class='invocation-").append(cssClass).append("'>\n")53 .append("<tr><td colspan='4' align='center'><b>").append(title).append("</b></td></tr>\n")54 .append("<tr>")55 .append("<td><b>Test method</b></td>\n")56 .append("<td width=\"30%\"><b>Exception</b></td>\n")57 .append("<td width=\"10%\"><b>Time (seconds)</b></td>\n")58 .append("<td><b>Instance</b></td>\n")59 .append("</tr>\n");60 if (tests instanceof List) {61 Collections.sort((List<ITestResult>) tests, comparator);62 }63 // User output?64 String id = "";65 Throwable tw = null;66 for (ITestResult tr : tests) {67 sb.append("<tr>\n");68 // Test method69 ITestNGMethod method = tr.getMethod();70 String name = method.getMethodName();71 sb.append("<td title='").append(tr.getTestClass().getName()).append(".")72 .append(name)73 .append("()'>")74 .append("<b>").append(name).append("</b>");75 // Test class76 String testClass = tr.getTestClass().getName();77 if (testClass != null) {78 sb.append("<br>").append("Test class: " + testClass);79 // Test name80 String testName = tr.getTestName();81 if (testName != null) {82 sb.append(" (").append(testName).append(")");83 }84 }85 // Method description86 if (! Utils.isStringEmpty(method.getDescription())) {87 sb.append("<br>").append("Test method: ").append(method.getDescription());88 }89 Object[] parameters = tr.getParameters();90 if (parameters != null && parameters.length > 0) {91 sb.append("<br>Parameters: ");92 for (int j = 0; j < parameters.length; j++) {93 if (j > 0) {94 sb.append(", ");95 }96 sb.append(parameters[j] == null ? "null" : parameters[j].toString());97 }98 }99 //100 // Output from the method, created by the user calling Reporter.log()101 //102 {103 List<String> output = Reporter.getOutput(tr);104 if (null != output && output.size() > 0) {105 sb.append("<br/>");106 // Method name107 String divId = "Output-" + tr.hashCode();108 sb.append("\n<a href=\"#").append(divId).append("\"")109 .append(" onClick='toggleBox(\"").append(divId).append("\", this, \"Show output\", \"Hide output\");'>")110 .append("Show output</a>\n")111 .append("\n<a href=\"#").append(divId).append("\"")112 .append(" onClick=\"toggleAllBoxes();\">Show all outputs</a>\n")113 ;114 // Method output115 sb.append("<div class='log' id=\"").append(divId).append("\">\n");116 for (String s : output) {117 sb.append(s).append("<br/>\n");118 }119 sb.append("</div>\n");120 }121 }122 sb.append("</td>\n");123 // Exception124 tw = tr.getThrowable();125 String stackTrace = "";126 String fullStackTrace = "";127 id = "stack-trace" + tr.hashCode();128 sb.append("<td>");129 if (null != tw) {130 String[] stackTraces = Utils.stackTrace(tw, true);131 fullStackTrace = stackTraces[1];132 stackTrace = "<div><pre>" + stackTraces[0] + "</pre></div>";133 sb.append(stackTrace);134 // JavaScript link135 sb.append("<a href='#' onClick='toggleBox(\"")136 .append(id).append("\", this, \"Click to show all stack frames\", \"Click to hide stack frames\")'>")137 .append("Click to show all stack frames").append("</a>\n")138 .append("<div class='stack-trace' id='" + id + "'>")139 .append("<pre>" + fullStackTrace + "</pre>")140 .append("</div>")141 ;142 }143 sb.append("</td>\n");144 // Time145 long time = (tr.getEndMillis() - tr.getStartMillis()) / 1000;146 String strTime = Long.toString(time);147 sb.append("<td>").append(strTime).append("</td>\n");148 // Instance149 Object instance = tr.getInstance();150 sb.append("<td>").append(instance).append("</td>");151 sb.append("</tr>\n");152 }153 sb.append("</table><p>\n");154 }155 private static String arrayToString(String[] array) {156 StringBuffer result = new StringBuffer("");157 for (String element : array) {158 result.append(element).append(" ");159 }160 return result.toString();161 }162 private static String HEAD =163 "\n<style type=\"text/css\">\n" +164 ".log { display: none;} \n" +165 ".stack-trace { display: none;} \n" +166 "</style>\n" +167 "<script type=\"text/javascript\">\n" +168 "<!--\n" +169 "function flip(e) {\n" +170 " current = e.style.display;\n" +171 " if (current == 'block') {\n" +172 " e.style.display = 'none';\n" +173 " return 0;\n" +174 " }\n" +175 " else {\n" +176 " e.style.display = 'block';\n" +177 " return 1;\n" +178 " }\n" +179 "}\n" +180 "\n" +181 "function toggleBox(szDivId, elem, msg1, msg2)\n" +182 "{\n" +183 " var res = -1;" +184 " if (document.getElementById) {\n" +185 " res = flip(document.getElementById(szDivId));\n" +186 " }\n" +187 " else if (document.all) {\n" +188 " // this is the way old msie versions work\n" +189 " res = flip(document.all[szDivId]);\n" +190 " }\n" +191 " if(elem) {\n" +192 " if(res == 0) elem.innerHTML = msg1; else elem.innerHTML = msg2;\n" +193 " }\n" +194 "\n" +195 "}\n" +196 "\n" +197 "function toggleAllBoxes() {\n" +198 " if (document.getElementsByTagName) {\n" +199 " d = document.getElementsByTagName('div');\n" +200 " for (i = 0; i < d.length; i++) {\n" +201 " if (d[i].className == 'log') {\n" +202 " flip(d[i]);\n" +203 " }\n" +204 " }\n" +205 " }\n" +206 "}\n" +207 "\n" +208 "// -->\n" +209 "</script>\n" +210 "\n";211 public static void generateLog(ITestContext testContext,212 String host,213 String outputDirectory,214 Collection<ITestResult> failedConfs,215 Collection<ITestResult> skippedConfs,216 Collection<ITestResult> passedTests,217 Collection<ITestResult> failedTests,218 Collection<ITestResult> skippedTests,219 Collection<ITestResult> percentageTests)220 {221 StringBuffer sb = new StringBuffer();222 sb.append("<html>\n<head>\n")223 .append("<title>TestNG: ").append(testContext.getName()).append("</title>\n")224 .append(HtmlHelper.getCssString())225 .append(HEAD)226 .append("</head>\n")227 .append("<body>\n");228 Date startDate = testContext.getStartDate();229 Date endDate = testContext.getEndDate();230 long duration = (endDate.getTime() - startDate.getTime()) / 1000;231 int passed =232 testContext.getPassedTests().size() +233 testContext.getFailedButWithinSuccessPercentageTests().size();234 int failed = testContext.getFailedTests().size();235 int skipped = testContext.getSkippedTests().size();236 String hostLine = Utils.isStringEmpty(host) ? "" : "<tr><td>Remote host:</td><td>" + host237 + "</td>\n</tr>";238 sb239 .append("<h2 align='center'>").append(testContext.getName()).append("</h2>")240 .append("<table border='1' align=\"center\">\n")241 .append("<tr>\n")242// .append("<td>Property file:</td><td>").append(m_testRunner.getPropertyFileName()).append("</td>\n")243// .append("</tr><tr>\n")244 .append("<td>Tests passed/Failed/Skipped:</td><td>").append(passed).append("/").append(failed).append("/").append(skipped).append("</td>\n")245 .append("</tr><tr>\n")246 .append("<td>Started on:</td><td>").append(testContext.getStartDate().toString()).append("</td>\n")247 .append("</tr>\n")248 .append(hostLine)249 .append("<tr><td>Total time:</td><td>").append(duration).append(" seconds (").append(endDate.getTime() - startDate.getTime())250 .append(" ms)</td>\n")251 .append("</tr><tr>\n")252 .append("<td>Included groups:</td><td>").append(arrayToString(testContext.getIncludedGroups())).append("</td>\n")253 .append("</tr><tr>\n")254 .append("<td>Excluded groups:</td><td>").append(arrayToString(testContext.getExcludedGroups())).append("</td>\n")255 .append("</tr>\n")256 .append("</table><p/>\n")257 ;258 sb.append("<small><i>(Hover the method name to see the test class name)</i></small><p/>\n");259 if (failedConfs.size() > 0) {260 generateTable(sb, "FAILED CONFIGURATIONS", failedConfs, "failed", CONFIGURATION_COMPARATOR);261 }262 if (skippedConfs.size() > 0) {263 generateTable(sb, "SKIPPED CONFIGURATIONS", skippedConfs, "skipped", CONFIGURATION_COMPARATOR);264 }265 if (failedTests.size() > 0) {266 generateTable(sb, "FAILED TESTS", failedTests, "failed", NAME_COMPARATOR);267 }268 if (percentageTests.size() > 0) {269 generateTable(sb, "FAILED TESTS BUT WITHIN SUCCESS PERCENTAGE",270 percentageTests, "percent", NAME_COMPARATOR);271 }272 if (passedTests.size() > 0) {273 generateTable(sb, "PASSED TESTS", passedTests, "passed", NAME_COMPARATOR);274 }275 if (skippedTests.size() > 0) {276 generateTable(sb, "SKIPPED TESTS", skippedTests, "skipped", NAME_COMPARATOR);277 }278 sb.append("</body>\n</html>");279 Utils.writeFile(outputDirectory, getOutputFile(testContext), sb.toString());280 }281 private static void ppp(String s) {282 System.out.println("[TestHTMLReporter] " + s);283 }284 private static class NameComparator implements Comparator<ITestResult>, Serializable {285 private static final long serialVersionUID = 381775815838366907L;286 public int compare(ITestResult o1, ITestResult o2) {287 String c1 = o1.getMethod().getMethodName();288 String c2 = o2.getMethod().getMethodName();289 return c1.compareTo(c2);290 }...
Source:CustomReport.java
...28 startHtml();29 startTable();30 endTable();31 endHtml();32 TestHTMLReporter.generateTable(arg0, arg1, arg2, arg3, arg4); 33 }3435 private void endHtml() {36 pwriter.println("</body>");37 pwriter.println("</html>");38 }3940 private void endTable() {41 pwriter.println("</table>");42 }4344 private void startTable() {45 pwriter.println("<table width='100%' border='5' cellpadding='2' cellspacing='2'>");46
...
generateTable
Using AI Code Generation
1public class TestHTMLReporterExample {2 public static void main(String[] args) {3 TestHTMLReporter testHTMLReporter = new TestHTMLReporter();4 String html = testHTMLReporter.generateTable(new String[]{"Id", "Name"}, new String[][]{{"1", "A"}, {"2", "B"}});5 System.out.println(html);6 }7}
generateTable
Using AI Code Generation
1import org.testng.annotations.Test;2import org.testng.reporters.TestHTMLReporter;3public class TestNgHtmlTable {4 public void testHtmlTable() {5 String[][] data = {{"a", "b", "c"}, {"d", "e", "f"}, {"g", "h", "i"}};6 String htmlTable = TestHTMLReporter.generateTable(data);7 System.out.println(htmlTable);8 }9}
generateTable
Using AI Code Generation
1import org.testng.reporters.TestHTMLReporter;2public class TestHTMLReporterExample {3 public static void main(String[] args) {4 String[][] data = new String[][]{5 {"1", "2", "3"},6 {"4", "5", "6"},7 {"7", "8", "9"}8 };9 String html = TestHTMLReporter.generateTable(data);10 System.out.println(html);11 }12}
generateTable
Using AI Code Generation
1import org.testng.reporters.TestHTMLReporter;2import java.io.BufferedWriter;3import java.io.FileWriter;4import java.io.IOException;5import java.util.HashMap;6import java.util.Map;7import org.testng.TestNG;8import org.testng.xml.XmlSuite;9import org.testng.xml.XmlTest;10public class TestHTMLReporterDemo {11 public static void main(String[] args) throws IOException {12 TestHTMLReporter testHTMLReporter = new TestHTMLReporter();13 Map<String, String> params = new HashMap<String, String>();14 params.put("test", "testng.xml");15 XmlSuite xmlSuite = new XmlSuite();16 xmlSuite.setParameters(params);17 XmlTest xmlTest = new XmlTest(xmlSuite);18 xmlTest.setName("testng.xml");19 TestNG testNG = new TestNG();20 testNG.setXmlSuites(java.util.Arrays.asList(xmlSuite));21 testNG.setOutputDirectory("target");22 testNG.run();23 String html = testHTMLReporter.generateTable(xmlSuite);24 BufferedWriter writer = new BufferedWriter(new FileWriter("testng.html"));25 writer.write(html);26 writer.close();27 }28}
TestNG 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!!