Generate Extent report and attach screen shots in extent report

What is Extent Report
ExtentReports is an open-source reporting library useful for test automation. It can also be easily integrated with major testing frameworks like JUnit, NUnit, TestNG, etc. These reports are HTML rich documents that depict results as pie charts.

Extent Reports offer several advantages when compared to the built-in reports that are generated through JUnit and TestNG such as test stepwise report generation ,pie chart representation, adding screenshots etc.

What are the main classes in Extent Report
 ExtentHtmlReporter - To create/generate report and for look and feel of the reports.
 ExtentReports - Add your test cases in extent report.
 ExtentTest - update the status of the test cases like pass/fail/skip etc.

Please refer below Program to generate extent report.

BaseClass:
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
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;
import com.aventstack.extentreports.reporter.configuration.Theme;

public class BaseClass {
 public WebDriver driver;
 
 public ExtentHtmlReporter htmlReporter;
 public ExtentReports extent;
 public ExtentTest test;
 
 @BeforeSuite
 public void setExtent() {
  htmlReporter= new ExtentHtmlReporter(System.getProperty("user.dir")+"/test-output/ExtentReport/MyReport.html");
  
  htmlReporter.config().setDocumentTitle("Automation Test Report");
  htmlReporter.config().setReportName("OrangeHRM Test Automation Report");
  htmlReporter.config().setTheme(Theme.DARK);
  
  extent = new ExtentReports();
  extent.attachReporter(htmlReporter);
  
  extent.setSystemInfo("HostName", "MyHost");
  extent.setSystemInfo("ProjectName", "OrangeHRM");
  extent.setSystemInfo("Tester", "Hitendra");
  extent.setSystemInfo("OS", "Win10");
  extent.setSystemInfo("Browser", "Chrome");
  
 }
 
 @AfterSuite
 public void endReport() {
  extent.flush();
 }
 
 @BeforeMethod
 public void setup() {
   System.setProperty("webdriver.chrome.driver",
     "C:\\Users\\Hitendra\\Downloads\\chromedriver_win32 (1)\\chromedriver.exe");
   driver = new ChromeDriver();
   driver.manage().window().maximize();
   driver.get("https://opensource-demo.orangehrmlive.com/index.php/auth/validateCredentials");
 }
 
 @AfterMethod
 public void tearDown(ITestResult result) throws IOException {
  if(result.getStatus()==ITestResult.FAILURE) {
   test.log(Status.FAIL, MarkupHelper.createLabel(result.getName() + " - Test Case Failed", ExtentColor.RED));
   test.log(Status.FAIL, MarkupHelper.createLabel(result.getThrowable() + " - Test Case Failed", ExtentColor.RED));
   
   String pathString=BaseClass.screenShot(driver, result.getName());
   test.addScreenCaptureFromPath(pathString);
   
  } else if(result.getStatus()==ITestResult.SKIP) {
   test.log(Status.SKIP, "Skipped Test case is: "+result.getName());
  } else if(result.getStatus()==ITestResult.SUCCESS) {
   test.log(Status.PASS, "Pass Test case is: "+result.getName());
  }
  driver.close();
 } 
 
 public static String screenShot(WebDriver driver,String filename) {
  String dateName = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
  TakesScreenshot takesScreenshot = (TakesScreenshot) driver;
  File source = takesScreenshot.getScreenshotAs(OutputType.FILE);
  String destination = System.getProperty("user.dir")+"\\ScreenShot\\"+filename+"_"+dateName+".png";
  File finalDestination= new File(destination);
  try {
   FileUtils.copyFile(source, finalDestination);
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.getMessage();
  }
  return destination;
 }

}

TestClass:
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
import org.testng.annotations.Test;
import com.basePackage.BaseClass;

public class OrangeHRMTest extends BaseClass {
 
 @Test
 public void loginPageTest() {
  test=extent.createTest("loginPageTest");
  WebElement imgElement=driver.findElement(By.xpath("//*[@id=\"divLogo\"]/img"));
  Assert.assertTrue(imgElement.isDisplayed());
 }
 
 @Test
 public void loginTest() {
  test=extent.createTest("loginTest");
  driver.findElement(By.id("txtUsername")).sendKeys("admin");
  driver.findElement(By.id("txtPassword")).sendKeys("admin123");
  driver.findElement(By.id("btnLogin")).click();
        String actualTitle=driver.getTitle();
        String expectedTitle="OrangeHRM";
        Assert.assertEquals(actualTitle, expectedTitle);
 }

 @Test
 public void sampleCase() {
  test=extent.createTest("sampleCase");
  test.createNode("Validation1");
  Assert.assertTrue(true);
  test.createNode("Validation2");
  Assert.assertTrue(true);
  test.createNode("Validation3");
  Assert.assertTrue(true);
  test.createNode("Validation4");
  Assert.assertTrue(true);
 }
}

Please refer below YouTube video to understand the above program:

1 comment:

  1. You also must be able to communicate with customers the highest level of search engine optimization. Sales useful, but not necessary, because this ability is the main strategy and client management role. Leadgeneration

    ReplyDelete