anshooarora / extentreports-java

Community version of Extent API for Java has moved to https://github.com/extent-framework/
http://extentreports.com
Other
528 stars 318 forks source link

I m facing null pointer exception in the below mentioned scenario #1001

Open sanmativardhaman opened 6 years ago

sanmativardhaman commented 6 years ago

i have created a Base test class, which has all declarations w.r.t Extent reports,

i,e public class BaseTest { public WebDriver driver; public ExtentReports reports; public ExtentTest logger; public WebDriverWait wait; public WebElement myElement; public static Calendar c = Calendar.getInstance();

@BeforeSuite
public void baseSetUp()
{
    reports=new    

ExtentReports(System.getProperty("user.dir")+"\Reports\"+dateStamp()+"\"+"API_SmokeTest.html"); }

@AfterSuite
public void tearDown()
{
    reports.endTest(logger);
    reports.flush();
}

}. i have two test classes which extends this base test. In test class one and two i have created logger object i,e logger =reports.StartTest("tc_01") Test class one runs fine, In test class two also i have same line, i,e logger =reports.startTest("tc_02"), when i run my testng.xml, only first testng class is executing while the second class throws 'null pointer exception" for this line logger =reports.startTest("tc_02"). Kindly help

dipesh17 commented 6 years ago

As you are creating reports.StartTest for each test meaning each method will be treated as a test. In this case, Try creating logger =reports.StartTest("tc_01") in a @beforeMethod and @aftermethod in baseclass

@BeforeMethod()
public void setupExtentTest(Method result) 
{
// result.getName() = name of the test method executing
 logger =reports.StartTest(result.getName())
}
@afterMethod()
public void teardown(ITestResult result)
{
//result.getname() = name of the test method executing
reports.endTest(result.getname());
reports.flush();
}

@aftermethod will declare your testcase finished and it will close that extenttest instance. in @aftersuite you will close the report and flush the report.

I hope this will help.

VikashGittt commented 6 years ago

Even I am also facing the same problem. ExtentReports report; ExtentTest logger;

  1. I initialized in @BeforeSuite
  2. Then Initialized in @BeforeMethod
  3. In testng.xml there are 2 classes Class1 & Class2
  4. On execution of testng.xml - All @Test of class1 runs completely but Class2 throws null pointer exception error when it reads in BeforeMethod(Initialized as mentioned in step2)

Note: When I change BeforeSuite to BeforeMethod i.e initialization of is done in BeforeClass then It runs fine but it produces extent report of only 1 class.

Also I am using Aftermethod to Flush the report and quit driver. Any solution?

VinWip commented 6 years ago

I am also facing the same issue.. Found any solution for this?

VinWip commented 6 years ago

@anshooarora Please help to resolve this.