0

This is my Code in my Listener class using extent Reports.

I am using Common Helper class which contains @Beforetest & @After test...etc.

I even tried to put th @Beforetest in this listener class. But error is coming.

Any other way to capture screen shot without using driver?

public class CommonITestNGListener implements ITestListener{

    //Extent Report Declarations
    private static ExtentReports extent = ExtentManager.createInstance();
    private static ThreadLocal<ExtentTest> test = new ThreadLocal<>();
    
    //Other Declartions
    WebDriver driver = commonhelper.driver;
    public static DateFormat DF = new SimpleDateFormat("dd-MM-yyyy_HH_mm_ss");
    public static Date D = new Date();
    public static String time = DF.format(D);
    public String ErrSS = System.getProperty("user.dir")+"\\Screenshots\\";
    
    
    @Override
    public synchronized void onStart(ITestContext context) {
        System.out.println("...Test Suite Execution started...");
    }
    
    @Override
    public synchronized void onFinish(ITestContext context) {
        System.out.println("...Test Suite Execution Ended...");
        File f = new File(System.getProperty("user.dir")+"\\TestReport\\Test_Automaton_Report.html");
        if (f.exists()) {
            String oldtDir = System.getProperty("user.dir") + "\\TestReport\\Old\\";
            File fold = new File(oldtDir);
            String rn = "Test_Automaton_Report_bkp_"+time+".html";
            System.out.println("A New Report Generated with name  : Test_Automaton_Report.html"+ "\n" +"Existing Old Report will moved to : TestReport\\Old and Renamed as = " + rn);
            File nf = new File(rn);
            f.renameTo(nf);
            try {
                FileUtils.moveFileToDirectory(nf, fold, true);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        extent.flush();
    }
    
    @Override
    public synchronized void onTestStart(ITestResult result) {
        System.out.println(result.getMethod().getMethodName() + " Started!");
        ExtentTest extentTest = extent.createTest(result.getMethod().getMethodName(), result.getMethod().getDescription());
        test.set(extentTest);
    }

    @Override
    public void onTestSuccess(ITestResult result) {
        System.out.println(result.getMethod().getMethodName() + " Passed!");
        test.get().pass("... Test Passed ...");
    }

    @Override
    public void onTestFailure(ITestResult result) {
        System.out.println(result.getMethod().getMethodName() + " Failed!");
        test.get().fail(result.getThrowable());
        try {
            test.get().addScreenCaptureFromPath(CaptureScreenShot(result.getMethod().getMethodName()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onTestSkipped(ITestResult result) {
        System.out.println(result.getMethod().getMethodName() + " Skipped!");
        test.get().skip(result.getThrowable());
    }

    @Override
    public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
        System.out.println("onTestFailedButWithinSuccessPercentage for" + result.getMethod().getMethodName());
    }

    //Capture Screen shot (with Normal Java Utility)
    public String CaptureScreenShot(String screenshotname) throws Exception{
        TakesScreenshot ts = (TakesScreenshot)driver;
        File Source = ts.getScreenshotAs(OutputType.FILE);
        String dest = ErrSS + screenshotname+ "_"+time+".png";
        File destination = new File(dest);
        FileUtils.copyFile(Source, destination);
        System.out.println("Screen shot captured for the error and saved");
        return dest;
    }

}

There were no syntax errors displayed. But while running the Script it fails on taking screen shot.

Following is the output.

NavtoWQ Started!
NavtoWQ Passed!
NavtoWQ2 Started!
NavtoWQ2 Failed!
java.lang.NullPointerException
    at common.CommonITestNGListener.CaptureScreenShot(CommonITestNGListener.java:100)
    at common.CommonITestNGListener.onTestFailure(CommonITestNGListener.java:79)
    at org.testng.internal.TestListenerHelper.runTestListeners(TestListenerHelper.java:67)
    at org.testng.internal.Invoker.runTestListeners(Invoker.java:1388)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:633)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:716)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:988)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
...Test Suite Execution Ended...
2
  • can you share before test method? Commented Jun 21, 2018 at 11:13
  • I am using the latest version of Extent Reports 3.1.5 Commented Jun 21, 2018 at 12:11

2 Answers 2

1

You are getting the NULL Pointer Exception due to the driver. I would recommend to use After test method to capture the screenshot for the failure scenarios. Please add the below method in your Common Helper Class( I assumed that you will have driver instance in Common Helper Class). Please make sure to comment out the try catch block in onTestFailure method.

Code needs to be added in Common Helper Class:

@AfterMethod(alwaysRun = true)
public void captureScreenshot(ITestResult result){
    //Take the Screenshot Only, If the Test is failed.
    // Change the condition , If the screenshot needs to be taken for other status as well
    if(ITestResult.FAILURE==result.getStatus()){
        System.out.println("Failed Status Check");
        File temp= ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        String dest = ErrSS + screenshotname+ "_"+time+".png";
        try{
            FileUtils.copyFile(temp,new File(dest));
        }
        catch(Exception e){
            System.out.println(e.getStackTrace());
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

This is the answer you are looking for (answered by me 2-3 days ago):
Extent Report 3 Add Screenshot

I think you got the same problem here, i.e, driver is not getting in listener.

1 Comment

could you please upvote this also and mark it as accepted. I have just started answering questions so want to speed up the scoring.Thanks in advance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.