Bharat Mane Bharat Mane - 10 days ago 4
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);
Thread.sleep(3000);
}

Answer

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);