0

It wouldbe silly question but would be great help it you can help me out. I tried implementing extent report for multple test cases, but report are not getting generated .

Code:

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)

Above exception I am getting.

Thanks in advance.

viki
  • 11
  • 1
  • 7
  • 3
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Saurabh Gaur Sep 11 '16 at 08:55
  • You are using `logger` which is Initialized as null, this is throwing null pointer.. – Saurabh Gaur Sep 11 '16 at 09:06
  • @SaurabhGaur yes Saurabh I dubugged and saw it...how to over this one..I would be missing somewhere..can you help me out – viki Sep 11 '16 at 09:45
  • Follow this link http://stackoverflow.com/questions/5712485/how-to-use-java-util-logger-in-a-web-application – Saurabh Gaur Sep 11 '16 at 11:27
  • 1
    @SaurabhGaur thanks saurabh, I fixed it..its working,..thanks – viki Sep 11 '16 at 13:33

4 Answers4

0

If you are using testng for running selenium suite, you can implement extent reports as a listener.

Like,

    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();
}}
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!

Neagu V
  • 470
  • 4
  • 16
0

You have to change your ExtentReports as static in the base class. I got this idea from Mukesh Otwani, Selenium automation trainer for Learn-Automation.

double-beep
  • 5,031
  • 17
  • 33
  • 41
-1

I created separate class for extent report, hence avoided NULL pointer exception

viki
  • 11
  • 1
  • 7