Best Testng code snippet using org.testng.reporters.TestHTMLReporter.onFinish
Source:TestngListener.java
...6* @method : onTestFailure()7* @method : onTestSkipped()8* @method : onTestFailedButWithinSuccessPercentage() 9* @method : onStart()10* @method : onFinish()11* @method 12*/13package com.test.baselib;14import java.io.File;15import java.io.FileOutputStream;16import java.io.IOException;17import java.io.OutputStream;18import java.text.SimpleDateFormat;19import java.util.ArrayList;20import java.util.Date;21import java.util.HashMap;22import org.apache.commons.io.FileUtils;23import org.apache.log4j.Logger;24import org.apache.poi.hssf.usermodel.HSSFSheet;25import org.apache.poi.hssf.usermodel.HSSFWorkbook;26import org.apache.xmlbeans.impl.tool.XSTCTester.TestCase;27import org.apache.xmlbeans.impl.tool.XSTCTester.TestCaseResult;28import org.openqa.selenium.OutputType;29import org.openqa.selenium.TakesScreenshot;30import org.testng.ITestContext;31import org.testng.ITestListener;32import org.testng.ITestResult;33import org.testng.TestException;34import org.testng.internal.TestResult;35import org.testng.reporters.TestHTMLReporter;36import org.testng.xml.XmlSuite;37import org.uncommons.reportng.JUnitXMLReporter.TestClassResults;38public class TestngListener implements ITestListener 39{40 public Logger qrLog;41 public static int iPassCount=0;42 public static int iFailCount=0;43 public static int iSkippedCount=0;44 public static ArrayList sTestName= new ArrayList<String>();45 public static ArrayList sStatus= new ArrayList<String>();46 public static String sSheet1="Results";47 public static String sSheet2="Test Status";48 public static File reportLocation;49 public static File sResults;50 public static File sLogs;51 public static File sScreenShots;52 public static String pdfngreportPath=BaseLib.sDirPath+"\\pdfngreport.properties";53 public static String pdfScreenShotsPAth;54 int cnt=0;55 static int i=0;56 public TestngListener() throws IOException 57 {58 Date date = new Date();59 SimpleDateFormat sdf=new SimpleDateFormat("dd-MM-yyyy_hh_mm_ss");60 String sdate1 = sdf.format(date);61 System.setProperty("sFileName",sdate1);62 63 reportLocation=new File(BaseLib.sDirPath+"\\..\\Reports\\TestHouzifyReports"+sdate1);64 sResults=new File(reportLocation.getAbsolutePath()+"\\Results");65 sLogs=new File(reportLocation.getAbsolutePath()+"\\Logs");66 sScreenShots=new File(reportLocation.getAbsolutePath()+"\\ScreenShots");67 GenericLib.sFile=sResults.getAbsolutePath()+"\\TestResult_"; 68 69 FileUtils.forceMkdir(reportLocation);70 FileUtils.forceMkdir(sResults);71 FileUtils.forceMkdir(sLogs);72 FileUtils.forceMkdir(sScreenShots);73 qrLog= Logger.getLogger(this.getClass());74 try{75 System.out.println();76 GenericLib.setCongigValue(pdfngreportPath,"pdfreport.outputdir",reportLocation.getAbsolutePath().replace("\\","/"));77 GenericLib.setCongigValue(pdfngreportPath,"pdfreport.selenium.failed.test.screenshot.outputdir",sScreenShots.getAbsolutePath().replace("\\","/"));78 GenericLib.setCongigValue(pdfngreportPath,"pdfreport.pie.chart.type","normal");79 GenericLib.sFile=GenericLib.sFile+sdate1+".xlsx";80 81 OutputStream fos = new FileOutputStream(GenericLib.sFile);82 HSSFWorkbook hwb = new HSSFWorkbook();83 HSSFSheet sheet = hwb.createSheet(sSheet1);84 hwb.createSheet(sSheet2);85 FileOutputStream fileOut = new FileOutputStream(GenericLib.sFile);86 hwb.write(fos);87 fileOut.close();88 }89 catch(Exception e)90 {91 e.printStackTrace();92 }93 }94 95 public void onTestStart(ITestResult result) 96 {97 qrLog.info("TESTCASE ID = "+result.getName().toString()+" ");98 }99 100 public void onTestSuccess(ITestResult result) 101 {102 GenericLib.setStatus(result.getName().toString(), "Passed",sTestName,sStatus);103 qrLog.info("TEST STATUS = PASSED"+" ");104 qrLog.info("___________________________________________________");105 qrLog.info(" ");106 107 }108 public void onTestFailure(ITestResult result) 109 {110 Date date = new Date();111 SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy_hh_mm_ss");112 String sdate = sdf.format(date);113 114 String sClass=result.getTestClass().getRealClass().toString();115 sClass= sClass.replace("class ", "");116 117 String sPackage=result.getTestClass().getRealClass().getPackage().toString();118 sPackage = sPackage.replace("package ", "");119 120 String sImage = sClass.replace(sPackage+".","");121 sImage = sImage+"_"+result.getName().toString();122 123 Object obj=result.getInstance();124 BaseLib baseLib = (BaseLib)obj;125 qrLog.error("TEST STATUS = FAILED"+" ");126 qrLog.info("___________________________________________________");127 qrLog.info(" ");128 File imgFile = ((TakesScreenshot) baseLib.driver).getScreenshotAs(OutputType.FILE);129 System.out.println(imgFile.getAbsolutePath());130 try131 {132 FileUtils.copyFile(imgFile, new File(sScreenShots.getAbsolutePath()+"\\"+sImage+".png"));133 } catch (IOException e) {134 e.printStackTrace();135 }136 GenericLib.setStatus(result.getName().toString(), "Failed",sTestName,sStatus); 137 }138 public void onTestSkipped(ITestResult result) 139 {140 qrLog.error("TEST STATUS = SKIPPED"+" ");141 qrLog.info("___________________________________________________");142 qrLog.info(" ");143 GenericLib.setStatus(result.getName(), "Skipped",sTestName,sStatus);144 }145 146 public void onTestFailedButWithinSuccessPercentage(ITestResult result) 147 {148 qrLog.warn("");149 }150 public void onStart(ITestContext context) 151 {152 String suiteName = context.getCurrentXmlTest().toString();153 suiteName=suiteName.substring(suiteName.lastIndexOf("scripts"), suiteName.lastIndexOf("Test] ")).replace("scripts.","");154 qrLog.info("###################### START OF THE -"+ suiteName+"Test ######################");155 }156 157 public void onFinish(ITestContext context) 158 {159 String suiteName = context.getCurrentXmlTest().toString();160 suiteName=suiteName.substring(suiteName.lastIndexOf("scripts"), suiteName.lastIndexOf("Test] ")).replace("scripts.","");161 GenericLib.toWriteIntoExcel(sTestName,sStatus);162 qrLog.info("###################### END OF THE -"+ suiteName+"Test ######################");163 qrLog.info(" ");164 qrLog.info(" ");165 }166 167 168 169}...
Source:RemoteTestNG.java
...129 RemoteSuiteListener(StringMessageSenderHelper smsh) {130 m_messageSender= smsh;131 }132 @Override133 public void onFinish(ISuite suite) {134 m_messageSender.sendMessage(new SuiteMessage(suite, false /*start*/));135 }136 @Override137 public void onStart(ISuite suite) {138 m_messageSender.sendMessage(new SuiteMessage(suite, true /*start*/));139 }140 }141 private static class DelegatingTestRunnerFactory implements ITestRunnerFactory {142 private final ITestRunnerFactory m_delegateFactory;143 private final StringMessageSenderHelper m_messageSender;144 DelegatingTestRunnerFactory(ITestRunnerFactory trf, StringMessageSenderHelper smsh) {145 m_delegateFactory= trf;146 m_messageSender= smsh;147 }...
Source:Reporting.java
...76 logger=extent.createTest(tr.getName()); // create new entry in the report77 logger.log(Status.SKIP,MarkupHelper.createLabel(tr.getName(),ExtentColor.ORANGE));78 }79 80 public void onFinish(ITestContext testContext)81 {82 extent.flush();83 }84 85 86}
Source:ExtentReporting.java
...60 {61 test=report.createTest(result.getName());62 test.log(Status.SKIP,MarkupHelper.createLabel(result.getName(), ExtentColor.GREY));63 }64 public void onFinish(ITestContext context)65 //public void onFinish()66 {67 report.flush();68 // System.out.println("I am called 5");69 }70}...
onFinish
Using AI Code Generation
1org.testng.reporters.TestHTMLReporter reporter = new org.testng.reporters.TestHTMLReporter();2reporter.onFinish(new org.testng.TestNG());3java.lang.reflect.Method m = org.testng.reporters.TestHTMLReporter.class.getDeclaredMethod("onFinish", org.testng.ITestContext.class);4m.setAccessible(true);5m.invoke(reporter, new org.testng.TestNG());6Source Project: testng Source File: TestHTMLReporterTest.java License: Apache License 2.0 5 votes private static void createReport() throws Exception { TestHTMLReporter reporter = new TestHTMLReporter(); reporter.onStart(new TestNG()); reporter.onFinish(new TestNG()); }7Source Project: testng Source File: TestHTMLReporterTest.java License: Apache License 2.0 5 votes private static void createReport() throws Exception { TestHTMLReporter reporter = new TestHTMLReporter(); reporter.onStart(new TestNG()); reporter.onFinish(new TestNG()); }8Source Project: testng Source File: TestHTMLReporterTest.java License: Apache License 2.0 5 votes private static void createReport() throws Exception { TestHTMLReporter reporter = new TestHTMLReporter(); reporter.onStart(new TestNG()); reporter.onFinish(new TestNG()); }9Source Project: testng Source File: TestHTMLReporterTest.java License: Apache License 2.0 5 votes private static void createReport() throws Exception { TestHTMLReporter reporter = new TestHTMLReporter(); reporter.onStart(new TestNG()); reporter.onFinish(new TestNG()); }10Source Project: testng Source File: TestHTMLReporterTest.java License: Apache License 2.0 5 votes private static void createReport() throws Exception { TestHTMLReporter reporter = new TestHTMLReporter(); reporter.onStart(new TestNG()); reporter.onFinish(new TestNG()); }11Source Project: testng Source File: TestHTMLReporterTest.java License: Apache License 2.0 5 votes private static void createReport() throws Exception { TestHTMLReporter reporter = new TestHTMLReporter(); reporter.onStart(new TestNG()); reporter.onFinish(new TestNG()); }
onFinish
Using AI Code Generation
1import org.testng.TestListenerAdapter;2import org.testng.TestNG;3import org.testng.reporters.TestHTMLReporter;4public class TestNGCustomReport {5 public static void main(String[] args) {6 TestListenerAdapter tla = new TestListenerAdapter();7 TestHTMLReporter htmlReporter = new TestHTMLReporter();8 TestNG testng = new TestNG();9 testng.setTestClasses(new Class[] { TestClass.class });10 testng.addListener(tla);11 testng.addListener(htmlReporter);12 testng.run();13 htmlReporter.onFinish(tla);14 }15}
onFinish
Using AI Code Generation
1package org.testng.reporters;2import java.io.File;3import java.io.IOException;4import java.nio.charset.Charset;5import java.nio.file.Files;6import java.nio.file.Path;7import java.nio.file.Paths;8import org.testng.IReporter;9import org.testng.ISuite;10import org.testng.xml.XmlSuite;11import org.apache.commons.io.FileUtils;12import org.apache.commons.io.FilenameUtils;13public class TestHTMLReporter implements IReporter {14 private static final String OUTPUT_DIRECTORY = "test-output";15 private static final String TEST_OUTPUT_DIRECTORY = "test-output";16 private static final String TESTNG_REPORTS = "testng-reports";17 private static final String TESTNG_CUSTOM_CSS = "testng-custom.css";18 private static final String TESTNG_REPORTS_CSS = "testng-reports.css";19 private static final String TESTNG_REPORTS_CSS_BAK = "testng-reports.css.bak";20 public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {21 String outputDir = OUTPUT_DIRECTORY;22 File outputDirFile = new File(outputDir);23 if (!outputDirFile.exists()) {24 outputDirFile.mkdirs();25 }26 String customCssFile = TESTNG_CUSTOM_CSS;27 File customCssFileFile = new File(customCssFile);28 if (customCssFileFile.exists()) {29 try {30 String testOutputDir = TEST_OUTPUT_DIRECTORY;31 String testngReportsDir = TESTNG_REPORTS;32 String testngReportsCss = TESTNG_REPORTS_CSS;33 String testngReportsCssBak = TESTNG_REPORTS_CSS_BAK;34 File testOutputDirFile = new File(testOutputDir);35 if (!testOutputDirFile.exists()) {36 testOutputDirFile.mkdirs();37 }38 String testngReportsDirPath = testOutputDir + File.separator + testngReportsDir;39 File testngReportsDirFile = new File(testngReportsDirPath);40 if (!testngReportsDirFile.exists()) {41 testngReportsDirFile.mkdirs();42 }43 String testngReportsCssFile = testngReportsDirPath + File.separator + testngReportsCss;44 String testngReportsCssBakFile = testngReportsDirPath + File.separator + testngReportsCssBak;45 File testngReportsCssFileFile = new File(testngReports
onFinish
Using AI Code Generation
1package com.testng.listeners;2import java.io.File;3import java.io.FileNotFoundException;4import java.io.FileOutputStream;5import java.io.IOException;6import java.io.PrintStream;7import java.io.PrintWriter;8import java.io.StringWriter;9import java.io.Writer;10import java.util.Date;11import org.testng.ITestContext;12import org.testng.ITestListener;13import org.testng.ITestResult;14public class CustomTestListener implements ITestListener {15 * (non-Javadoc)16 * @see org.testng.ITestListener#onStart(org.testng.ITestContext)17 public void onStart(ITestContext context) {18 System.out.println("Test started");19 }
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!!