中使用ExtentReports生成测试报告时获取NullPoi

中使用ExtentReports生成测试报告时获取NullPoi

本文介绍了尝试在TestNG中使用ExtentReports生成测试报告时获取NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在尝试使用TestNG中的ExtentReports准备测试执行报告.下面是我的代码-

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

运行此代码后,我得到了从ExtentReports生成的NullPointerException.在下面,我提出了一个完整的例外-

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

推荐答案

通过设置文件路径初始化报告之后.

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所有日志事件都在测试下添加.由于测试本身尚未启动,因此会引发空指针异常

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

代码:

@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

这篇关于尝试在TestNG中使用ExtentReports生成测试报告时获取NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 23:04