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...