Prerequisites

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
import java.io.IOException;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.reporter.ExtentSparkReporter;
import com.aventstack.extentreports.reporter.configuration.Theme;

public class KW 
{
    WebDriver driver;
    
    @BeforeClass
    public void SetBrowser() 
    {
    	System.setProperty("webdriver.chrome.driver", "/home/kodingwindow/drivers/chromedriver");
        driver = new ChromeDriver();
    }
    
    @Test
    public void VerifyTitle() throws IOException, InterruptedException 
    {
    	ExtentSparkReporter esr = new ExtentSparkReporter("/home/kodingwindow/ExtentReport.html");
        ExtentReports er = new ExtentReports();
        er.attachReporter(esr);
        
        esr.config().setDocumentTitle("Extent Report");
        esr.config().setTheme(Theme.DARK);
        
        ExtentTest et = 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");
        
        String actual = driver.getTitle();
        String expected = "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();
    }
    @AfterClass
    public void CloseDriver() 
    {
        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
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;

import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;

public class KW 
{
    WebDriver driver;
    ExtentReports er;
    ExtentTest et;
    
    @Test
    public void VerifyTitle()
    {
        er = new ExtentReports("/home/kodingwindow/ExtentReport.html");
        et = er.startTest("VerifyTitle");
        
    	System.setProperty("webdriver.chrome.driver", "/home/kodingwindow/drivers/chromedriver");
        driver = new ChromeDriver();
        
        driver.navigate().to("https://kodingwindow.com/testapp/");
        et.log(LogStatus.INFO,"Successfully Navigated to the URL");
        
        String actual = driver.getTitle();
        String expected = "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);
    }
    
    @AfterTest
    public void CloseDriver() 
    {
        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
===============================================


Advertisement