In this post we will implement the extent report using listeners. Below complete implementation code given in the form of different classes/components.
POM.xml: to setup selenium-java, testng, extent report etc dependencies.
BaseClass: webdriver initialization, setup method and common methods.
extent-config.xml to configure Extent Report
ExtentManger Class: to setup extent report
Listener Class: configure listener class
TestClass: create test cases
testng.xml - to run test suites
Please refer below YouTube video to understand the above programs:
POM.xml: to setup selenium-java, testng, extent report etc dependencies.
<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>ExtentDemo</groupId>
<artifactId>ExtentDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>com.aventstack</groupId>
<artifactId>extentreports</artifactId>
<version>4.0.9</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
</dependencies>
</project>
BaseClass: webdriver initialization, setup method and common methods.
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.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import com.extentManager.ExtentManager;
public class BaseClass {
public static WebDriver driver;
@BeforeSuite
public void BeforeSuite() {
ExtentManager.setExtent();
}
@AfterSuite
public void AfterSuite() {
ExtentManager.endReport();
}
@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() throws IOException {
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;
}
public static String getCurrentTime() {
String currentDate = new SimpleDateFormat("yyyy-MM-dd-hhmmss").format(new Date());
return currentDate;
}
}
extent-config.xml to configure Extent Report
<?xml version="1.0" encoding="UTF-8"?>
<extentreports>
<configuration>
<!-- report theme -->
<!-- standard, dark -->
<theme>standard</theme>
<!-- document encoding -->
<!-- defaults to UTF-8 -->
<encoding>UTF-8</encoding>
<!-- protocol for script and stylesheets -->
<!-- defaults to https -->
<protocol>http</protocol>
<!-- title of the document -->
<documentTitle>OrangeHRMProject</documentTitle>
<!-- report name - displayed at top-nav -->
<reportName>OrangeHRMProject Report
<![CDATA[
<img src='D:/Workspace_Eclipse/ExtentDemo/Logo/ATI.png'/>
]]>
</reportName>
<!-- location of charts in the test view -->
<!-- top, bottom -->
<testViewChartLocation>bottom</testViewChartLocation>
<!-- custom javascript -->
<scripts>
<![CDATA[
$(document).ready(function() {
});
]]>
</scripts>
<!-- custom styles -->
<styles>
<![CDATA[
.report-name { padding-left: 18px; } .report-name > img { float:
left;height: 90%;margin-left: 30px;margin-top: 3px;width: auto; }
]]>
</styles>
</configuration>
</extentreports>
ExtentManger Class: to setup extent report
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.basePackage.BaseClass;
public class ExtentManager {
public static ExtentHtmlReporter htmlReporter;
public static ExtentReports extent;
public static ExtentTest test;
public static void setExtent() {
htmlReporter= new ExtentHtmlReporter(System.getProperty("user.dir")+"/test-output/ExtentReport/"+"MyReport_"+BaseClass.getCurrentTime()+".html");
htmlReporter.loadXMLConfig(System.getProperty("user.dir")+"/extent-config.xml");
//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");
}
public static void endReport() {
extent.flush();
}
}
Listener Class: configure listener class
import java.io.IOException;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.markuputils.ExtentColor;
import com.aventstack.extentreports.markuputils.MarkupHelper;
import com.basePackage.BaseClass;
import com.extentManager.ExtentManager;
public class ListenerClass extends ExtentManager implements ITestListener {
public void onTestStart(ITestResult result) {
test = extent.createTest(result.getName());
}
public void onTestSuccess(ITestResult result) {
if (result.getStatus() == ITestResult.SUCCESS) {
test.log(Status.PASS, "Pass Test case is: " + result.getName());
}
}
public void onTestFailure(ITestResult result) {
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(BaseClass.driver, result.getName());
try {
test.addScreenCaptureFromPath(pathString);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void onTestSkipped(ITestResult result) {
if (result.getStatus() == ITestResult.SKIP) {
test.log(Status.SKIP, "Skipped Test case is: " + result.getName());
}
}
public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
// TODO Auto-generated method stub
}
public void onStart(ITestContext context) {
// TODO Auto-generated method stub
}
public void onFinish(ITestContext context) {
// TODO Auto-generated method stub
}
}
TestClass: create test cases
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
import org.testng.annotations.Test;
import com.basePackage.BaseClass;
import com.extentManager.ExtentManager;
public class OrangeHRMTest extends BaseClass {
@Test
public void loginPageTest() {
WebElement imgElement=driver.findElement(By.xpath("//*[@id=\"divLogo\"]/img"));
Assert.assertTrue(imgElement.isDisplayed());
}
@Test
public void 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="OrangeHRM1";
Assert.assertEquals(actualTitle, expectedTitle);
}
@Test
public void sampleCase() {
ExtentManager.test.createNode("Validation1");
Assert.assertTrue(true);
ExtentManager.test.createNode("Validation2");
Assert.assertTrue(true);
ExtentManager.test.createNode("Validation3");
Assert.assertTrue(true);
ExtentManager.test.createNode("Validation4");
Assert.assertTrue(true);
}
}
testng.xml - to run test suites
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<listeners>
<listener class-name="com.listener.ListenerClass"></listener>
</listeners>
<test thread-count="5" name="Test">
<classes>
<class name="com.test.OrangeHRMTest"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
Please refer below YouTube video to understand the above programs:
i got null pointer excepetion on listener class " test.log(Status.FAIL, MarkupHelper.createLabel(result.getName() + " - Test Case Failed", ExtentColor.RED));".
ReplyDeleteI dont understand know i used correct libraries and instruction as your video. Please help me out.
Let me know complete details so that i can help you out.
DeleteHow can I write test.log in test classes?
ReplyDeleteI'm getting null pointer exception if I write test.log in test classes
Hello Sir,
ReplyDeleteI have prepared one framework of the Website and its working fine in my system. But I want to share my this project to my PM without share the source-code. Is this possible to share any executive file and they run that file?. If yes then how to share and which file share?.
Thanks in Advance.
I followed as you said in video but report not generating
ReplyDeleteThe King Casino - Atlantic City, NJ | Jancasino
ReplyDeleteCome on wooricasinos.info in the King Casino gri-go.com for casinosites.one fun, 1등 사이트 no wagering requirements, delicious dining, and enjoyable casino gaming all at the https://jancasino.com/review/merit-casino/ heart of Atlantic City.
I want to create one report for multiple test define in testng.xml file.. But getting multiple extent report.. Any solution
ReplyDeleteHere is the testng.cml file code:
Delete