Bharat Mane Bharat Mane - 8 months ago 44
Java Question

How to take screenshots in Selenium at different time intervals and save it at different place?

By using below script, I am taking only one Screenshot every time, which it overrides on the same screenshot.

What should I do If I want to take different screenshots at some small time of interval?

public void screenShot() throws IOException, InterruptedException
File scr=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

File dest= new File("filPath/1.png");
FileUtils.copyFile(scr, dest);

Answer Source

Just use the current time to name your files, this example names the files after the current minutes and hours values (of course you may use seconds, milliseconds, days, whatever..) :

Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);

String directory = "filPath";
String fileName = "snapshot_"+ hour + "_"+ minute +".png";

File dest = new File(directory, fileName);