ExtentReport记录器NullPointerException

时间:2018-05-28 00:39:16

标签: testng extentreports selenium-extent-report

无法弄清楚为什么我在这里变空。

ExtentReport在@AfterMethod注释块中失败。如果您看到我的@AfterMethod注释块,则此行“logger.log(LogStatus.PASS, result.getName() + " passed");”失败。它在控制台中显示NullPointerException。

package com.cna.qa.testcases;

import java.io.File;
import java.ioenter code here.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

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

import junit.framework.Assert;

public class VerifyTitle {

    public ExtentReports extent;
    public ExtentTest logger;
    public WebDriver driver;

    @BeforeTest
    public void beforetest() {
        extent = new ExtentReports("C:\\Users\\mm\\selenium_projects\\SmokeTest\\output\\extentReport.html", true);
    }

    @Test
    public void verifyBlogTitle1() {

        System.setProperty("webdriver.gecko.driver", "C:\\Users\\mmia\\IdeaProjects\\geckoDriver\\geckodriver.exe");
        driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.get("https://devxx.yyy.net");

        String title = driver.getTitle();
        Assert.assertTrue(title.contains("Dashboard"));
    }

    public static String screenshot(WebDriver driver, String screenshotname) throws IOException {
        String dtname = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
        TakesScreenshot ts = (TakesScreenshot) driver;
        File source = ts.getScreenshotAs(OutputType.FILE);
        String destination = System.getProperty("user.dir") + "\\screenshots\\" + screenshotname + "_" + dtname + ".png";
        File finalDestination = new File(destination);
        FileUtils.copyFile(source, finalDestination);

        return destination;
    }

    @AfterMethod()
    public void teardown(ITestResult result) throws IOException {

        if (result.getStatus() == ITestResult.SUCCESS) {
            System.out.println(result.getStatus());
            logger.log(LogStatus.PASS, result.getName() + " passed");

        } else if (result.getStatus() == ITestResult.FAILURE) {
            logger.log(LogStatus.FAIL, "Test failed name is " + result.getName()); // failed test case name
            logger.log(LogStatus.FAIL, "Test failed name is " + result.getThrowable()); // faile test case error
            String screenshotPath = VerifyTitle.screenshot(driver, result.getName());
            logger.log(LogStatus.FAIL, screenshotPath);
        } else if (result.getStatus() == ITestResult.SKIP) {
            logger.log(LogStatus.SKIP, "Test skipped name is " + result.getName()); // skipped test case name
        }

        extent.endTest(logger); // ending test and send all log messages

        driver.quit();
    }

    @AfterTest
    public void aftertest() {
        extent.flush();
        extent.close();
    }`enter code here`

}

这是我在控制台中看到的:

1
FAILED CONFIGURATION: @AfterMethod teardown([TestResult name=verifyBlogTitle1 status=SUCCESS method=VerifyTitle.verifyBlogTitle1()[pri:0, instance:com.cna.qa.testcases.VerifyTitle@1e397ed7] output={null}])
java.lang.NullPointerException
    at com.cna.qa.testcases.VerifyTitle.teardown(VerifyTitle.java:77)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
    at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:59)
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:451)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:222)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:634)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:707)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:979)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1187)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1116)
    at org.testng.TestNG.runSuites(TestNG.java:1028)
    at org.testng.TestNG.run(TestNG.java:996)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

2 个答案:

答案 0 :(得分:1)

据我所知,你还没有定义任何ExtentTest的Object值,你刚刚定义了ExtentReport位置,没有这样的赋值或创建Report。

您需要定义它,logger = extent.startTest("Description");

尝试使用简单消息生成报告,如果得到它,请尝试实施并应用此ITestResult逻辑。

您正在使用版本2.41,实施this示例进行基本试用。

另外,我建议使用Version 3

答案 1 :(得分:1)

如代码中所示,您没有初始化ExtentTest logger;无处可能,这可能就是您获得null的原因。

尝试这样,你的代码调整了:

   @BeforeTest
    public void beforetest() {
        extent = new ExtentReports("C:\\Users\\mm\\selenium_projects\\SmokeTest\\output\\extentReport.html", true);
        logger = extent.createTest(testName, desc); // enter testName, desc....
    }