There are two steps:
1. First Create JyperionListener.java
2. Then use JyperionListener.java in any TestNG class (like we have developed google.java)
1. Create class JyperionListener.java
2. Use JyperionListener.java in TestNG class, find below code of TestNG class:
1. First Create JyperionListener.java
2. Then use JyperionListener.java in any TestNG class (like we have developed google.java)
1. Create class JyperionListener.java
package reporting; import java.awt.Color; import java.io.FileOutputStream; import java.util.Date; import java.util.HashMap; import java.util.Random; import java.util.Set; import org.testng.ITestContext; import org.testng.ITestListener; import org.testng.ITestResult; import reusable.Base; import com.lowagie.text.Chunk; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.Element; import com.lowagie.text.Font; import com.lowagie.text.FontFactory; import com.lowagie.text.Paragraph; import com.lowagie.text.pdf.PdfAction; import com.lowagie.text.pdf.PdfPCell; import com.lowagie.text.pdf.PdfPTable; import com.lowagie.text.pdf.PdfWriter; public class JyperionListener implements ITestListener { /** * Document */ private Document document = null; /** * PdfPTables */ PdfPTable successTable = null, failTable = null; /** * throwableMap */ private HashMap<Integer, Throwable> throwableMap = null; /** * nbExceptions */ private int nbExceptions = 0; public JyperionListener() { log("JyperionListener()"); this.document = new Document(); this.throwableMap = new HashMap<Integer, Throwable>(); } /** * @see com.beust.testng.ITestListener#onTestSuccess(com.beust.testng.ITestResult) */ public void onTestSuccess(ITestResult result) { log("onTestSuccess("+result+")"); if (successTable == null) { this.successTable = new PdfPTable(new float[]{.3f, .3f, .1f, .3f}); Paragraph p = new Paragraph("PASSED TESTS", new Font(Font.TIMES_ROMAN, Font.DEFAULTSIZE, Font.BOLD)); p.setAlignment(Element.ALIGN_CENTER); PdfPCell cell = new PdfPCell(p); cell.setColspan(4); cell.setBackgroundColor(Color.GREEN); this.successTable.addCell(cell); cell = new PdfPCell(new Paragraph("Class")); cell.setBackgroundColor(Color.LIGHT_GRAY); this.successTable.addCell(cell); cell = new PdfPCell(new Paragraph("Method")); cell.setBackgroundColor(Color.LIGHT_GRAY); this.successTable.addCell(cell); cell = new PdfPCell(new Paragraph("Time (ms)")); cell.setBackgroundColor(Color.LIGHT_GRAY); this.successTable.addCell(cell); cell = new PdfPCell(new Paragraph("Exception")); cell.setBackgroundColor(Color.LIGHT_GRAY); this.successTable.addCell(cell); } PdfPCell cell = new PdfPCell(new Paragraph(result.getTestClass().toString())); this.successTable.addCell(cell); cell = new PdfPCell(new Paragraph(result.getMethod().getMethodName().toString())); this.successTable.addCell(cell); cell = new PdfPCell(new Paragraph("" + (result.getEndMillis()-result.getStartMillis()))); this.successTable.addCell(cell); Throwable throwable = result.getThrowable(); if (throwable != null) { this.throwableMap.put(new Integer(throwable.hashCode()), throwable); this.nbExceptions++; Paragraph excep = new Paragraph( new Chunk(throwable.toString(), new Font(Font.TIMES_ROMAN, Font.DEFAULTSIZE, Font.UNDERLINE)). setLocalGoto("" + throwable.hashCode())); cell = new PdfPCell(excep); this.successTable.addCell(cell); } else { this.successTable.addCell(new PdfPCell(new Paragraph(""))); } } /** * @see com.beust.testng.ITestListener#onTestFailure(com.beust.testng.ITestResult) */ public void onTestFailure(ITestResult result) { log("onTestFailure("+result+")"); String file = System.getProperty("user.dir")+"\\Test Results\\Screenshots\\"+"screenshot"+(new Random().nextInt())+".png"; try { Base.takeSnapShot(Base.getDriver(), file); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } if (this.failTable == null) { this.failTable = new PdfPTable(new float[]{.3f, .3f, .1f, .3f}); this.failTable.setTotalWidth(20f); Paragraph p = new Paragraph("FAILED TESTS", new Font(Font.TIMES_ROMAN, Font.DEFAULTSIZE, Font.BOLD)); p.setAlignment(Element.ALIGN_CENTER); PdfPCell cell = new PdfPCell(p); cell.setColspan(4); cell.setBackgroundColor(Color.RED); this.failTable.addCell(cell); cell = new PdfPCell(new Paragraph("Class")); cell.setBackgroundColor(Color.LIGHT_GRAY); this.failTable.addCell(cell); cell = new PdfPCell(new Paragraph("Method")); cell.setBackgroundColor(Color.LIGHT_GRAY); this.failTable.addCell(cell); cell = new PdfPCell(new Paragraph("Time (ms)")); cell.setBackgroundColor(Color.LIGHT_GRAY); this.failTable.addCell(cell); cell = new PdfPCell(new Paragraph("Exception")); cell.setBackgroundColor(Color.LIGHT_GRAY); this.failTable.addCell(cell); } PdfPCell cell = new PdfPCell(new Paragraph(result.getTestClass().toString())); this.failTable.addCell(cell); cell = new PdfPCell(new Paragraph(result.getMethod().getMethodName().toString())); this.failTable.addCell(cell); cell = new PdfPCell(new Paragraph("" + (result.getEndMillis()-result.getStartMillis()))); this.failTable.addCell(cell); //String exception = result.getThrowable() == null ? "" : result.getThrowable().toString(); //cell = new PdfPCell(new Paragraph(exception)); //this.failTable.addCell(cell); Throwable throwable = result.getThrowable(); if (throwable != null) { this.throwableMap.put(new Integer(throwable.hashCode()), throwable); this.nbExceptions++; Chunk imdb = new Chunk("[SCREEN SHOT]", new Font(Font.TIMES_ROMAN, Font.DEFAULTSIZE, Font.UNDERLINE)); imdb.setAction(new PdfAction("file:///"+file)); Paragraph excep = new Paragraph( throwable.toString()); excep.add(imdb); //Paragraph excep = new Paragraph(ck.setLocalGoto("" + throwable.hashCode())); cell = new PdfPCell(excep); this.failTable.addCell(cell); } else { this.failTable.addCell(new PdfPCell(new Paragraph(""))); } } /** * @see com.beust.testng.ITestListener#onTestSkipped(com.beust.testng.ITestResult) */ public void onTestSkipped(ITestResult result) { log("onTestSkipped("+result+")"); } /** * @see com.beust.testng.ITestListener#onStart(com.beust.testng.ITestContext) */ public void onStart(ITestContext context) { log("onStart("+context+")"); try { PdfWriter.getInstance(this.document, new FileOutputStream(System.getProperty("user.dir")+"\\Test Results\\Automation Test Report"+".pdf")); } catch (Exception e) { e.printStackTrace(); } this.document.open(); Paragraph p = new Paragraph(context.getName() + " TESTNG RESULTS", FontFactory.getFont(FontFactory.HELVETICA, 20, Font.BOLD, new Color(0, 0, 255))); try { this.document.add(p); this.document.add(new Paragraph(new Date().toString())); } catch (DocumentException e1) { e1.printStackTrace(); } } /** * @see com.beust.testng.ITestListener#onFinish(com.beust.testng.ITestContext) */ public void onFinish(ITestContext context) { log("onFinish("+context+")"); try { if (this.failTable != null) { log("Added fail table"); this.failTable.setSpacingBefore(15f); this.document.add(this.failTable); this.failTable.setSpacingAfter(15f); } if (this.successTable != null) { log("Added success table"); this.successTable.setSpacingBefore(15f); this.document.add(this.successTable); this.successTable.setSpacingBefore(15f); } } catch (DocumentException e) { e.printStackTrace(); } Paragraph p = new Paragraph("EXCEPTIONS SUMMARY", FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD, new Color(255, 0, 0))); try { this.document.add(p); } catch (DocumentException e1) { e1.printStackTrace(); } Set<Integer> keys = this.throwableMap.keySet(); assert keys.size() == this.nbExceptions; for(Integer key : keys) { Throwable throwable = this.throwableMap.get(key); Chunk chunk = new Chunk(throwable.toString(), FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD, new Color(255, 0, 0))); chunk.setLocalDestination("" + key); Paragraph throwTitlePara = new Paragraph(chunk); try { this.document.add(throwTitlePara); } catch (DocumentException e3) { e3.printStackTrace(); } StackTraceElement[] elems = throwable.getStackTrace(); String exception = ""; for(StackTraceElement ste : elems) { Paragraph throwParagraph = new Paragraph(ste.toString()); try { this.document.add(throwParagraph); } catch (DocumentException e2) { e2.printStackTrace(); } } } this.document.close(); } /** * log * @param o */ public static void log(Object o) { //System.out.println("[JyperionListener] " + o); } public void onTestFailedButWithinSuccessPercentage(ITestResult arg0) { // TODO Auto-generated method stub } public void onTestStart(ITestResult arg0) { // TODO Auto-generated method stub } }
2. Use JyperionListener.java in TestNG class, find below code of TestNG class:
import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; import org.openqa.selenium.WebDriver; import org.testng.Assert; import org.testng.annotations.Listeners; import org.testng.annotations.Test; import reporting.JyperionListener; import reusable.Base; @Listeners(JyperionListener.class) public class google { static Logger log = Logger.getLogger(NewTest.class); static Logger elog = Logger.getLogger("errorLog"); WebDriver driver; @Test public void google1() { PropertyConfigurator.configure("src/main/resources/utilities/log4j.properties"); log.info("Starting first test case"); driver = Base.getDriver(); driver.get("http://www.google.com/"); Assert.assertTrue(false); elog.error("Test Case Fail"); } }
No comments:
Post a Comment