范围报告的空指针异常

时间:2016-09-11 08:39:23

标签: selenium-webdriver

这可能是一个愚蠢的问题但是很有帮助,你可以帮助我。 我尝试为多个测试用例实现范围报告,但报告没有生成。

代码:

public class SampleTc1 
{
static WebDriver driver;
static ExtentReports report;
static ExtentTest logger;
    static void testcase1()
    {
        System.setProperty("webdriver.chrome.driver","chromedriver.exe");
        driver = new ChromeDriver();
        driver.get("https://www.google.co.in");
        logger.log(LogStatus.PASS, "This step is passed");
        driver.close(); 
    }   
}

public class SampleTc2 
{
    static WebDriver driver;
    static ExtentReports report;
    static ExtentTest logger;

    static void testcase2()
    {
        System.setProperty("webdriver.chrome.driver","chromedriver.exe");
        driver = new ChromeDriver();
        driver.get("https://www.google.co.in");
        logger.log(LogStatus.PASS, "This step is passed");
        driver.close(); 
    }   
}

Main Class:
public class Maindriver {
    static WebDriver driver;
    static ExtentReports report;
    static ExtentTest logger;
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        report=new ExtentReports("./Report/ExtentReport/ExecutionResult.html", true);

        logger=report.startTest("TC1", "Testc Case1");
        SampleTc1.testcase1();
        report.endTest(logger);

        logger=report.startTest("TC2", "Testc Case2");
        SampleTc2.testcase2();
        report.endTest(logger);

        report.flush();     
    }

}

After running no reports are getting generated and it is showing null ponter exception:
Exception in thread "main" java.lang.NullPointerException
    at SmokeTest.SampleTc1.testcase1(SampleTc1.java:24)
    at SmokeTest.Maindriver.main(Maindriver.java:22)

以上我得到的例外情况。

提前致谢。

4 个答案:

答案 0 :(得分:0)

如果您使用testng运行selenium suite,则可以将范围报告实现为侦听器。

像,

    public class ExtentReporterNG implements IReporter 
    {

public ExtentReports extent;
private void buildTestNodes(IResultMap testMap, LogStatus status) 

{

    ExtentTest test;

    if (testMap.size() > 0) 
    {
        for (ITestResult result : testMap.getAllResults()) 
        {
            //test = extent.startTest(result.getInstance().getClass().getSimpleName(),result.getMethod().getMethodName());
            test = extent.startTest(result.getMethod().getMethodName().toUpperCase(),result.getInstance().getClass().getSimpleName().toUpperCase());
            test.assignCategory(result.getInstance().getClass().getSimpleName().toUpperCase());
            test.setStartedTime(getTime(result.getStartMillis()));
            for (String group : result.getMethod().getGroups())
                test.assignCategory(group);
            String message = "Test " + status.toString().toLowerCase() + "ed";
            if (result.getThrowable() != null)
                message = result.getThrowable().getMessage();
            test.setEndedTime(getTime(result.getEndMillis()));
            test.log(status, message);
            extent.endTest(test);
        }
    }
}

private Date getTime(long millis) 
{
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(millis);
    return calendar.getTime();        
}
@Override
public void generateReport(List<XmlSuite> xmlsuite, List<ISuite> suites,String file) 
{
    final String filePath=GlobalSettings.getProperty(GlobalSettings.EXTENTFILE);
    extent = new ExtentReports(filePath, true,DisplayOrder.NEWEST_FIRST,NetworkMode.OFFLINE );
    extent.loadConfig(new File("./config/extentConfig.xml"));
    for (ISuite suite : suites) 
    {
        Map<String, ISuiteResult> result = suite.getResults();
        for (ISuiteResult r : result.values()) 
        {
            ITestContext context = r.getTestContext();
            buildTestNodes(context.getPassedTests(), LogStatus.PASS);
            buildTestNodes(context.getFailedTests(), LogStatus.FAIL);
            buildTestNodes(context.getSkippedTests(), LogStatus.SKIP);
            buildTestNodes(context.getFailedConfigurations(),LogStatus.FAIL);
            buildTestNodes(context.getSkippedConfigurations(),LogStatus.SKIP);
        }
    }
    extent.flush();
    extent.close();
}}

答案 1 :(得分:0)

It is not well organized, hard to say what/where is the problem ...

You can check here a selenium webdriver testng tutorial I used it when I started and it is a good starting point!

答案 2 :(得分:0)

您必须在基类中将ExtentReports更改为静态。我是从学习自动化的硒自动化培训师Mukesh Otwani那里得到这个想法的。

答案 3 :(得分:-1)

我为范围报告创建了单独的类,因此避免了NULL指针异常