Null Pointer Exception in Extent Reports

1,515 views
Skip to first unread message

Madhurya A N

unread,
May 27, 2020, 10:43:09 AM5/27/20
to Selenium Users
Hi,

I am getting below null pointer exception when i run LoginTestCase, I have defined ExtentReports in base class and trying to extend it in LoginTestCase

Please suggest if i have missed anything. 

The same code is working fine if i write the extentreport method in LoginTestCase

FAILED: LoginPageTest
java.lang.NullPointerException
at com.testcases.LoginTest.LoginPageTest(LoginTest.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:135)

BaseClassCode:

public class BaseClass 
{
public static Properties prop;
public static FileInputStream fis;
public static WebDriver driver;
public static WebDriverWait wait;
public static ExtentHtmlReporter htmlreport;  
public static ExtentReports reports;
public static ExtentTest logger; 
public BaseClass() throws Throwable
{
prop = new Properties();
try 
{
fis = new FileInputStream("C:\\Users\\mna\\eclipse-workspace\\Automation\\src\\com\\config\\ConfigProperties");
catch (FileNotFoundException e) 
{
e.printStackTrace();
System.out.println("File not found");
}
prop.load(fis);
}
@BeforeTest
public void SetExtentReport()
{
htmlreport = new ExtentHtmlReporter(System.getProperty("user.dir") + "/test-output/myReport.html");
htmlreport.config().setDocumentTitle("Automation Report");
htmlreport.config().setTheme(Theme.STANDARD);
reports = new ExtentReports();
reports.attachReporter(htmlreport);
logger = reports.createTest("test");
}
@AfterTest
public void endExtentReport()
{
reports.flush();
}
@AfterMethod
public void Teardown(ITestResult result) 
{
if(result.getStatus() == ITestResult.FAILURE)
{
logger.log(Status.FAIL, MarkupHelper.createLabel(result.getName() + " - Test Case Failed", ExtentColor.RED));
logger.log(Status.FAIL, "TESTCASE IS FAILED - " + result.getName());  //return the name of the method
logger.log(Status.FAIL, "TESTCASE IS FAILED - " + result.getThrowable());  // throw exception to the report
}
else if(result.getStatus() == ITestResult.SKIP)
{
logger.log(Status.SKIP, "TESTCASE IS SKIPPED - " + result.getName());
}
else if(result.getStatus() == ITestResult.SUCCESS)
{
logger.log(Status.PASS, "TESTCASE IS PASSED - " + result.getName());
}
driver.quit();
}
public static void Initalization() throws Throwable
{
    if(prop.getProperty("browser").equals("chrome"))
{
   
String Chropath = "C:\\Users\\mna\\eclipse-workspace\\Automation\\Drivers\\chromedriver.exe";
System.setProperty("webdriver.chrome.driver",Chropath);
   
   ChromeOptions chromeOptions = new ChromeOptions();
   chromeOptions.addArguments("--verbose");
   chromeOptions.addArguments("--whitelisted-ips=''");
   chromeOptions.addArguments("--proxy-server=10.196.175.100:8080");
   driver = new ChromeDriver(chromeOptions);
   wait = new WebDriverWait(driver, 40);
}
    else if(prop.getProperty("browser").equals("firefox"))
    {
    String Firepath = "C:\\Users\\mna\\eclipse-workspace\\Automation\\drivers\\geckodriver.exe";
System.setProperty("webdriver.gecko.driver",Firepath);
driver = new FirefoxDriver();
    }
    driver.get(prop.getProperty("applicationurl"));
    driver.manage().window().maximize();
    driver.manage().deleteAllCookies();
    driver.manage().timeouts().implicitlyWait(testutil.IMPLICIT_WAIT_TIMEOUT, TimeUnit.SECONDS);
    driver.manage().timeouts().pageLoadTimeout(testutil.PAGELOAD_WAIT_TIMEOUT, TimeUnit.SECONDS);
    System.out.println("Application launched successfully");
}
}

LoginTestClass:
public class LoginTest extends BaseClass
{
LoginPage loginpage;
DashboardPage dashboardpage;
ExtentHtmlReporter htmlreport;  
ExtentReports reports;
ExtentTest logger;   
public LoginTest() throws Throwable 
{
super();
}
@BeforeMethod
public void SetUp() throws Throwable
{
Initalization();
loginpage = new LoginPage();
dashboardpage = new DashboardPage();
}
@Test(priority = 0)
public void VerifyTitleTest()
{
//logger = reports.createTest("Verify Title");
String PageTitle = loginpage.VerifyPageTitle();
}
@Test(priority = 1)
public void VerifyLogoTest()
{
boolean PLogo = loginpage.VerifyLogo();
Assert.assertEquals(PLogo , true);
logger.log(Status.PASS, "Verified logo");
}
@Test(priority = 2)
public void LoginPageTest() throws Throwable
{
//logger = reports.createTest("Verify Login");
dashboardpage = loginpage.Login(prop.getProperty("username"), prop.getProperty("password"));
System.out.println("Logged in Successfully");
logger.log(Status.PASS, "Logged in successfully");
}
}



Mansi Uppal

unread,
May 27, 2020, 11:05:49 AM5/27/20
to seleniu...@googlegroups.com
HI,

In your LoginTest class, try to write this

test = extent.createTest(getClass().getSimpleName());

This should be the first line in your test method. Or you can also put this in @BeforeClass.

Failing to do so will result in NullPointerException in Extent reports.

Thanks & Regards,
Mansi Uppal


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/bf056be5-dba7-4109-85d3-65b2ba7ae0bf%40googlegroups.com.

Villju Joseph

unread,
May 27, 2020, 11:16:49 AM5/27/20
to seleniu...@googlegroups.com
Why can't u use extent reporting added in hooks.Add dependency in Pom.Also set the source for extent report point to generated json report file in target .
Thanks, 
Villju

Reply all
Reply to author
Forward
0 new messages