How to capture screen while running test suite





  • Create a file path file1 location where  the image needs to be stored
  • SimpleDateFormat sdf = new SimpleDateFormat("dd_MM_yyyy_HH_mm_ssaa")
  • Date date=new Date();



public void screenshot() {
try {

  1. System.out.println("Capturing screen.........");
  2. String filepath = file1+"\\image"+sdf.format(date)+new Date().getTime()+".png";
  3. File srcfile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
  4. System.out.println("capturing..............");
  5. FileUtils.copyFile(srcfile, new File(filepath));
  6. Count++;
  7. String link1 = "<a href = "+filepath+">screenshot"+Count+"</a>";
  8. org.testng.Reporter.log(link1);
  9. System.out.println("Image Captured");

} catch (IOException e) {
e.printStackTrace();
}
}

In the above code the


  1. Just a System.out.println in the console
  2. location where the captured file is stored 
  3. This is a selenium command, where the output image is stored as Output file
  4. System.out.println in the console
  5. Copying the srcfile to new File path
  6. Count++ is used to show the image names like screen1, screen2
  7. This works as link which is helpful in reporting


Comments