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

Two class files are not running continuously, second class files fails to run due to null pointer #453

Closed SreenivasanM closed 8 years ago

SreenivasanM commented 8 years ago

Please find the below error message.

FAILED CONFIGURATION: @AfterMethod afterMethod([TestResult name=LoginReports status=FAILURE method=Reports.LoginReports()[pri:0, instance:com.test.Reports@343f4d3d] output={null}]) java.lang.NullPointerException at com.extentreports.BaseExample.afterMethod(BaseExample.java:28)

If I run both the files individually it works, running as a suite fails in second class file. Please help out to solve the issue

SreenivasanM commented 8 years ago

@anshooarora - Could you please give some inputs on my tickets ?

anshooarora commented 8 years ago

Please share your code so I can reproduce this error.

SreenivasanM commented 8 years ago

Test Case 1

package com.test;

public class Reports extends BaseExample {

//Create instance for Appium Driver 
 public static AppiumDriver driver;

 @BeforeClass
 public void SetupLaunch() throws IOException, InterruptedException {

     DesiredCapabilities cap = new DesiredCapabilities();

     // Real Device
     cap.setCapability(MobileCapabilityType.DEVICE_NAME, "a9e3ec36");

     /* Emulators
     cap.setCapability(MobileCapabilityType.DEVICE_NAME, "Android emulator");
     cap.setCapability(MobileCapabilityType.DEVICE_NAME, "192.168.56.101:5555"); 
     cap.setCapability("avd", "Google Nexus 4 - 5.1.0 - API 22 - 768x1280");
      */

     cap.setCapability(MobileCapabilityType.APP_PACKAGE, "in.impaact.demo");
     driver = new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), cap);
     driver.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS);
 }

 @Test(priority=0)
  public void LoginReports() throws InterruptedException {

      LoginPage loginPage = new LoginPage(driver);

      test = extent.startTest("LoginReports");
      test.log(LogStatus.INFO, "Login Page");

      loginPage.Login("222222222222", "anjisony");
      Thread.sleep(5000);

      Assert.assertEquals(test.getRunStatus(), LogStatus.PASS);
  }

  @Test(priority=1)
  public void ReportsWebviewLogin() throws InterruptedException {

      test = extent.startTest("ReportsWebviewLogin");
      test.log(LogStatus.INFO, "Navigating to Reports");

      HomePage home = new HomePage(driver);
      Common com = new Common(driver);

      WebDriverWait waitr = new WebDriverWait(driver, 100);
      waitr.until(ExpectedConditions.elementToBeClickable(home.ReportsBtn));

      home.ReportsBtn.click();

      Set<String> contextNames = driver.getContextHandles();
      System.out.println("No. of Context" + contextNames.size());

      for (String contextName : contextNames) {
      System.out.println(contextName);
      }

      driver.context("WEBVIEW_in.impaact.demo");

      //explicit wait for search field
      WebDriverWait wait = new WebDriverWait(driver, 100);
      wait.until(ExpectedConditions.elementToBeClickable(By.name("aadhar_id")));

      driver.findElementByName("aadhar_id").sendKeys("222222222222");
      driver.findElementByName("password").sendKeys("anjisony");
      driver.hideKeyboard();
      driver.findElementByXPath("//input[@value='Sign In']").click();
      driver.context("NATIVE_APP");
      ExplicitWait(home.backbutton);
      home.backbutton.click();

      Assert.assertEquals(test.getRunStatus(), LogStatus.PASS);

  }

  public void ExplicitWait(WebElement element) {
      (new WebDriverWait(driver,100)).until(ExpectedConditions.elementToBeClickable(element));
  }

  @AfterClass
  public void StopDriver() throws IOException {

      HomePage home = new HomePage(driver);
      home.Demo.click();
      home.Logout.click();
      LoginPage loginPage = new LoginPage(driver);
      ExplicitWait(loginPage.email);
      driver.quit();
  }
}

**Base Class**

package com.extentreports;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;

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

public abstract class BaseExample {
    protected ExtentReports extent;
    protected ExtentTest test;

    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
    Date date = new Date();

    final String filePath = "D:\\Impaact Automation Reports_" + dateFormat.format(date) + ".html " ;

    @AfterMethod
    protected void afterMethod(ITestResult result) {
        if (result.getStatus() == ITestResult.FAILURE) {
            test.log(LogStatus.FAIL, "Test Failed " + result.getThrowable());
        } else if (result.getStatus() == ITestResult.SKIP) {
            test.log(LogStatus.SKIP, "Test Skipped " + result.getThrowable());
        } else {
            test.log(LogStatus.PASS, "Test Passed");
        }

        extent.endTest(test);        
        extent.flush();
    }

    @BeforeSuite
    public void beforeSuite() {
        extent = ExtentManager.getReporter(filePath);
    }

    @AfterSuite
    protected void afterSuite() {
        extent.close();
    }
}

Test Case 2

package com.test;

public class Enterprise extends BaseExample {

//Create instance for Appium Driver 
 public static AppiumDriver driver;

 @BeforeClass
 public void SetupLaunch() throws IOException, InterruptedException {

     DesiredCapabilities cap = new DesiredCapabilities();

     // Real Device
     cap.setCapability(MobileCapabilityType.DEVICE_NAME, "a9e3ec36");

     /* Emulators
     cap.setCapability(MobileCapabilityType.DEVICE_NAME, "Android emulator");
     cap.setCapability(MobileCapabilityType.DEVICE_NAME, "192.168.56.101:5555"); 
     cap.setCapability("avd", "Google Nexus 4 - 5.1.0 - API 22 - 768x1280");
      */

     cap.setCapability(MobileCapabilityType.APP_PACKAGE, "in.impaact.demo");
     driver = new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), cap);
     driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);
 }

  @Test(priority=0)
  public void Login() throws InterruptedException {

      test = extent.startTest("Login");
      test.log(LogStatus.INFO, "Login Page");

      LoginPage loginPage = new LoginPage(driver);

      loginPage.Login("222222222222", "anjisony");
      Thread.sleep(5000);

      Assert.assertEquals(test.getRunStatus(), LogStatus.PASS);
  }

  @Test(priority=1)
  public void addHotspotPoint() throws InterruptedException {

      test = extent.startTest("Add Hotspot Point");
      test.log(LogStatus.INFO, "Started adding HotspotPoint");

      HomePage home = new HomePage(driver);
      Common com = new Common(driver);

      ExplicitWait(home.EnterpriseCensus);
      home.EnterpriseCensus.click();

      ExplicitWait(home.MyLocation);
      home.MyLocation.click();
      home.MyLocation.click();
      home.MyLocation.click();
      Thread.sleep(2000);
      com.swipingHorizontal("RighttoLeft", driver);
      com.swipingVertical("BottomtoTop",driver);
      Thread.sleep(5000);
      home.Add_Hotspot_Point.click(); 
      home.GoogleMap.click();
      home.ScrollSelector("Select Hotspot Point", "MAIN CENTER", driver);
      home.ScrollSelector("Select Size Of Hotspot Point", "Large", driver);
      home.Ok.click();
      driver.findElement(By.xpath("//*[contains(@content-desc, 'MAIN CENTER.')]")).isDisplayed();
      home.backbutton.click();
      Thread.sleep(5000);

      Assert.assertEquals(test.getRunStatus(), LogStatus.PASS);
  }

  public void ExplicitWait(WebElement element) {
      (new WebDriverWait(driver,100)).until(ExpectedConditions.elementToBeClickable(element));
  }

  @AfterClass
  public void StopDriver() throws IOException {

      HomePage home = new HomePage(driver);
      home.Demo.click();
      home.Logout.click();
      LoginPage loginPage = new LoginPage(driver);
      ExplicitWait(loginPage.email);
      driver.quit();
  }
}
anshooarora commented 8 years ago

Are you running them parallel? As classes, or methods?

I am assuming you are running both classes parallel - in which case, I would recommend using ExtentTestManager to manage your tests. A very basic design pattern is shown here: http://extentreports.relevantcodes.com/java/#parallel-classes. If not, you would have to manage a single test instance per class and you won't receive NullPointers.

SreenivasanM commented 8 years ago

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

``` ```
anshooarora commented 8 years ago

If using parallel methods, see here.