Bharat Mane
Bharat Mane

Reputation: 296

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

Upvotes: 0

Views: 3408

Answers (2)

Paweł Adamski
Paweł Adamski

Reputation: 3415

You should add timestamp to your file name, e.g.

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

    File dest= new File("filPath/screenshot_"+timestamp()+".png");
    FileUtils.copyFile(scr, dest);
    Thread.sleep(3000); 
}

public string timestamp() {
    return new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(new Date());
}

Upvotes: 2

Arnaud
Arnaud

Reputation: 17534

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

Upvotes: 2

Related Questions