Getting NullPointerException when trying to generate test reports using ExtentReports in TestNG

自作多情 提交于 2019-12-24 13:45:01

问题


I am trying to prepare test execution report using ExtentReports in TestNG. Below is my code -

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

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

public class TestNGExample {

WebDriver driver;
WebElement wb;
ExtentReports report = ExtentReports.get(TestNGExample.class);


@BeforeTest
public void open(){
    driver = new FirefoxDriver();
    driver.get("https://www.google.co.in/?gws_rd=ssl");
    driver.manage().window().maximize();
    report.init("E:\\Report\\report.html", true);
    report.log(LogStatus.INFO, "Browser is up and running");
}//open

@AfterClass
public void tearDown(){
    report.log(LogStatus.INFO, "Test Completed");
    driver.close();
    driver.quit();
}//tearDown


@Test
public void First(){
report.log(LogStatus.INFO, "Starting the test process");
String s =  driver.findElement(By.xpath(".//*[@id='hplogo']")).getAttribute("Title");
Assert.assertEquals("Google", s);
report.log(LogStatus.PASS, "Checking for the result");
report.attachScreenshot("E:\\Report.png");
report.endTest();
}//first

}//TestNGExample 

After running this code i am getting NullPointerException generated from ExtentReports. Below i am putting the complete exception which i am getting -

[TestNG] Running:
 C:\Users\Shantanu_Nandan\AppData\Local\Temp\testng-eclipse-1317498292\testng-customsuite.xml

log4j:WARN No appenders could be found for logger  (org.apache.http.client.protocol.RequestAddCookies).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
java.lang.NullPointerException
at  com.relevantcodes.extentreports.support.RegexMatcher.getNthMatch(RegexMatcher.java:30)
at com.relevantcodes.extentreports.Logger.log(Logger.java:65)
at com.relevantcodes.extentreports.AbstractLog.log(AbstractLog.java:68)
at com.relevantcodes.extentreports.AbstractLog.log(AbstractLog.java:77)
at com.relevantcodes.extentreports.ExtentReports.log(ExtentReports.java:168)
at com.Selenium_Practice.TestNGExample.open(TestNGExample.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
at org.testng.TestRunner.beforeRun(TestRunner.java:641)
at org.testng.TestRunner.run(TestRunner.java:609)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
FAILED CONFIGURATION: @BeforeTest open
java.lang.NullPointerException
at java.lang.String.replace(Unknown Source)
at com.relevantcodes.extentreports.Logger.log(Logger.java:65)
at com.relevantcodes.extentreports.AbstractLog.log(AbstractLog.java:68)
at com.relevantcodes.extentreports.AbstractLog.log(AbstractLog.java:77)
at com.relevantcodes.extentreports.ExtentReports.log(ExtentReports.java:168)
at com.Selenium_Practice.TestNGExample.open(TestNGExample.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at  org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
at org.testng.TestRunner.beforeRun(TestRunner.java:641)
at org.testng.TestRunner.run(TestRunner.java:609)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

 SKIPPED CONFIGURATION: @AfterClass tearDown
 SKIPPED: First

 ===============================================
 Default test
 Tests run: 1, Failures: 0, Skips: 1
 Configuration Failures: 1, Skips: 1
 ===============================================


 ===============================================
 Default suite
 Total tests run: 1, Failures: 0, Skips: 1
 Configuration Failures: 1, Skips: 1
 ===============================================

[TestNG] Time taken by org.testng.reporters.EmailableReporter2@1afea7d: 218 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@104a311: 17 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@19a06ac: 384 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 53 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@18ed237: 142 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@e5c649: 520 ms

回答1:


After you initialize the report by setting the file path.

report.init("E:\\Report\\report.html", true);

You have to start the test before adding logs to it

report.startTest("TestName");

bcoz all the log events are added under the test.Since the test itself has not started it is throwing an null pointer exception

code:

@BeforeTest
public void open(){
    driver = new FirefoxDriver();
    driver.get("https://www.google.co.in/?gws_rd=ssl");
    driver.manage().window().maximize();
    report.init("E:\\Report\\report.html", true);
    report.startTest("TestName");//start the test before adding logs to it
    report.log(LogStatus.INFO, "Browser is up and running");
}//open

I have tested the above code it is working fine.

Hope this helps you.Kindly get back if you have any doubts



来源:https://stackoverflow.com/questions/31747315/getting-nullpointerexception-when-trying-to-generate-test-reports-using-extentre

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!