11package UITestFramework .retryLogic ;
22
33
4+ import java .io .File ;
5+ import java .io .IOException ;
6+ import java .text .SimpleDateFormat ;
7+ import java .util .Date ;
48import java .util .Set ;
9+
10+ import org .apache .commons .io .FileUtils ;
11+ import org .openqa .selenium .OutputType ;
12+ import org .openqa .selenium .TakesScreenshot ;
13+ import org .openqa .selenium .WebDriver ;
514import org .testng .ITestContext ;
615import org .testng .ITestListener ;
716import org .testng .ITestNGMethod ;
817import org .testng .ITestResult ;
18+ import UITestFramework .CreateSession ;
919
10- public class TestListener implements ITestListener {
11- @ Override
20+ public class TestListener extends CreateSession implements ITestListener {
21+
22+
1223 public void onFinish (ITestContext context ) {
1324 Set <ITestResult > failedTests = context .getFailedTests ().getAllResults ();
1425 for (ITestResult temp : failedTests ) {
@@ -23,20 +34,37 @@ public void onFinish(ITestContext context) {
2334 }
2435 }
2536
26- public void onTestStart (ITestResult result ) {
27-
28- }
37+ public void onTestStart (ITestResult result ) { }
2938
3039 public void onTestSuccess (ITestResult result ) { }
31-
32- public void onTestFailure (ITestResult result ) { }
40+
41+ @ Override
42+ public void onTestFailure (ITestResult result ) {
43+ Object TestListener = result .getInstance ();
44+ WebDriver webDriver = ((CreateSession ) TestListener ).getDriver ();
45+ if (webDriver != null )
46+ {
47+ File scr = ((TakesScreenshot )webDriver ).getScreenshotAs (OutputType .FILE );
48+ String filename = new SimpleDateFormat ("yyyyMMddhhmmss'.jpg'" ).format (new Date ());
49+ File dest = new File ("./Screenshots/" + filename ); //Directory where Screenshot get saved.
50+ try
51+ {
52+ FileUtils .copyFile (scr , dest );
53+ }
54+ catch (IOException e ) {
55+ // TODO Auto-generated catch block
56+ e .printStackTrace ();
57+ }
58+ }
59+ }
3360
3461 public void onTestSkipped (ITestResult result ) { }
3562
3663 public void onTestFailedButWithinSuccessPercentage (ITestResult result ) { }
3764
38- public void onStart (ITestContext context ) {
39- }
40- }
65+ public void onStart (ITestContext context ) { }
66+
67+ }
68+
4169
4270
0 commit comments