How to take screenshot on test fail and test skip

Step 1 : Right click on the package you want, Create class to handle the screenshot for us

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
public class TakeScreenShot {
  private static WebDriver driver;
  
  public static void init(WebDriver d) {
      driver = d;
  }
  
  public static void takeSnapShot(String methodName) {
      String folderBaseDriver = null;
      if (driver instanceof InternetExplorerDriver) {
          folderBaseDriver = "Internet Explore";
      } else if (driver instanceof ChromeDriver) {
          folderBaseDriver = "Chrome";
      } else if (driver instanceof FirefoxDriver) {
          folderBaseDriver = "Firefox";
      }
      String screenshootDir = "path to your folder you want to save the screenshot"+File.separator + folderBaseDriver;
      File f = new File(screenshootDir);
      if (!f.exists() || !f.isDirectory()) {
          f.mkdirs();
      }
      File output = null;
      File file;
      if (folderBaseDriver.equalsIgnoreCase("Internet Explore")) {
          ScreenRegion s = new DesktopScreenRegion();
          try {
              ImageIO.write(s.capture(), "png", new File(screenshootDir
                      + File.separator + methodName + ".png"));
          } catch (IOException e) {
              e.printStackTrace();
          }
      }else{
          output = ((TakesScreenshot) driver)
                  .getScreenshotAs(OutputType.FILE);
          file = new File(screenshootDir, methodName + ".png");
          try {
              FileUtils.copyFile(output, file);
          } catch (IOException e) {
              e.printStackTrace();
          }
      }

  }
  }

Step 2 : Right click on the package of project and Create class ItestListen implement ITestListener of testNG

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class ItestListen implements ITestListener {
  
  public void onTestFailure(ITestResult result) {
      try {
          TakeScreenShot.takeSnapShot(result.getName());
      } catch (Exception e) {
      }
  }

  public void onTestSkipped(ITestResult result) {
      try {
          TakeScreenShot.takeSnapShot(result.getName());
      } catch (Exception e) {
      }
  }
}

Step 3 : Open file xml that allow you to manage the test script of testNG and Define your listener to the xml file of TestNG

1
2
3
4
5
<suite name="Suite" parallel="none">
<listeners>
      <listener class-name="com.cmg.pl.listenner.ItestListen" />
</listeners>
</suite>

Step 4 : init the TakeScreenShot class in before method of each test script

1
2
3
4
5
6
@BeforeMethod
public void beforeMethod(String browser, String super_user_name, String super_user_pass, String walled_ref_no01) 
{
  driver = DriverUtil.getInstance(browser);
  TakeScreenShot.init(driver);
}

Step 5 : Run your test by right click on your xml of TestNG. If any test script had fail or skip , it will had the screenshot in the folder you define in step 1. Have fun!