Mukul Rajput
Mukul Rajput

Reputation: 1

Cannot invoke "com.aventstack.extentreports.ExtentTest.info(String)" because "extentlisteners.ExtentListeners.test" is null

I was learning Selenium and was on a topic of Extent Reports. Now I'm getting this error when I'm implementing my previous code in a framework. This code worked once by restarting the IDE but it's not working anymore. Here is the code for my code.

Base test file:

package base;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.time.Duration;
import java.util.Properties;

import org.apache.log4j.PropertyConfigurator;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.log4testng.Logger;

import extentlisteners.ExtentListeners;
import utilities.DbManager;
import utilities.ExcelReader;
import utilities.MonitoringMail;

public class BaseTest {
    
    public static WebDriver driver;
    public static Properties OR = new Properties();
    public static Properties config = new Properties();
    public static FileInputStream fis;
    public static ExcelReader excel = new ExcelReader("./src/test/resources/excel/testdata.xlsx");
    public static Logger log = Logger.getLogger(BaseTest.class);
    public static MonitoringMail mail = new MonitoringMail();
    public static WebDriverWait wait;
    
    
    //findEle finds the element based on the select type and enters the value : ID/Xpath/Css
    public void type(String key, String value) {
        
        if(key.endsWith("ID")) {
            driver.findElement(By.id(OR.getProperty(key))).sendKeys(value);;
        }else if(key.endsWith("XPATH")) {
            driver.findElement(By.xpath(OR.getProperty(key))).sendKeys(value);
        }else if(key.endsWith("CSS")) {
            driver.findElement(By.cssSelector(OR.getProperty(key))).sendKeys(value);
        }
        log.info("Typing in an Element : " + key + " , entered the values as : " + value);

        ExtentListeners.test.info("Typing in an Element : " + key + " , entered the values as : " + value); //Getting error on this line
        
    }
    
    //clickEle clicks on the element based on the select type : ID/Xpath/Css
    public void click(String key) {
        
        if(key.endsWith("ID")) {
            driver.findElement(By.xpath(OR.getProperty(key))).click();
        }else if(key.endsWith("XPATH")) {
            driver.findElement(By.xpath(OR.getProperty(key))).click();
        }else if(key.endsWith("CSS")) {
            driver.findElement(By.xpath(OR.getProperty(key))).click();
        }
        log.info("Clicking on element : " + key);
        ExtentListeners.test.info("Clicking on element : " + key);
        
    }
    
    
    @BeforeSuite
    public void setUp() {
        
        if(driver == null) {
            
            PropertyConfigurator.configure("./src/test/resources/properties/log4j.properties"); //Configuring Log4j
            log.info("Test Execution Started");
            
            
            try {
                fis = new FileInputStream("./src/test/resources/properties/config.properties"); //Loading config file into FIS
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } 
            try {
                config.load(fis); //Loading FIS to config properties
                log.info("config.properties file loaded");
            } catch (IOException e) {
                e.printStackTrace();
            }
            
            
            try {
                fis = new FileInputStream("./src/test/resources/properties/OR.properties"); //Loading OR file into FIS
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } 
            try {
                OR.load(fis); //Loading FIS to OR properties
                log.info("config.properties file loaded");
            } catch (IOException e) {
                e.printStackTrace();
            }
            
            
            //Launching browser by checking which browser to launch from config file
            if(config.getProperty("browser").equals("chrome")){
                driver = new ChromeDriver();
                log.info("Chrome browser launched");
            }
            else if(config.getProperty("browser").equals("firefox")){
                driver = new FirefoxDriver();
                log.info("Firefox browser launched");
            }
            
            
            //navigating to URL provided in config file
            driver.get(config.getProperty("testsiteurl"));
            log.info("Navigating to test site : " + config.getProperty("testsiteurl"));
            
            
            //Maximizing browser window
            driver.manage().window().maximize();
            
            //Applying Implicit wait
            driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(Integer.parseInt(config.getProperty("implicit.wait"))));
            
            //Applying Explicit wait
            wait = new WebDriverWait(driver, Duration.ofSeconds(Integer.parseInt(config.getProperty("explicit.wait"))));
            
            
            //JDBC Connection
            try {
                DbManager.setMysqlDbConnection();
                log.info("Database Connection established");
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
    }
    
    @AfterSuite
    public void tearDown() {
        driver.quit();
        log.info("Test Execution Completed");
    }

}

LoginTest file:

package testCases;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import base.BaseTest;

public class LoginTest extends BaseTest {
    
    @Test(dataProvider = "Data")
    public void doLogin(String username, String password) {
        
        type("username_ID",username);
        type("password_ID",password);
        click("loginBtn_XPATH");
        
    }
    
    @DataProvider(name="Data")
    public Object[][] getData() {
        
        String sheetName = "LoginTest";
        int rowNum = excel.getRowCount(sheetName);
        int colNum = excel.getColumnCount(sheetName);
        
        excel.getCellData(sheetName, colNum, rowNum);
        
        Object[][] data = new Object[rowNum-1][colNum];
        int row,col;
        for(row=2;row<=rowNum;row++){
            for(col=0;col<colNum;col++){
                data[row-2][col]=excel.getCellData(sheetName, col, row);
            }
        }
        return data;
        
    }

}

ExtentListener:

package extentlisteners;

import java.util.Date;

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

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.markuputils.ExtentColor;
import com.aventstack.extentreports.markuputils.Markup;
import com.aventstack.extentreports.markuputils.MarkupHelper;



public class ExtentListeners implements ITestListener, ISuiteListener {

    static Date d = new Date();
    static String fileName = "Extent_" + d.toString().replace(":", "_").replace(" ", "_") + ".html";

    private static ExtentReports extent = ExtentManager
            .createInstance(".\\reports\\" + fileName);

    public static ExtentTest test;
    
    

    public void onTestStart(ITestResult result) {

        test = extent
                .createTest(result.getTestClass().getName() + "     @TestCase : " + result.getMethod().getMethodName());
    

    }

    public void onTestSuccess(ITestResult result) {

        String methodName = result.getMethod().getMethodName();
        String logText = "<b>" + "TEST CASE:- " + methodName.toUpperCase() + " PASSED" + "</b>";
        Markup m = MarkupHelper.createLabel(logText, ExtentColor.GREEN);
        test.pass(m);

    }

    public void onTestFailure(ITestResult result) {
        

        ///test.fail(result.getThrowable().getMessage());
        /*try {
            ExtentManager.captureScreenshot();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }*/
        String methodName=result.getMethod().getMethodName();
        String logText="<b>"+"TEST CASE:- "+ methodName.toUpperCase()+ " FAILED"+"</b>";        
    
    

        //test.fail("<b><font color=red>" + "Screenshot of failure" + "</font></b><br>",MediaEntityBuilder.createScreenCaptureFromPath(ExtentManager.fileName)
        //      .build());
    
        
        Markup m = MarkupHelper.createLabel(logText, ExtentColor.RED);
        test.log(Status.FAIL, m);
        
    }

    public void onTestSkipped(ITestResult result) {
        String methodName = result.getMethod().getMethodName();
        String logText = "<b>" + "Test Case:- " + methodName + " Skipped" + "</b>";
        Markup m = MarkupHelper.createLabel(logText, ExtentColor.AMBER);
        test.skip(m);

    }

    public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
        // TODO Auto-generated method stub

    }

    public void onStart(ITestContext context) {

    }

    public void onFinish(ITestContext context) {

        if (extent != null) {

            extent.flush();
        }

    }

    public void onStart(ISuite suite) {
        // TODO Auto-generated method stub
        
    }

    public void onFinish(ISuite suite) {
        // TODO Auto-generated method stub
        
    }

}

I have tried commenting the ExtentListener line and it worked fine, so there is no issue with fetching or inserting the data into elements. This code worked once, so I'm very confused that why isn't it working and throwing this error.

Error Message:

FAILED: testCases.LoginTest.doLogin("[email protected]", "kjsdfnvjndklsv") java.lang.NullPointerException: Cannot invoke "com.aventstack.extentreports.ExtentTest.info(String)" because "extentlisteners.ExtentListeners.test" is null at base.BaseTest.type(BaseTest.java:64) at testCases.LoginTest.doLogin(LoginTest.java:13) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at org.testng.internal.invokers.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:139) at org.testng.internal.invokers.TestInvoker.invokeMethod(TestInvoker.java:664) at org.testng.internal.invokers.TestInvoker.invokeTestMethod(TestInvoker.java:227) at org.testng.internal.invokers.MethodRunner.runInSequence(MethodRunner.java:50) at org.testng.internal.invokers.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:957) at org.testng.internal.invokers.TestInvoker.invokeTestMethods(TestInvoker.java:200) at org.testng.internal.invokers.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:148) at org.testng.internal.invokers.TestMethodWorker.run(TestMethodWorker.java:128) at java.base/java.util.ArrayList.forEach(ArrayList.java:1511) at org.testng.TestRunner.privateRun(TestRunner.java:848) at org.testng.TestRunner.run(TestRunner.java:621) at org.testng.SuiteRunner.runTest(SuiteRunner.java:443) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:437) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:397) at org.testng.SuiteRunner.run(SuiteRunner.java:336) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:95) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1280) at org.testng.TestNG.runSuitesLocally(TestNG.java:1200) at org.testng.TestNG.runSuites(TestNG.java:1114) at org.testng.TestNG.run(TestNG.java:1082) at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

Upvotes: 0

Views: 1048

Answers (1)

sashkins
sashkins

Reputation: 931

It seems like there is nothing wrong with your code.

The reason why 'ExtentListeners.test' is null, may be only related to the fact that onTestStart didn't run before your test.

Make sure that your listener is added to the TestNG XML:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="MyTestSuite">
    <listeners>
        <listener class-name="extentlisteners.ExtentListeners"/>
    </listeners>
    <test name="MyTest">
        <packages>
            <package name="testCases"/>
        </packages>
    </test>
</suite>

Or, in case you create a suite programmatically, don't forget to add your listener there:

    TestNG testng = new TestNG();
    testng.setTestClasses(new Class[] { LoginTest.class });
    testng.addListener(new ExtentListener());
    testng.run();

Also, you can set a breakpoint on your test initialization line in the onTestStart method, and execute your test in the debug mode to check what value is being set to it.

Upvotes: 0

Related Questions