0
votes

I m trying to run a small test program with Extent Reports but getting Null Pointer Exception. If i am running this code, without using Extent Report functionality it's working fine. Kindly help. Below is my code-

package selenium.ExtentReports;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.reporter.ExtentSparkReporter;
import io.github.bonigarcia.wdm.WebDriverManager;

public class ExtentReportDemo {

    ExtentReports extent;
    WebDriver driver;

    @BeforeTest
    public void config() {

        WebDriverManager.chromedriver().setup();
        driver = new ChromeDriver();

        String path = System.getProperty("user.dir") + "//Reports//index.html";
        ExtentSparkReporter reporter = new ExtentSparkReporter(path);
        reporter.config().setReportName("Web Automation Results");
        reporter.config().setDocumentTitle("Test Results");

        ExtentReports extent = new ExtentReports();
        extent.attachReporter(reporter);
        extent.setSystemInfo("Tester", "Gaurav Singh");
    }

    @Test
    public void initialDemo() {
        extent.createTest("Initial Demo");
        driver.get("https://rahulshettyacademy.com");
        System.out.println(driver.getTitle());
        extent.flush();
    }

}

Error which i am getting

FAILED: initialDemo
java.lang.NullPointerException
    at selenium.ExtentReports.ExtentReportDemo.initialDemo(ExtentReportDemo.java:38)

2
While running test, only browser is loading (blank-without any URL), then after that i got this error. - Gaurav
May be changing from ExtentReports extent = new ExtentReports(); to extent = new ExtentReports(); help? - Dhamo
Yes, it's working now. - Gaurav

2 Answers

0
votes

I have just instantiate the extent variable outside the clas. And it's working for me now. ExtentReports extent = new ExtentReports();

0
votes
ExtentReports extent = new ExtentReports();

Don't initialize in @BeforeTest methods once you declared it Publicly. Instead Use :

extent = new ExtentReports();