我正在尝试使用TestNG中的ExtentReports准备测试执行报告。以下是我的代码 -
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。 下面我将提出完整的例外情况 -
[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
答案 0 :(得分:3)
通过设置文件路径初始化报告。
report.init("E:\\Report\\report.html", true);
您必须在向其添加日志之前开始测试
report.startTest("TestName");
bcoz在test下添加了所有日志事件。由于测试本身尚未启动,因此抛出空指针异常
<强>码强>:
@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
我已经测试了上面的代码,它运行正常。
希望这会对你有所帮助。如果你有任何疑问,请回复