[英]Getting Null Pointer Exception with Extent Reports
I m trying to run a small test program with Extent Reports but getting Null Pointer Exception.我正在尝试使用 Extent Reports 运行一个小型测试程序,但得到 Null 指针异常。 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)
I have just instantiate the extent variable outside the clas.我刚刚在类之外实例化了范围变量。 And it's working for me now.
它现在对我有用。 ExtentReports extent = new ExtentReports();
ExtentReports 范围 = new ExtentReports();
ExtentReports extent = new ExtentReports();
Don't initialize in @BeforeTest methods once you declared it Publicly.公开声明后,不要在@BeforeTest方法中进行初始化。 Instead Use:
而是使用:
extent = new ExtentReports();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.