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 {
- System.out.println("Capturing screen.........");
- String filepath = file1+"\\image"+sdf.format(date)+new Date().getTime()+".png";
- File srcfile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
- System.out.println("capturing..............");
- FileUtils.copyFile(srcfile, new File(filepath));
- Count++;
- String link1 = "<a href = "+filepath+">screenshot"+Count+"</a>";
- org.testng.Reporter.log(link1);
- System.out.println("Image Captured");
} catch (IOException e) {
e.printStackTrace();
}
}
In the above code the
- Just a System.out.println in the console
- location where the captured file is stored
- This is a selenium command, where the output image is stored as Output file
- System.out.println in the console
- Copying the srcfile to new File path
- Count++ is used to show the image names like screen1, screen2
- This works as link which is helpful in reporting
Comments