1

无法弄清楚为什么我在这里得到空值。

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)
4

2 回答 2

1

据我所知,您还没有定义 ExtentTest 的任何 Object 值,您只是定义了 ExtentReport 位置,没有这样的分配或创建报告。

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

尝试使用简单消息生成报告,如果你得到它,实现并应用这个 ITestResult 逻辑。

您使用的是 2.41 版,实现示例进行基本试用。

另外,我建议使用Version 3

于 2018-05-28T06:39:37.073 回答
1

正如代码中所见,你没有在ExtentTest logger;任何地方初始化,所以可能这就是你得到空值的原因。

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

   @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....
    }
于 2018-05-28T06:40:22.787 回答