1. TestNG installed in Eclispse.
2. You need to download the following jar files for Extent Reports. Once downloaded, you can import the library by right-clicking on the project -> Properties -> Java Build Path -> Libraries -> Add External JARs...
ExtentReports ExtentReports For Selenium Apache FreeMarker RxJava
How to generate Extent Reports in Selenium WebDriver (Example 1)
KW.java
importjava.io.IOException;importorg.openqa.selenium.WebDriver;importorg.openqa.selenium.chrome.ChromeDriver;importorg.testng.Assert;importorg.testng.annotations.AfterClass;importorg.testng.annotations.BeforeClass;importorg.testng.annotations.Test;importcom.aventstack.extentreports.ExtentReports;importcom.aventstack.extentreports.ExtentTest;importcom.aventstack.extentreports.Status;importcom.aventstack.extentreports.reporter.ExtentSparkReporter;importcom.aventstack.extentreports.reporter.configuration.Theme;publicclassKW{WebDriverdriver;@BeforeClasspublicvoidSetBrowser(){System.setProperty("webdriver.chrome.driver","/home/kodingwindow/drivers/chromedriver");driver=newChromeDriver();}@TestpublicvoidVerifyTitle()throwsIOException,InterruptedException{ExtentSparkReporteresr=newExtentSparkReporter("/home/kodingwindow/ExtentReport.html");ExtentReportser=newExtentReports();er.attachReporter(esr);esr.config().setDocumentTitle("Extent Report");esr.config().setTheme(Theme.DARK);ExtentTestet=er.createTest("VerifyHomePageTitle");et.log(Status.INFO,"Chrome Browser Launched");driver.navigate().to("https://kodingwindow.com/testapp/");et.log(Status.INFO,"Successfully Navigated to the URL");Stringactual=driver.getTitle();Stringexpected="KodingWindow's TestApp";et.log(Status.INFO,"Actual Title: "+actual);et.log(Status.INFO,"Expected Title: "+expected);if(actual.equals(expected)){et.log(Status.PASS,"Test Passed");}else{et.log(Status.FAIL,"Test Failed");}Assert.assertEquals(actual,expected);er.flush();}@AfterClasspublicvoidCloseDriver(){driver.close();}}
Output
[RemoteTestNG] detected TestNG version 7.4.0
PASSED: VerifyTitle
===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================
How to generate Extent Reports in Selenium WebDriver (Example 2)
KW.java
importorg.openqa.selenium.WebDriver;importorg.openqa.selenium.chrome.ChromeDriver;importorg.testng.Assert;importorg.testng.annotations.AfterTest;importorg.testng.annotations.Test;importcom.relevantcodes.extentreports.ExtentReports;importcom.relevantcodes.extentreports.ExtentTest;importcom.relevantcodes.extentreports.LogStatus;publicclassKW{WebDriverdriver;ExtentReportser;ExtentTestet;@TestpublicvoidVerifyTitle(){er=newExtentReports("/home/kodingwindow/ExtentReport.html");et=er.startTest("VerifyTitle");System.setProperty("webdriver.chrome.driver","/home/kodingwindow/drivers/chromedriver");driver=newChromeDriver();driver.navigate().to("https://kodingwindow.com/testapp/");et.log(LogStatus.INFO,"Successfully Navigated to the URL");Stringactual=driver.getTitle();Stringexpected="KodingWindow's TestApp";et.log(LogStatus.INFO,"Actual Title: "+actual);et.log(LogStatus.INFO,"Expected Title: "+expected);if(actual.equals(expected)){et.log(LogStatus.PASS,"Test Passed");}else{et.log(LogStatus.FAIL,"Test Failed");}Assert.assertEquals(actual,expected);}@AfterTestpublicvoidCloseDriver(){er.endTest(et);er.flush();driver.close();}}
Output
[RemoteTestNG] detected TestNG version 7.4.0
PASSED: VerifyTitle
===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================
Comments and Reactions
Advertisement
Thank You
Dear User, Thank you for visitng KodingWindow. If you are interested in technical articles, latest technologies, and our journey further, please follow us on LinkedIn.