Saturday, May 4, 2019

Extent report for multiple classes

pom.xml


 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
      <modelVersion>4.0.0</modelVersion>  
      <groupId>com.extentreport</groupId>  
      <artifactId>practice</artifactId>  
      <version>0.0.1-SNAPSHOT</version>  
      <dependencies>  
           <dependency>  
                <groupId>org.testng</groupId>  
                <artifactId>testng</artifactId>  
                <version>6.8</version>  
                <scope>test</scope>  
           </dependency>  
           <dependency>  
                <groupId>com.aventstack</groupId>  
                <artifactId>extentreports</artifactId>  
                <version>3.0.0</version>  
           </dependency>  
           <dependency>  
                <groupId>org.seleniumhq.selenium</groupId>  
                <artifactId>selenium-java</artifactId>  
                <version>3.141.59</version>  
           </dependency>  
      </dependencies>  
 </project>  

Base class


 package extentReportForMultipleTestsInAClass;  
 import org.openqa.selenium.WebDriver;  
 import org.testng.ITestContext;  
 import org.testng.ITestListener;  
 import org.testng.ITestResult;  
 import org.testng.annotations.AfterMethod;  
 import org.testng.annotations.AfterTest;  
 import org.testng.annotations.BeforeTest;  
 import com.aventstack.extentreports.ExtentReports;  
 import com.aventstack.extentreports.ExtentTest;  
 import com.aventstack.extentreports.Status;  
 import com.aventstack.extentreports.markuputils.ExtentColor;  
 import com.aventstack.extentreports.markuputils.MarkupHelper;  
 import com.aventstack.extentreports.reporter.ExtentHtmlReporter;  
 public class BaseTest implements ITestListener {  
      public WebDriver driver;  
      public static ExtentHtmlReporter htmlReporter;  
      public static ExtentReports extent;  
      public static ExtentTest test;  
      @BeforeTest  
      public void setUp() {  
           getReport();  
      }  
      @AfterTest  
      public void tearDown() {  
           extent.flush();  
      }  
      @AfterMethod  
      public void closeDriver() {  
           driver.quit();  
      }  
      public void onTestStart(ITestResult result) {  
      }  
      public void onTestSuccess(ITestResult result) {  
           test.log(Status.PASS, MarkupHelper.createLabel(result.getName() + " Test Case PASSED", ExtentColor.GREEN));  
      }  
      public void onTestFailure(ITestResult result) {  
           test.log(Status.FAIL,  
                     MarkupHelper.createLabel(result.getName() + " Test case FAILED due to below issues:", ExtentColor.RED));  
           test.fail(result.getThrowable());  
      }  
      public void onTestSkipped(ITestResult result) {  
           test.log(Status.SKIP, MarkupHelper.createLabel(result.getName() + " Test Case SKIPPED", ExtentColor.ORANGE));  
           test.skip(result.getThrowable());  
      }  
      public void onTestFailedButWithinSuccessPercentage(ITestResult result) {  
      }  
      public void onStart(ITestContext context) {  
      }  
      public void onFinish(ITestContext context) {  
      }  
      public static void getReport() {  
           htmlReporter = new ExtentHtmlReporter(System.getProperty("user.dir") + "/test-output/MyReport.html");  
           extent = new ExtentReports();  
           extent.attachReporter(htmlReporter);  
           extent.setSystemInfo("OS", "Windows 10");  
           extent.setSystemInfo("Host Name", "Test");  
           extent.setSystemInfo("Environment", "QA");  
           extent.setSystemInfo("QA Name", "Mutturaj Hulagabal");  
           htmlReporter.config().setChartVisibilityOnOpen(true);  
           htmlReporter.config().setDocumentTitle("Extent report");  
           htmlReporter.config().setReportName("Final Report");  
      }  
 }  

Tow test classes

 package extentReportForMultipleTestsInAClass;  
 import org.testng.Assert;  
 import org.testng.SkipException;  
 import org.testng.annotations.Test;  
 public class ClassA extends BaseTest {  
      @Test  
      public void classB1() {  
           test = extent.createTest("class A1");  
           driver.get("https://www.facebook.com/");  
           Assert.assertTrue(true);  
      }  
      @Test  
      public void classB2() {  
           test = extent.createTest("class A2");  
           Assert.assertTrue(false);  
      }  
      @Test  
      public void classB3() {  
           test = extent.createTest("class A3");  
           throw new SkipException("Skipped");  
      }  
 }  


 package extentReportForMultipleTestsInAClass;  
 import org.testng.Assert;  
 import org.testng.annotations.Test;  
 public class ClassB extends BaseTest {  
      @Test  
      public void classB1() {  
           test=extent.createTest("class B");  
           driver.get("https://www.facebook.com/");  
           test.info(driver.getTitle());  
           Assert.assertTrue(true);  
      }  
      @Test  
      public void classB2() {  
           test=extent.createTest("class B");  
           Assert.assertTrue(false);  
      }  
 }  

TestNG.ml file


 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">  
 <suite name="Suite">  
 <listeners>  
 <listener class-name="extentReportForMultipleTestsInAClass.BaseTest"/>  
 </listeners>  
      <test thread-count="5" name="Test1">  
           <classes>  
                <class name="extentReportForMultipleTestsInAClass.ClassA" />  
                <class name="extentReportForMultipleTestsInAClass.ClassB" />  
           </classes>  
      </test> <!-- Test -->  
 </suite> <!-- Suite -->  



Let me know what you think in the comment :) Happy coding!

No comments:

Post a Comment