0

I'm trying to use ExtentReports with ITestListener. The problem I'm facing is I'm unable to use instance of ExtentTest from ITestListener in Test class for logging. Please help me with this.

Below is the listener class.

package com.dice.listeners;

import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

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

public class ExtentListener implements ITestListener    
{
    private ExtentReports reports = new ExtentReports("File Location");
    private ExtentTest test;

    @Override
      public void onTestStart(ITestResult arg0)
    {
        test = reports.startTest(arg0.getMethod().getMethodName());
    }

    @Override
      public void onFinish(ITestContext arg0)
    {
        reports.endTest(test);
        reports.flush();
        reports.close();
    }

    @Override
      public void onTestFailure(ITestResult arg0)
    {
    }

    @Override
      public void onTestSuccess(ITestResult arg0)
    {
    }

    @Override
      public void onTestSkipped(ITestResult arg0)
    {
        // TODO Auto-generated method stub
    }

    @Override
      public void onStart(ITestContext arg0)
    {
        // TODO Auto-generated method stub
    }

    @Override
      public void onTestFailedButWithinSuccessPercentage(ITestResult arg0)
    {
        // TODO Auto-generated method stub
    }
}

Below is the test class.

package com.dice;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

import com.dice.base.BaseTest;
import com.dice.listeners.ExtentListener;
import com.relevantcodes.extentreports.*;

@Listeners(com.dice.listeners.ExtentListener.class)
public class FirstTest extends BaseTest {

    @Test
    public void firstTestMethod() {
        driver.get("http://www.dice.com");
        test.log(LogStatus.INFO, "opening dice.com"); /*'test' not same instance as in ExtentListener class*/
    }

    @Test
    public void secondTestMethod() {
        driver.get("http://www.linkedin.com");
        test.log(LogStatus.INFO, "opening linkedin.com");/*'test' not same instance as in ExtentListener class*/
    }
}
George Alexandria
  • 2,841
  • 2
  • 16
  • 24
ppal
  • 1
  • 1

1 Answers1

0

In the Class ExtentListener on the line no 13 the instance "test" is created as a "private" which is a access modifier which will restrict the visibility only into the class itself. So try changing the access modifier to public.

Please refer this to use a suitable access modifier to you requirement. Hope this helps.

Rahal Kanishka
  • 720
  • 13
  • 27